📄 initializetraining.cpp
字号:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <float.h>
#include "initializeTraining.h"
FeaturePtr **example;
int * target;
double * lambda;
int *nonZeroFeature;
double *error;
int *nonBound;
double *weight;
int numExample;
int maxFeature;
int numNonBound;
int *unBoundIndex;
int dataSetSize;
int *errorCache;
int *nonZeroLambda;
/*******private defines and data structure******/
#define DATATEMP1 10000
#define DATATEMP2 1000
//DBL_EPSILON 2.2204460492503131e-016 Smallest such that 1.0+DBL_EPSILON !=1.0
#define EPS DBL_EPSILON
//LONG_MAX 2147483647 Maximum (signed) long value
#define MAXLINE LONG_MAX
static int exampleIndex;
static int featureIndex;
/*********declared function********/
static double dotProduct(Feature *x,int sizeX,Feature *y,int sizeY);
static int initializeData(int size);
/****private function**********/
static int initializeData(int size)
{
example = (FeaturePtr **)malloc(size * sizeof(FeaturePtr*));
if(example == NULL)
{
fprintf(stderr,"Memory allocation failed.\n");
return 0;
}
target =(int *)malloc(size*sizeof(int));
if(target == NULL)
{
fprintf(stderr,"Memory allocation failed.\n");
return 0;
}
nonZeroFeature =(int *)malloc(size*sizeof(int));
if(nonZeroFeature == NULL)
{
fprintf(stderr,"Memory allocation failed.\n");
return 0;
}
error =(double *)malloc(size*sizeof(double));
if(error == NULL)
{
fprintf(stderr,"Memory allocation failed.\n");
return 0;
}
return 1;
}
/*********public function************/
int readFile(FILE *in)
{
char temp[DATATEMP1];
char temp2[DATATEMP2];
char label[DATATEMP1];
int numFeature;
int i,j;
int lineCount;
int idValue;
float featureValue;
char c;
extern int maxFeature;
extern int numExample;
int numZeroFeature =0;
lineCount =0;
dataSetSize =0;
/*****estimate the number of example*****/
while(fgets(temp,MAXLINE,in) !=NULL)
dataSetSize++;
dataSetSize++;
rewind(in);
if(!initializeData(dataSetSize))
{
fprintf(stderr,"Error in initializeing data\n");
return 0;
}
numExample=0;exampleIndex =0;
maxFeature =0;
printf("Reading training file...\n");
c=getc(in);
while(c!=EOF){
/****ignore comment and comment blank lines*****/
while(c == '#'||c =='\n'){
if (c =='#'){
while((c =getc(in)) !='\n')
;
}
if(c =='\n')
c=getc(in);
}
if (c == EOF)
break;
else { ///read one line of description
exampleIndex++;
i =0;
numFeature =0;
temp [i]=c;
i++;
c= getc (in);
while( (c !='\n')){
temp[i] =c;
i++;
if(c ==':')
numFeature++;
c=getc(in);
}
temp[i]='\0';
lineCount++;
/******each line should start with a class label****/
j=0;
while( temp[j] !=' '){
label[j] = temp[j];
j++;
}
label[j]='\0';
if( atoi(label) !=1 && atoi(label) !=-1){
fprintf(stderr,"Expect a class label in line %d\n",lineCount);
return 0;
}
numExample++;
nonZeroFeature[exampleIndex] = numFeature;
if(numFeature !=0){
example[exampleIndex] =(FeaturePtr*)malloc(numFeature *sizeof(FeaturePtr));
if(example[exampleIndex] == NULL){
fprintf(stderr,"Memory allocation failed \n");
return 0;
}
for (j =0;j<numFeature;j++){
example[exampleIndex][j] = (FeaturePtr)malloc(sizeof(struct feature));
if(example[exampleIndex][j] == NULL){
fprintf(stderr,"Memory allocation failed \n");
return 0;
}
}
}
else{
/***********/
example[exampleIndex] =(FeaturePtr *)malloc(sizeof(FeaturePtr));
if(example[exampleIndex] == NULL){
fprintf(stderr,"Memory allocation failed \n");
return 0;
}
example[exampleIndex][0]=(FeaturePtr)malloc (sizeof(struct feature));
if(example[exampleIndex][0] == NULL){
fprintf(stderr,"Memory allocation failed \n");
return 0;
}
nonZeroFeature[exampleIndex] =0;
}
/***extract the class label of the example******/
i = 0;
while (temp[i] != ' '){
temp2[i] = temp[i];
i++;
}
temp2[i] ='\0';
target[exampleIndex] = atoi(temp2);
error[exampleIndex] = 0 - target[exampleIndex];
i++;
if(numFeature !=0){
/****extract and store the feature id/value pairs*********/
featureIndex =0;
while(temp[i] != '\0'){
j =0;
while(temp[i] !=':'){
temp2[j] = temp[i];
i++;j++;
}
temp2[j] = '\0';
if( sscanf(temp2,"%d",&idValue) == 0){
fprintf(stderr,"Expect an integer for id in line %d\n",lineCount);
return 0;
}
example[exampleIndex][featureIndex]->id = idValue; //atoi(temp2)
j =0; i++;
while( temp[i] !=' ' && temp[i] !='\0'){
temp2[j] = temp[i];
i++; j++;
}
temp2[j] ='\0';
if( sscanf(temp2,"%f",&featureValue) == 0){
fprintf(stderr,"Expect a real number for id in line %d\n",lineCount);
return 0;
}
if (fabs(atof(temp2)) >EPS){
example[exampleIndex][featureIndex]->value = featureValue; //atof(temp2)
featureIndex++;
}
} //////end while not end of line
/********update the number of nonzero features of the example and the target
feature id found so far for all the examples read.*********/
nonZeroFeature[exampleIndex] = featureIndex;
if(example[exampleIndex][featureIndex -1]->id >maxFeature)
maxFeature = example[exampleIndex][featureIndex-1]->id ;
}
} //end else
c=getc(in);
}//end while not eof
printf("Finish reading training file.\n");
return 1;
}
/********to initialize dpCache,weight vector,threshold b,lambda,nonBound arrays***/
int initializeTraining(void)
{
int i;
printf("Initializing data structures ...\n");
weight = (double*)malloc( (maxFeature+1)*sizeof(double));
if(weight ==NULL){
fprintf(stderr,"Memory allocation failed.\n");
return 0;
}
for (i=1;i<=maxFeature;i++)
weight[i] = 0;
lambda = (double*)malloc((numExample+1)*sizeof(double));
if(lambda ==NULL){
fprintf(stderr,"Memory allocation failed.\n");
return 0;
}
for(i=1;i<=numExample;i++)
lambda[i] =0;
nonBound =(int*)malloc((numExample+1)*sizeof(int));
if(nonBound ==NULL){
fprintf(stderr,"Memory allocation failed.\n");
return 0;
}
for (i=1;i<=numExample;i++)
nonBound[i]=0;
numNonBound =0;
unBoundIndex =(int*)malloc(numExample*sizeof(int));
if(unBoundIndex ==NULL){
fprintf(stderr,"Memory allocation failed.\n");
return 0;
}
errorCache =(int*)malloc(numExample*sizeof(int));
if(errorCache ==NULL){
fprintf(stderr,"Memory allocation failed.\n");
return 0;
}
nonZeroLambda =(int*)malloc(numExample*sizeof(int));
if(nonZeroLambda ==NULL){
fprintf(stderr,"Memory allocation failed.\n");
return 0;
}
printf("Finishing initializing data sturcture.\n");
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -