您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > BP算法程序C语言实现
文件输入输出目录为:F:\BP\训练样本文件名:训练样本.txt值为:11-11-110101输出文件名为:阈值.txt权值.txt=========================#includestdlib.h#includemath.h#includeconio.h#includestdio.h#defineN2/*/学习样本个数*/#defineIN3/*/输入层神经元数目*/#defineHN3/*/隐层神经元数目*/#defineON2/*/输出层神经元数目*/#defineZ20/*旧权值保存,每次study的权值都保存下来*/doubleP[IN];/*单个样本输入数据*/doubleT[ON];/*单个样本输出数据*/doubleW[HN][IN];/*/输入层至隐层权值*/doubleV[ON][HN];/*/隐层至输出层权值*/doubleX[HN];/*/隐层的输入*/doubleY[ON];/*/输出层的输入*/doubleH[HN];/*/隐层的输出*/doubleO[ON];/*/输出层的输出*/doubleYU_HN[HN];/*/隐层的阈值*/doubleYU_ON[ON];/*/输出层的阈值*/doubleerrm[N];/*/第m个样本的总误差*/doublea;/*/输出层至隐层的学习效率*/doubleb;/*/隐层至输入层学习效率*/doublealpha;/*/动量因子,改进型bp算法使用*/doublederr[ON];FILE*fp;/*定义一个放学习样本的结构*/struct{doubleinput[IN];doubleteach[ON];}Study_Data[N];/*改进型bp算法用来保存每次计算的权值*/struct{doubleold_W[HN][IN];doubleold_V[ON][HN];}Old_WV[Z];显示开始界面intStart_Show(){clrscr();printf(\n***********************\n);printf(*Welcometouse*\n);printf(*thisprogramof*\n);printf(*calculatingtheBP*\n);printf(*model!*\n);printf(*Happyeveryday!*\n);printf(***********************\n);printf(\n\nBeforestarting,pleasereadthefollowscarefully:\n\n);printf(1.PleaseensurethePathofthe'训练样本.txt'(xunlianyangben.txt)is\ncorrect,like'F:\BP\训练样本.txt'!\n);printf(2.ThecalculatingresultswillbesavedinthePathof'F:\\BP\\'!\n);printf(3.Theprogramwillload10dataswhenrunningfrom'F:\\BP\\训练样本.txt'!\n);printf(4.TheprogramofBPcanstudyitselffornomorethan30000times.\nAndsurpassingthenumber,theprogramwillbeendedbyitselfin\npreventingrunninginfinitelybecauseoferror!\n);printf(\n\n\n);printf(Nowpressanykeytostart...\n);getch();getch();clrscr();}显示结束界面intEnd_Show(){printf(\n\n---------------------------------------------------\n);printf(Theprogramhasreachedtheendsuccessfully!\n\nPressanykeytoexit!\n\n);printf(\n***********************\n);printf(*Thisistheend*\n);printf(*oftheprogramwhich*\n);printf(*cancalculatetheBP*\n);printf(*model!*\n);printf(***********************\n);printf(*Thanksforusing!*\n);printf(*Happyeveryday!*\n);printf(***********************\n);getch();exit(0);}获取训练样本GetTrainingData()/*OK*/{intm,i,j;intdatr;if((fp=fopen(f:\\bp\\训练样本.txt,r))==NULL)/*读取训练样本*/{printf(Cannotopenfileandstrikeanykeyexit!);getch();exit(1);}m=0;i=0;j=0;while(fscanf(fp,%d,&datr)!=EOF){j++;if(j=(N*IN))/*N为学习样本个数;IN为输入层神经元数目*/{if(iIN){Study_Data[m].input[i]=datr;/*printf(\ntheStudy_Datat[%d].input[%d]=%f\n,m,i,Study_Data[m].input[i]);getch();*//*usetochecktheloadedtrainingdatas*/}if(m==(N-1)&&i==(IN-1)){m=0;i=-1;}if(i==(IN-1)){m++;i=-1;}}elseif((N*IN)J&&J=(N*(IN+ON))){if(iON){Study_Data[m].teach[i]=datr;/*printf(\nTheStudy_Data[%d].teach[%d]=%f,m,i,Study_Data[m].teach[i]);getch();*//*usetochecktheloadedtrainingdatas*/}if(m==(N-1)&&i==(ON-1))printf(\n);if(i==(ON-1)){m++;i=-1;}}i++;}fclose(fp);printf(\nThereare[%d]datatsthathavebeenloadedsuccessfully!\n,j);/*showthedatawhichhasbeenloaded!*/printf(\nShowthedatawhichhasbeenloadedasfollows:\n);for(m=0;mN;M++){for(i=0;iIN;I++){printf(\nStudy_Data[%d].input[%d]=%f,m,i,Study_Data[m].input[i]);}for(j=0;jON;J++){printf(\nStudy_Data[%d].teach[%d]=%f,m,j,Study_Data[m].teach[j]);}}printf(\n\nPressanykeytostartcalculating...);getch();return1;}/*///////////////////////////////////*//*初始化权、阈值子程序*//*///////////////////////////////////*/initial(){inti;intii;intj;intjj;intk;intkk;/*隐层权、阈值初始化*/for(i=0;iHN;i++){for(j=1;jIN;j++){W[i][j]=(double)((rand()/32767.0)*2-1);/*初始化输入层到隐层的权值,随机模拟0和1-1*/printf(w[%d][%d]=%f\n,i,j,W[i][j]);}}for(ii=0;iiON;II++){for(jj=0;jjHN;JJ++){V[ii][jj]=(double)((rand()/32767.0)*2-1);/*初始化隐层到输出层的权值,随机模拟0和1-1*/printf(V[%d][%d]=%f\n,ii,jj,V[ii][jj]);}}for(k=0;kHN;K++){YU_HN[k]=(double)((rand()/32767.0)*2-1);/*隐层阈值初始化,-0.01~0.01之间*/printf(YU_HN[%d]=%f\n,k,YU_HN[k]);}for(kk=0;kkON;KK++){YU_ON[kk]=(double)((rand()/32767.0)*2-1);/*输出层阈值初始化,-0.01~0.01之间*/}return1;}/*子程序initial()结束*//*//////////////////////////////////////////*//*第m个学习样本输入子程序*//*/////////////////////////////////////////*/input_P(intm){inti,j;for(i=0;iIN;I++){P[i]=Study_Data[m].input[i];printf(P[%d]=%f\n,i,P[i]);}/*获得第m个样本的数据*/return1;}/*子程序input_P(m)结束*//*/////////////////////////////////////////*//*第m个样本教师信号子程序*//*/////////////////////////////////////////*/input_T(intm){intk;for(k=0;kON;k++)T[k]=Study_Data[m].teach[k];return1;}/*子程序input_T(m)结束*/H_I_O(){doublesigma;inti,j;for(j=0;jHN;j++){sigma=0;for(i=0;iIN;i++){sigma+=W[j][i]*P[i];/*求隐层内积*/}X[j]=sigma-YU_HN[i];/*求隐层净输入,为什么减隐层的阀值*/H[j]=1.0/(1.0+exp(-X[j]));/*求隐层输出siglon算法*/}return1;}/*子程序H_I_O()结束*/O_I_O(){intk;intj;doublesigma;for(k=0;kON;k++){sigma=0.0;for(j=0;jHN;j++){sigma+=V[k][j]*H[k];}Y[k]=sigma-YU_ON[k];O[k]=1.0/(1.0+exp(-Y[k]));}return1;}intErr_O_H(intm){intk;doubleabs_err[ON];doublesqr_err=0;for(k=0;kON;k++){abs_err[k]=T[k]-O[k];sqr_err+=(abs_err[k])*(abs_err[k]);d_err[k]=abs_err[k]*O[k]*(1.0-O[k]);err_m[m]=sqr_err/2;}return1;}doublee_err[HN];intErr_H_I(){intj,k;doublesigma;for(j=0;jHN;j++){sigma=0.0;for(k=0;kON;k++){sigma+=d_err[k]*V[k][j];}e_err[j]=sigma*H[j]*(1-H[j]);}return1;}saveWV(intm){inti;intii;intj;intjj;for(i=0;i
本文标题:BP算法程序C语言实现
链接地址:https://www.777doc.com/doc-2901010 .html