intMNIST::ReadFeatures(double**x,constchar*filename){
MPC_OPTION &mp =*MPC_OPTION::GetMPCOption();if(x ==NULL)return-1;//judge if input number of n and d out of the range of datasetint realFeatures =getFileColumns(filename);int reallables =getFileRows(filename);int number_of_features = mp.local_d;int number_of_lables = mp.local_n;if(number_of_features > realFeatures || number_of_features <=0){Log(DEBUG_INFO,"input data_d out of range");return-1;}if(number_of_lables > reallables || number_of_lables <=0){Log(DEBUG_INFO,"input data_n out of range");return-1;}
std::ifstream fileStream;
fileStream.open(filename);if(!fileStream){Log(DEBUG_INFO,"[ReadFeatures] cannot open file:%s\n", filename);//std::cout<<"Unable to open file";return-1;}if(fileStream.is_open()){double tmp =0;//当前位置上的数值int colCount =0;// 列数计数器int index =0;// X[]数组的下标int maxIndex =(number_of_features +1)* number_of_lables;char douhao;double*xx =newdouble[maxIndex];while(!fileStream.eof()){
fileStream >> tmp;if(index < maxIndex){
xx[index]= tmp;//Log(DEBUG_INFO, "tmp: %.4f.", xx[index]);
index++;}elsebreak;if(colCount +1!= number_of_features &&'\n'!= fileStream.peek())// 未到行尾,colCount累加{++colCount;
fileStream >> douhao;}else//已到行尾,colCount清零{
colCount =0;// 列数清零while('\n'!= fileStream.peek()){
fileStream >> douhao;
fileStream >> tmp;}
xx[index]=1;
index++;}}double*xxx =newdouble[maxIndex];stdnorm(xxx, xx);*x =newdouble[maxIndex];for(int i =0; i < maxIndex; i++){(*x)[i]= xxx[i]*(double)(1<< mp.fraction_bits);//turn xx to xxx can do stdnorm}//DoubleToFixPoint(*x, xxx, maxIndex);// 关闭文件delete[]xx;delete[]xxx;
fileStream.close();return number_of_features +1;}return0;}
读取标签
intMNIST::ReadLabels(double**y,constchar*filename){
MPC_OPTION &mp =*MPC_OPTION::GetMPCOption();if(y ==NULL)return-1;//judge if input number of n out of the range of datasetint reallables =getFileRows(filename);int number_of_lables = mp.local_n;if(number_of_lables > reallables || number_of_lables <=0){Log(DEBUG_INFO,"input data_n out of range");return-1;}
std::ifstream fileStream;
fileStream.open(filename);if(!fileStream){Log(DEBUG_INFO,"[ReadFeatures] cannot open file:%s\n", filename);//std::cout<<"Unable to open file";return-1;}if(fileStream.is_open()){double tmp =0;//当前位置上的数值int colCount =0;// 列数计数器int index =0;// X[]数组的下标int maxIndex = mp.local_n;char douhao;double*yy =newdouble[maxIndex];while(!fileStream.eof()){
fileStream >> tmp;if(index < maxIndex){
yy[index]= tmp;//cout << (*X)[index] << endl;
index++;}else{break;}if(colCount +1!=1&&'\n'!= fileStream.peek())// 未到行尾,colCount累加{++colCount;
fileStream >> douhao;}else//已到行尾,colCount清零{
colCount =0;// 列数清零while('\n'!= fileStream.peek()){
fileStream >> douhao;
fileStream >> tmp;}}}*y =newdouble[maxIndex];for(int i =0; i < maxIndex; i++){(*y)[i]= yy[i]*(double)(1<< mp.fraction_bits);}//DoubleToFixPoint(*y, yy, maxIndex);// 关闭文件
fileStream.close();delete[]yy;return number_of_lables;}return0;}