C++实现的BP神经网络(代码)

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

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

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

资源描述

C++实现的BP神经网络(代码)来源:天新网-开发中心#pragmahdrstop#includestdio.h#includeiostream.hconstA=30.0;constB=10.0;constMAX=500;//最大训练次数constCOEF=0.0035;//网络的学习效率constBCOEF=0.001;//网络的阀值调整效率constERROR=0.002;//网络训练中的允许误差constACCURACY=0.0005;//网络要求精度doublesample[41][4]={{0,0,0,0},{5,1,4,19.020},{5,3,3,14.150},{5,5,2,14.360},{5,3,3,14.150},{5,3,2,15.390},{5,3,2,15.390},{5,5,1,19.680},{5,1,2,21.060},{5,3,3,14.150},{5,5,4,12.680},{5,5,2,14.360},{5,1,3,19.610},{5,3,4,13.650},{5,5,5,12.430},{5,1,4,19.020},{5,1,4,19.020},{5,3,5,13.390},{5,5,4,12.680},{5,1,3,19.610},{5,3,2,15.390},{1,3,1,11.110},{1,5,2,6.521},{1,1,3,10.190},{1,3,4,6.043},{1,5,5,5.242},{1,5,3,5.724},{1,1,4,9.766},{1,3,5,5.870},{1,5,4,5.406},{1,1,3,10.190},{1,1,5,9.545},{1,3,4,6.043},{1,5,3,5.724},{1,1,2,11.250},{1,3,1,11.110},{1,3,3,6.380},{1,5,2,6.521},{1,1,1,16.000},{1,3,2,7.219},{1,5,3,5.724}};doublew[4][10][10],wc[4][10][10],b[4][10],bc[4][10];doubleo[4][10],netin[4][10],d[4][10],differ;//单个样本的误差doubleis;//全体样本均方差intcount,a;voidnetout(intm,intn);//计算网络隐含层和输出层的输出voidcalculd(intm,intn);//计算网络的反向传播误差voidcalcalwc(intm,intn);//计算网络权值的调整量voidcalcaulbc(intm,intn);//计算网络阀值的调整量voidchangew(intm,intn);//调整网络权值voidchangeb(intm,intn);//调整网络阀值voidclearwc(intm,intn);//清除网络权值变化量wcvoidclearbc(intm,intn);//清除网络阀值变化量bcvoidinitialw(void);//初始化NN网络权值Wvoidinitialb(void);//初始化NN网络阀值voidcalculdiffer(void);//计算NN网络单个样本误差voidcalculis(void);//计算NN网络全体样本误差voidtrainNN(void);//训练NN网络/*计算NN网络隐含层和输出层的输出*/voidnetout(intm,intn){inti,j,k;//隐含层各节点的的输出for(j=1,i=2;j=m;j++)//m为隐含层节点个数{netin[i][j]=0.0;for(k=1;k=3;k++)//隐含层的每个节点均有三个输入变量netin[i][j]=netin[i][j]+o[i-1][k]*w[i][k][j];netin[i][j]=netin[i][j]-b[i][j];o[i][j]=A/(1+exp(-netin[i][j]/B));}//输出层各节点的输出for(j=1,i=3;j=n;j++){netin[i][j]=0.0;for(k=1;k=m;k++)netin[i][j]=netin[i][j]+o[i-1][k]*w[i][k][j];netin[i][j]=netin[i][j]-b[i][j];o[i][j]=A/(1+exp(-netin[i][j]/B));}}/*计算NN网络的反向传播误差*/voidcalculd(intm,intn){inti,j,k;doublet;a=count-1;d[3][1]=(o[3][1]-sample[a][3])*(A/B)*exp(-netin[3][1]/B)/pow(1+exp(-netin[3][1]/B),2);//隐含层的误差for(j=1,i=2;j=m;j++){t=0.00;for(k=1;k=n;k++)t=t+w[i+1][j][k]*d[i+1][k];d[i][j]=t*(A/B)*exp(-netin[i][j]/B)/pow(1+exp(-netin[i][j]/B),2);}}/*计算网络权值W的调整量*/voidcalculwc(intm,intn){inti,j,k;//输出层(第三层)与隐含层(第二层)之间的连接权值的调整for(i=1,k=3;i=m;i++){for(j=1;j=n;j++){wc[k][i][j]=-COEF*d[k][j]*o[k-1][i]+0.5*wc[k][i][j];}//printf(\n);}//隐含层与输入层之间的连接权值的调整for(i=1,k=2;i=m;i++){for(j=1;j=m;j++){wc[k][i][j]=-COEF*d[k][j]*o[k-1][i]+0.5*wc[k][i][j];}//printf(\n);}}/*计算网络阀值的调整量*/voidcalculbc(intm,intn){intj;for(j=1;j=m;j++){bc[2][j]=BCOEF*d[2][j];}for(j=1;j=n;j++){bc[3][j]=BCOEF*d[3][j];}}/*调整网络权值*/voidchangw(intm,intn){inti,j;for(i=1;i=3;i++)for(j=1;j=m;j++){w[2][i][j]=0.9*w[2][i][j]+wc[2][i][j];//为了保证系统有较好的鲁棒性,计算权值时乘惯性系数0.9printf(w[2][%d][%d]=%f\n,i,j,w[2][i][j]);}for(i=1;i=m;i++)for(j=1;j=n;j++){w[3][i][j]=0.9*w[3][i][j]+wc[3][i][j];printf(w[3][%d][%d]=%f\n,i,j,w[3][i][j]);}}/*调整网络阀值*/voidchangb(intm,intn){intj;for(j=1;j=m;j++)b[2][j]=b[2][j]+bc[2][j];for(j=1;j=n;j++)b[3][j]=b[3][j]+bc[3][j];}/*清除网络权值变化量wc*/voidclearwc(void){for(inti=0;i4;i++)for(intj=0;j10;j++)for(intk=0;k10;k++)wc[i][j][k]=0.00;}/*清除网络阀值变化量*/voidclearbc(void){for(inti=0;i4;i++)for(intj=0;j10;j++)bc[i][j]=0.00;}/*初始化网络权值W*/voidinitialw(void){inti,j,k,x;doubleweight;for(i=0;i4;i++)for(j=0;j10;j++)for(k=0;k10;k++){randomize();x=100+random(400);weight=(double)x/5000.00;w[i][j][k]=weight;}}/*初始化网络阀值*/voidinitialb(void){inti,j,x;doublefazhi;for(i=0;i4;i++)for(j=0;j10;j++){randomize();for(intk=0;k12;k++){x=100+random(400);}fazhi=(double)x/50000.00;b[i][j]=fazhi;}}/*计算网络单个样本误差*/voidcalculdiffer(void){a=count-1;differ=0.5*(o[3][1]-sample[a][3])*(o[3][1]-sample[a][3]);}voidcalculis(void){inti;is=0.0;for(i=0;i=19;i++){o[1][1]=sample[i][0];o[1][2]=sample[i][1];o[1][3]=sample[i][2];netout(8,1);is=is+(o[3][1]-sample[i][3])*(o[3][1]-sample[i][3]);}is=is/20;}/*训练网络*/voidtrainNN(void){longinttime;inti,x[4];initialw();initialb();for(time=1;time=MAX;time++){count=0;while(count=40){o[1][1]=sample[count][0];o[1][2]=sample[count][1];o[1][3]=sample[count][2];count=count+1;clearwc();clearbc();netout(8,1);calculdiffer();while(differERROR){calculd(8,1);calculwc(8,1);calculbc(8,1);changw(8,1);changb(8,1);netout(8,1);calculdiffer();}}printf(Thisis%dtimestrainingNN...\n,time);calculis();printf(is==%f\n,is);if(isACCURACY)break;}}//---------------------------------------------------------------------------#pragmaargsusedintmain(intargc,char*argv[]){doubleresult;intm,test[4];charch='y';coutPleasewaitforthetrainofNN:endl;trainNN();coutNow,thismodularnetworkcanworkforyou.endl;while(ch=='y'||ch=='Y'){coutPleaseinputdatatobetested.endl;for(m=1;m=3;m++)cintest[m];ch=getchar();o[1][1]=test[1];o[1][2]=test[2];o[1][3]=test[3];netout(8,1);result=o[3][1];printf(Finalresultis%f.\n,result);printf(Stilltest?[Yes]or[No]\n);ch=getchar();}return0;}

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

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

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

×
保存成功