www.pudn.com > myRBF.rar > RBF.CPP
#include "RBF.h" #include#include #include #include #include #include #include #include "matrix.h" using namespace std; #ifndef _INIFILE_H_ #define _INIFILE_H_ /* * char* GetInitKey(FileName, Section, Key) * Return Key=>Value * 读取ini文件 */ char * CRBF::GetInitKey(char *filename, char *title,char *key) { FILE * fp; char tmpLine[1024]; int rtnval; int i = 0; int flag = 0; char * tmp; static char tmpstr[1024]; if ((fp = fopen( filename, "r")) == NULL ) { return "have no such file"; } while (!feof(fp)) { rtnval = fgetc( fp ); if ( rtnval == EOF ) { break; } else { tmpLine[i++] = rtnval; } if ( rtnval == '\n') { tmpLine[--i]=0; i = 0; tmp = strchr(tmpLine, '='); if (( tmp != NULL )&&(flag == 1)) { if (strstr(tmpLine,key)!=NULL) { strcpy ( tmpstr, tmp + 1 ); fclose ( fp ); return tmpstr; } } else { strcpy(tmpstr,"["); strcat(tmpstr,title); strcat(tmpstr,"]"); if (strcmp(tmpstr,tmpLine)==0) { flag = 1; } } } } fclose ( fp ); return ""; } #endif //_INIFILE_H_*/ CRBF::CRBF() { int i; //读INI文件 WordNum=atoi(GetInitKey("C_SetOption.ini", "Num","WordNum")); TrainNum=atoi(GetInitKey("C_SetOption.ini", "Num","TrainNum")); TestNum=atoi(GetInitKey("C_SetOption.ini", "Num","TestNum")); SequNum=atoi(GetInitKey("C_SetOption.ini", "Num","SequNum")); Hidden=atoi(GetInitKey("C_SetOption.ini", "Num","HIDDEN")); Dimension=atoi(GetInitKey("C_SetOption.ini", "Num","Dimension")); effectiveH=Hidden+1;//cs TrainPath=GetInitKey("C_SetOption.ini", "Path","TrainPath"); TestPath=GetInitKey("C_SetOption.ini", "Path","TestPath"); Codebook=GetInitKey("C_SetOption.ini", "Path","Codebook"); Sigma=GetInitKey("C_SetOption.ini", "Path","Sigma"); Weight=GetInitKey("C_SetOption.ini", "Path","Weight"); Feature=GetInitKey("C_SetOption.ini", "Else","Feature"); Network=GetInitKey("C_SetOption.ini", "Else","Network"); SNR=GetInitKey("C_SetOption.ini", "Else","SNR"); inputPattern=new float*[TrainNum];//1 for( i=0 ; i >Filename; string FeatData=TrainPath+Filename; ifstream FeatdataIn; FeatdataIn.open(FeatData.c_str()); for(j=0;j >inputPattern[i][j]; } FeatdataIn.close(); FeatdataIn.clear(); } FeatfilenameIn.close(); } void CRBF:: calCenter() //中心半径 { int i,j,k; float *patternSum; patternSum= new float[Dimension]; float sigmaTemp; for(i=0;i elementAt(i,j) = centerOutput[i][j];//为第i,j个元素赋值 if (VT) VT->elementAt(j,i) = V->elementAt(i,j); } V->elementAt(i,j) = 1.0; if (VT) VT->elementAt(j,i) = 1.0; Y->elementAt(i,0) = output[i][n]; } // for i=[0..p) Matrix *VTVinv = NULL; Matrix *VTY = NULL; Matrix *VTV = NULL; VTV = VT->multiply(V); VTVinv = VTV->inverse(); delete VTV; VTV=NULL; VTY = VT->multiply(Y); W = VTVinv->multiply(VTY); delete VTVinv; VTVinv=NULL; delete VTY; VTY=NULL; //save the weights // string weightRoute=Weight+"weight.txt"; ofstream weightOut; weightOut.open(weightRoute.c_str(),ios::app); for (i=0;i elementAt(i,0)<<" "; } weightOut< >CenterArray[i][k]; } } ClusterIn.close(); ClusterIn.clear(); } void CRBF::readSigma() { int i; ifstream SigmaIn; string SigFname; SigFname=Sigma+"sg.txt"; SigmaIn.open(SigFname.c_str()); for(i=0;i >sigma[i]; if(sigma[i]==0) { sigma[i]=100.0; } } SigmaIn.close(); SigmaIn.clear(); } float CRBF::sigmoid(float x) //{ return (float)(1/(1+exp(-x))); } {return (float)(2.0/(1.0+exp(-1.*((float)(x))))-1.);} void CRBF::RbfTest() { int i,j,k; //read in centroid readCenter(); //read in sigma readSigma(); //read in the weights ifstream weightIn; string weightRoute=Weight+"weight.txt"; weightIn.open(weightRoute.c_str()); for(j=0; j >weight[j][k]; } } weightIn.close(); weightIn.clear(); //read in input pattern and recognize string TestFnameRoute=TestPath+"test.txt"; string TestFname; ifstream TestFeatnameIn; TestFeatnameIn.open(TestFnameRoute.c_str()); float temp; int seq; int correct,error; correct=0; error=0; for(i=0;i >TestFname; TestFeatnameIn>>seq; string TestFeatData=TestPath+TestFname; ifstream TestFeatdataIn; TestFeatdataIn.open(TestFeatData.c_str()); for(k=0;k >temp; Testinput[k]=temp; } for(j=0;j Testy[max]) // if(Testoutput[j]>Testoutput[max]) max=j; } if(max==seq) correct++; else error++; TestFeatdataIn.close(); TestFeatdataIn.clear(); } TestFeatnameIn.close(); cout<