您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > c语言版bp神经网络
看到学生用Matlab做了BP神经网络应用的程序,突然想起自己在上学时写了个BP的C语言版,现在真不知道当时是怎么写出来的,这里放上来做一个纪念。#includestdio.h#includestdlib.h#includemath.h#includeconio.h#includectype.h#includestring.h#includetime.h#defineMAXWEIGHT((float)0.3)#defineSCALEWEIGHT((float)32767)#definenInputNodes4#definenHiddenNodes8#definenOutputNodes3#definenPatterns15#definenIterations5000#defineESC27#defineERRORLEVEL0.0001#defineITEMS8typedeffloat*PFLOAT;typedefPFLOATVECTOR;typedefPFLOAT*MATRIX;voidVectorAllocate(VECTOR*vector,intnCols);voidAllocateCols(PFLOATmatrix[],intnRows,intnCols);voidMatrixAllocate(MATRIX*pmatrix,intnRows,intnCols);voidMatrixFree(MATRIXmatrix,intnRows);MATRIXout0;MATRIXout1;MATRIXdelta1;MATRIXdelw1;MATRIXw1;MATRIXout2;MATRIXdelta2;MATRIXdelw2;MATRIXw2;MATRIXtarget;//VECTORPatternID;voidmain(){floateta=0.15,alpha=0.075;intnReportErrors=100;floatErrorLevel=ERRORLEVEL;charMonitorError=0;floaterror;registerinth;registerinti;registerintj;intp,q;//r;FILE*fpPattern,*fpWeightsOut,*fpResults,*fpError,*fpTest;MatrixAllocate(&out0,nPatterns,nInputNodes);MatrixAllocate(&out1,nPatterns,nHiddenNodes);MatrixAllocate(&out2,nPatterns,nOutputNodes);MatrixAllocate(&delta2,nPatterns,nOutputNodes);MatrixAllocate(&delw2,nOutputNodes,nHiddenNodes+1);MatrixAllocate(&w2,nOutputNodes,nHiddenNodes+1);MatrixAllocate(&delta1,nPatterns,nHiddenNodes);MatrixAllocate(&delw1,nHiddenNodes,nInputNodes+1);MatrixAllocate(&w1,nHiddenNodes,nInputNodes+1);MatrixAllocate(&target,nPatterns,nOutputNodes);//VectorAllocate(&PatternID,nPatterns);//初始化权值----------------------------------srand((unsigned)time(NULL));for(h=0;hnHiddenNodes;h++){for(i=0;i=nInputNodes;i++){floatfrand=rand();w1[h][i]=MAXWEIGHT*(1.0-2*frand/SCALEWEIGHT);delw1[h][i]=0.0;}}for(j=0;jnOutputNodes;j++)for(h=0;h=nHiddenNodes;h++){floatfrand=rand();w2[j][h]=MAXWEIGHT*(0.1-2*frand/SCALEWEIGHT);delw2[j][h]=0.0;}//输入训练样本-------------------------------if(fpPattern=fopen(input.txt,r))for(p=0;pnPatterns;p++)for(i=0;inInputNodes;i++)fscanf(fpPattern,%f,&out0[p][i]);for(p=0;pnPatterns;p++)for(i=0;inOutputNodes;i++)fscanf(fpPattern,%f,&target[p][i]);fpError=fopen(error.txt,w);//开始训练--------------------------------fprintf(stderr,nIterations1?Training...\n:Testing\n);for(q=0;qnIterations;q++){for(p=0;pnPatterns;p++){for(h=0;hnHiddenNodes;h++){floatsum=w1[h][nInputNodes];for(i=0;inInputNodes;i++)sum+=w1[h][i]*out0[p][i];out1[p][h]=1.0/(1.0+exp(-sum));}for(j=0;jnOutputNodes;j++){floatsum=w2[j][nHiddenNodes];for(h=0;hnHiddenNodes;h++)sum+=w2[j][h]*out1[p][h];out2[p][j]=1.0/(1.0+exp(-sum));}for(j=0;jnOutputNodes;j++)delta2[p][j]=(target[p][j]-out2[p][j])*out2[p][j]*(1.0-out2[p][j]);for(h=0;hnHiddenNodes;h++){floatsum=0.0;for(j=0;jnOutputNodes;j++)sum+=delta2[p][j]*w2[j][h];delta1[p][h]=sum*out1[p][h]*(1.0-out1[p][h]);}}///forpfor(j=0;jnOutputNodes;j++){floatdw;floatsum=0.0;for(p=0;pnPatterns;p++)sum+=delta2[p][j];dw=eta*sum+alpha*delw2[j][nHiddenNodes];w2[j][nHiddenNodes]+=dw;delw2[j][nHiddenNodes]=dw;for(h=0;hnHiddenNodes;h++){floatsum=0.0;for(p=0;pnPatterns;p++)sum+=delta2[p][j]*out1[p][h];dw=eta*sum+alpha*delw2[j][h];w2[j][h]+=dw;delw2[j][h]=dw;}}for(h=0;hnHiddenNodes;h++){floatdw;floatsum=0.0;for(p=0;pnPatterns;p++)sum+=delta1[p][h];dw=eta*sum+alpha*delw1[h][nInputNodes];w1[h][nInputNodes]+=dw;delw1[h][nInputNodes]=dw;for(i=0;inInputNodes;i++){floatsum=0.0;for(p=0;pnPatterns;p++)sum+=delta1[p][h]*out0[p][i];dw=eta*sum+alpha*delw1[h][i];w1[h][i]+=dw;delw1[h][i]=dw;}}if(kbhit()){intc=_getch();if((c=toupper(c))=='E')MonitorError++;elseif(c==ESC)break;}if(MonitorError||(q%nReportErrors==0)){for(error=0.0,p=0;pnPatterns;p++){for(j=0;jnOutputNodes;j++){floattemp=target[p][j]-out2[p][j];error+=temp*temp;}}error/=(nPatterns*nOutputNodes);fprintf(stderr,Iteration%5d/%-5dError%f\r,q,nIterations,error);MonitorError=0;if(q%nReportErrors==0)fprintf(fpError,%d%f\n,q,error);if(errorErrorLevel)break;}}//qfor(p=0,error=0.0;pnPatterns;p++){for(j=0;jnPatterns;j++){floattemp=target[p][j]-out2[p][j];error+=temp*temp;}}error/=(nPatterns*nOutputNodes);fprintf(stderr,Iteration%5d/%-5dError%f\n,q,nIterations,error);fprintf(fpError,\n%d%f\n,q,error);fclose(fpError);//输出权值-------------------------------if((fpWeightsOut=fopen(WeightsOut.txt,w))==NULL){fprintf(stderr,can'twritefile%s\n,WeightsOut.txt);exit(1);}for(h=0;hnHiddenNodes;h++)for(i=0;i=nInputNodes;i++)fprintf(fpWeightsOut,%g%c,w1[h][i],i%ITEMS==ITEMS-1?'\n':'');for(j=0;jnOutputNodes;j++)for(h=0;hnHiddenNodes;h++)fprintf(fpWeightsOut,%g%c,w2[j][h],j%ITEMS==ITEMS-1?'\n':'');fclose(fpWeightsOut);//输出训练结果----------------------------------------if((fpResults=fopen(Results.txt,w))==NULL){fprintf(stderr,can'twritefile%s\n,Results.txt);fpResults=stderr;}for(p=0;pnPatterns;p++){fprintf(fpResults,%d,p+1);for(j=0;jnOutputNodes;j++)fprintf(fpResults,%f,target[p][j]);fprintf(fpResults,--);for(j=0;jnOutputNodes;j++)fprintf(fpResults,%f,out2[p][j]);fprintf(fpResults,\n);}fclose(fpResults);//--测试数据--------------------------printf(Traininghascomplete!\n1.Testd
本文标题:c语言版bp神经网络
链接地址:https://www.777doc.com/doc-1414310 .html