BP神经网络C++源码及训练测试数据

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#ifndef_BP_#define_BP_#includeiostream#includecmath#includecstdlib#includestring#includectimeusingnamespacestd;classBP{private:intinNodeCount;//输入层节点数intmidNodeCount;//隐藏层节点数intoutNodeCount;//输出层节点数double**midWeight;//隐藏层权值double**outWeight;//输出层权值double**detaMidW;//隐藏层权值增量double**detaOutW;//输出层权值增量double*midThreshold;//隐藏层阈值double*outThreshold;//输出层阈值doubleerror;//精度doublestudyRate;//学习效率doublealpha;//动量doublesigmoid(doublex);//传递函数:双曲正切函数doublediffSigmoid(doublex);//传递函数导数doubleturn(double*in,double*out);public:BP(intinc,intmnc,intonc);voidnormalize(double**data,intr,intc);voidtrain(double**in,double**out,intsize);voidtest(double**in,double**out,intsize);};BP::BP(intinc,intmnc,intonc){inNodeCount=inc;midNodeCount=mnc;outNodeCount=onc;studyRate=0.5;error=0.0;alpha=0.5;midWeight=newdouble*[midNodeCount];//申请隐藏层权值存储空间detaMidW=newdouble*[midNodeCount];//申请隐藏层权值增量存储空间for(inti=0;imidNodeCount;i++){midWeight[i]=newdouble[inNodeCount];detaMidW[i]=newdouble[inNodeCount];}outWeight=newdouble*[outNodeCount];//申请输出层权值存储空间detaOutW=newdouble*[outNodeCount];//申请输出层权值增量存储空间for(inti=0;ioutNodeCount;i++){outWeight[i]=newdouble[midNodeCount];detaOutW[i]=newdouble[midNodeCount];}midThreshold=newdouble[midNodeCount];//申请隐藏层阈值存储空间outThreshold=newdouble[outNodeCount];//申请输出层阈值存储空间srand(time(NULL));for(inti=0;imidNodeCount;i++){midThreshold[i]=1;//初始化隐藏层阈值for(intj=0;jinNodeCount;j++){midWeight[i][j]=2*rand()/RAND_MAX-1;//初始化隐藏层权值detaMidW[i][j]=0.0;}}for(inti=0;ioutNodeCount;i++){outThreshold[i]=1;//初始化输出层阈值for(intj=0;jmidNodeCount;j++){outWeight[i][j]=2*rand()/RAND_MAX;//初始化输出层权值detaOutW[i][j]=0.0;}}}voidBP::normalize(double**data,intr,intc)//数据规整化:-a~a{doublemax;doublemin;doubleb;doublek;doublea=1;for(inti=0;ic;i++){max=data[0][i];min=data[0][i];for(intj=0;jr;j++){if(data[j][i]min)min=data[j][i];if(data[j][i]max)max=data[j][i];}for(intj=0;jr;j++){k=2.0*a/(max-min);b=-a*(max+min)/(max-min);data[j][i]=data[j][i]*k+b;}}}doubleBP::sigmoid(doublex)//f(x)=a*tanh(b*x){return1.7159*tanh(2.0/3.0*x);}doubleBP::diffSigmoid(doublex){doublea=1.7159;doubleb=2.0/3.0;return-a*b*(pow(tanh(b*x),2)-1);}doubleBP::turn(double*in,double*out)//in:输入数据,out:期望输出数据,返回值:单次训练误差{double*inMid=newdouble[midNodeCount];//申请隐藏层神经元输入存储空间double*inOut=newdouble[outNodeCount];//申请输出层神经元输入存储空间double*Omid=newdouble[midNodeCount];//申请隐藏层输出空间double*Oout=newdouble[outNodeCount];//申请输出层输出空间double*detaMid=newdouble[midNodeCount];//申请隐藏层梯度存储空间double*detaOut=newdouble[outNodeCount];//申请输出层梯度存储空间doublee=0.0;for(inti=0;imidNodeCount;i++)//计算隐藏层输出值{inMid[i]=0.0;for(intj=0;jinNodeCount;j++)inMid[i]+=midWeight[i][j]*in[j];Omid[i]=sigmoid(inMid[i]-midThreshold[i]);}for(inti=0;ioutNodeCount;i++)//计算输出层输出值{inOut[i]=0.0;for(intj=0;jmidNodeCount;j++)inOut[i]+=outWeight[i][j]*Omid[j];Oout[i]=sigmoid(inOut[i]-midThreshold[i]);}for(inti=0;ioutNodeCount;i++){detaOut[i]=(out[i]-Oout[i])*diffSigmoid(inOut[i]);//计算输出层梯度}for(inti=0;ioutNodeCount;i++){for(intj=0;jmidNodeCount;j++){detaOutW[i][j]=alpha*detaOutW[i][j]+studyRate*detaOut[i]*Omid[j];//计算输出层权值增量outWeight[i][j]+=detaOutW[i][j];//更新输出层权值}}for(inti=0;imidNodeCount;i++)//计算隐藏层梯度{detaMid[i]=0.0;for(intj=0;joutNodeCount;j++)detaMid[i]+=detaOut[j]*outWeight[j][i];detaMid[i]*=diffSigmoid(in[i]);}for(inti=0;imidNodeCount;i++)//更新隐藏层权值{for(intj=0;jinNodeCount;j++){detaMidW[i][j]=detaMidW[i][j]*alpha+studyRate*detaMid[i]*in[j];midWeight[i][j]+=detaMidW[i][j];}}for(inti=0;ioutNodeCount;i++){e+=pow(out[i]-Oout[i],2);}e=0.5*e;delete[]inMid;//释放空间delete[]inOut;delete[]Omid;delete[]Oout;delete[]detaMid;delete[]detaOut;returne;}voidBP::train(double**in,double**out,intsize){inttimes=0;intMAX_TIMES=200;//最大训练次数error=0.0;double*dataIn;double*dataOut;while(true){for(inti=0;isize;i++){dataIn=in[i];dataOut=out[i];error+=turn(dataIn,dataOut);}error*=1.0/(double)size;//样本集均方误差coutTimes:++timesError:errorendl;if(timesMAX_TIMES){break;}else{error=0.0;}}coutCompletetrain!endl;}voidBP::test(double**in,double**out,intsize){double*inMid=newdouble[midNodeCount];//申请隐藏层神经元输入存储空间double*inOut=newdouble[outNodeCount];//申请输出层神经元输入存储空间double*Omid=newdouble[midNodeCount];//申请隐藏层输出空间for(inti=0;isize;i++){for(intj=0;jmidNodeCount;j++)//计算隐藏层输出值{inMid[j]=0.0;for(intk=0;kinNodeCount;k++)inMid[j]+=midWeight[j][k]*in[i][k];Omid[j]=sigmoid(inMid[j]-midThreshold[j]);}for(intj=0;joutNodeCount;j++)//计算输出层输出值{inOut[j]=0.0;for(intk=0;kmidNodeCount;k++)inOut[j]+=outWeight[j][k]*Omid[k];out[i][j]=sigmoid(inOut[j]-outThreshold[j]);}}delete[]inMid;delete[]inOut;delete[]Omid;}#endif#includeBP.h#includeiomanip#includefstreamusingnamespacestd;intmain(){intsize=85;//训练集样本数intts=15;//测试集样本数intinNode=4;intmidNode=10;intoutNode=1;intindexA;intindexB;ifstreamft(temp.txt);ifstreamfs(test.txt);BPbp=BP(inNode,midNode,outNode);double**srcIn=newdouble*[size];double**srcOut=newdouble*[size];double**result=newdouble*[size];doubletemp;for(inti=0;isize;i++){srcIn[i]=newdouble[inNode];srcOut[i]=newdouble[outNode];result[i]=newdouble[outNode];for

1 / 12
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功