1、time.h#define N 8 /*学习样本个数(测试样本个数)*/#define IN 3 /*输入层神经元数目*/#define HN 2 /*隐层神经元数目*/#define ON 1 /*输出层神经元数目*/float PIN; /*单个样本输入数据*/float TON; /*单个样本教师数据*/float WHNIN; /*输入层至隐层权值*/float VONHN; /*隐层至输出层权值*/float XHN; /*隐层的输入*/float YON; /*输出层的输入*/float HHN; /*隐层的输出*/float OON; /*输出层的输出*/float YU_HNH
2、N; /*隐层的阈值*/float YU_ONON; /*输出层的阈值*/float err_mN; /*第m个样本的总误差*/float a; /*输出层至隐层学习效率*/float b; /*隐层至输入层学习效率*/float alpha; /*/动量因子,改进型bp算法使用*/float d_errON;/*k*/float e_errHN;/*j*/FILE *fp;/*定义一个放学习样本的结构*/struct float inputIN;float teachON; Study_DataN;/*定义一个放测试样本的结构*/float expectON; Test_DataN;/*改进
3、型bp算法用来保存每次计算的权值*/float old_WHNIN;float old_VONHN;int Start_Show()clrscr();printf(n * n); * Welcome to use *n this program of calculating the BP *n model! Happy every day! *nnnBefore starting,please read the follows carefully:nn The program of BP can study itself for no more than 200000 times.nAnd s
4、urpassing the number,the program will be ended by itself innpreventing running infinitely because of error!nnnnNow press any key to start.ngetch();int End_Show()nn-nThe program has reached the end successfully!nnPress any key to exit! This is the end * of the program which*n * can calculate the BP*n
5、 Thanks for using!exit(0);/*读取训练样本*/GetTrainingData()int i,j,m;float datr;if(fp=fopen(sample.txt,r)=NULL) printf(Cannot open file strike any key exit! getch(); exit(1); for(i=0;iIN-1) Study_Datai.teachj-IN=datr; else Study_Datai.inputj=datr;/*printf(nthe Study_Data%d.input%d=%fn %f,i,j,Study_Datai.i
6、nputj,datr);*/*use to check the loaded training datas*/ j+;fclose(fp);nThere are %d sample datas that have been loaded successfully!,N*(IN+ON);nShow the data which has been loaded as follows:for(m=0;mm+) for(i=0;IN; printf(Study_Data%d.input%d=%f ,m,i,Study_Datam.inputi); for(j=0;jON;j+)Study_Data%d
7、.teach%d=%f,m,j,Study_Datam.teachj);nnnPress any key to begin Study.return 1;/*初始化权、阈值子程序*/initial()int i;int ii;int j;int jj;int k;int kk;nRandsom Weight value and Bias value as follow:srand(time(NULL);/*随机函数种子*/nWeight Value:HN;i+) j+) Wij=(float)(rand()/32767.0)*2-1)/2);/*初始化输入层到隐层的权值,随机模拟0.5-0.5
8、 */nw%d%d=%f,i,j,Wij);for(ii=0;iiii+) for(jj=0;jjjj+) Viijj= (float)(rand()/32767.0)*2-1)/2);/*初始化隐层到输出层的权值,随机模拟0.5-0.5 */nV%d%d=%f,ii,jj,Viijj);nnBias Value:for(k=0;kk+) YU_HNk = 1.0;/*隐层阈值初始化 ,-0.01 0.01 之间*/nYU_HN%d=%f,k,YU_HNk);for(kk=0;kkkk+) YU_ONkk = 1.0;/*输出层阈值初始化 ,-0.01 0.01 之间*/nYU_ON%d=%fn,kk,YU_ONkk);nnnnnPress any key to start culculating.:Please wait./*第m个学习样本输入子程序*/input_P(int m) int i,j; for(i=0;i+) Pi=Study_Datam.inputi; retu
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1