




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、神经网络BP算法(C程序) 文件输入输出目录为:F:BP训练样本文件名:训练样本。txt值为:1 1 1 1 -1 1 0 1 0 1输出文件名为:阈值。txt 权值.txtinclude ”stdlib。h"include ”math.h"#include "conio。h”#include ”stdio。h"define N 2 /学习样本个数*/#define IN 3 /输入层神经元数目*/define HN 3 /*/隐层神经元数目*/#define ON 2 /*/输出层神经元数目/#define Z 20
2、 /*旧权值保存,每次study的权值都保存下来*/double PIN; /单个样本输入数据/double TON; /单个样本输出数据*/double WHNIN; /*/输入层至隐层权值/double VONHN; /隐层至输出层权值*/double XHN; /隐层的输入*/double YON; /输出层的输入/double HHN; /*/隐层的输出/double OON; /*/输出层的输出*/double YU_HNHN; /隐层的阈值/double YU_ONON; /*/输出层的阈值*/double err mN; /*/第m个样本的总误差*/double a; /输出层至
3、隐层的学习效率/double b; /*/隐层至输入层学习效率*/double alpha; /动量因子,改进型bp算法使用*/double d errON;文档为个人收集整理,来源于网络文档为个人收集整理,来源于网络FILE fp;/*定义一个放学习样本的结构*/struct double inputIN;double teachON;Study_DataN;/改进型bp算法用来保存每次计算的权值*/struct double old_WHNIN;double old_VONHN;Old_WVZ;显示开始界面int Start_Show()clrscr();printf(&quo
4、t;n *n");printf(" * &
5、#160; Welcome to use *n");printf(" this program of n”);printf(”
6、0; calculating the BP *n”);printf(” model!
7、0; n");printf(” * Happy every day! n");printf(”
8、60; *n”);printf("nn Before starting, please read the follows carefully:nn”);printf(" 1。Please ensure the Path of the 训练样本。txt(xunlianyangben。txt) is n correct, like 'F:BP训练样本.txt!n");printf(&
9、quot; 2.The calculating results will be saved in the Path of F:BP'!n");printf(" 3.The program will load 10 datas when running from 'F:BP训练样本。txt!n");printf(" 4。The program of BP can study itself for no more than 30000
10、times。n And surpassing the number, the program will be ended by itself inn preventing running infinitely because of error!n");printf(”nnn”);printf(”Now press any key to start。n”);getch();getch();clrscr();文档为个人收集整理,来源于网络文档为个人收集整理,来源于网络显示结束界面int End_Show()printf("nn-n”);printf("The prog
11、ram has reached the end successfully!nn Press any key to exit!nn");printf(”n *n");printf("
12、60; This is the end *n”);printf(” of the program which*n”);printf("
13、; can calculate the BPn”);printf(” &
14、#160; model! n");printf(" *n");printf(”
15、 Thanks for using! n”);printf(" * Happy every day!
16、0; *n”);printf(" *n”);getch();exit(0);文档为个人收集整理,来源于网络个人收集整理,勿做商业用途获取训练样本GetTrainingData() /OK/ int m,i,j; int datr;if(fp=fopen(
17、"f:bp训练样本.txt”,”r”))=NULL) /*读取训练样本*/ printf(”Cannot open file and strike any key exit!”); getch(); exit(1); m=0;i=0;j=0;while(fscanf(fp,"%d”,&datr)!=EOF) j+; if(j=(NIN)) /N为学习样本个数;IN为输入层神经元数目/
18、160; if(iIN) Study_Datam。inputi=datr; /printf(”nthe Study_Datat%d.inputd=%fn”,m,i,Study_Datam.inputi);getch();/ /use to check the loaded training datas*/
19、if(m=(N1)&&i=(IN1) m=0; i=-1; if(i=(IN1) m+;
20、; i=1; else if(NIN)<J&&J=(N(IN+ON)) if(iON) Study_Datam.teachi=datr; /printf(”nThe Study_Data%d。teach%d=%f”,m,i,Study_Datam.teachi);getch();*/&
21、#160; /*use to check the loaded training datas/ if(m=(N1)i=(ON-1)) printf(”n”);个人收集整理,勿做商业用途本文为互联网收集,请勿用作商业用途 if(i=(ON-1) m+;
22、 i=1; i+; fclose(fp);printf("nThere are d datats that have been loaded successfully!n",j);/*show the data which has been loaded!*/printf(”nShow the data which has been loaded as follows:n");for(m=0;mN;M+) for(i=0;
23、iIN;I+) printf(”nStudy_Datad。inputd=%f",m,i,Study_Datam。inputi); for(j=0;jON;J+) printf("nStudy_Data%d.teach%d=f",m,j,Study_Datam。teachj); printf("nnPress any key to start calculating。.");getch(); return 1;/初始
24、化权、阈值子程序/*/initial()int i; int ii; int j; int jj; int k; int kk;/隐层权、阈值初始化/ for(i=0;iHN;i+) for(j=1;jIN;j+) Wij=(double)((rand()/32767。0)*2-1); /*初始化输入层到隐层的权值,随机模拟0 和 1 -1 / printf(”wdd=fn”,i,j,Wij); for
25、(ii=0;ii<ON;II+) for(jj=0;jj<HN;JJ+) Viijj= (double)(rand()/32767。0)*2-1); /初始化隐层到输出层的权值,随机模拟0 和 1 1*/ printf("Vd%d=%fn",ii,jj,Viijj); for(k=0;k<HN;K+) YU_HNk = (double)((rand()/32767.0)*21); /隐层阈
26、值初始化 ,0。01 0.01 之间*/ printf(”YU_HN%d=fn”,k,YU_HNk); for(kk=0;kk<ON;KK+) YU_ONkk = (double)((rand()/32767。0)21); /输出层阈值初始化 ,-0。01 0。01 之间*/ return 1;/*子程序initial()结束/文档为个人收集整理,来源于网络本文为互联网收集,请勿用作商业用途/*/*/*第m个学习样本输入子程序*/*/input_P(int m) int i,j; for(i
27、=0;i<IN;I+) Pi=Study_Datam.inputi; printf(”Pd=%fn",i,Pi); /*获得第m个样本的数据*/return 1;/子程序input_P(m)结束*/*/*第m个样本教师信号子程序/*/input_T(int m)int k; for(k=0;kON;k+) Tk=Study_Datam.teachk;return 1;/*子程序input_T(m)结束*/H_I_O() double sigma; int i,j; for(j=0
28、;j<HN;j+) sigma=0; for(i=0;i<IN;i+) sigma+=Wji*Pi;/*求隐层内积/ Xj=sigma-YU_HNi;/*求隐层净输入,为什么减隐层的阀值*/ Hj=1.0/(1。0+exp(-Xj));/*求隐层输出 siglon算法*/ return 1;/*子程序H_I_O()结束/O_I_O()int k; int j;
29、0;double sigma; for(k=0;k<ON;k+) sigma=0。0; for(j=0;j<HN;j+) sigma+=VkjHk; Yk=sigmaYU_ONk; Ok=1.0/(1。0+exp(-Yk); return 1;int Err_O_H(int m)int k;double abs_errON;double sqr_err=0;for (k=0;k<ON;k+) abs_errk=Tk-Ok;&
30、#160; sqr_err+=(abs_errk)(abs_errk); d_errk=abs_errkOk*(1。0Ok); err_mm=sqr_err/2; return 1;double e_errHN;int Err_H_I() int j,k; double sigma; for(j=0;jHN;j+) sigma=0。0; for(k=0;kON;k+) sigma+=d_errk*Vkj; e_errj=
31、sigma*Hj*(1-Hj); return 1;saveWV(int m)int i; int ii; int j; int jj; for(i=0;iHN;i+) for(j=0;jIN;j+) Old_WVm.old_Wij = Wij; for(ii=0;iiON;ii+) for(jj=0;jjHN;jj+)
32、160; Old_WVm。old_Viijj = Viijj; return 1;int Delta_O_H(int n) /*(int m,int n)*/int k,j; if(n<1) /n<=1/ for
33、 (k=0;k<ON;k+) for (j=0;jHN;j+) Vkj=Vkj+a*d_errkHj; YU_ONk+=ad_errk; else if(n>1)
34、0; for (k=0;kON;k+) for (j=0;j<HN;j+) Vkj=Vkj+ad_errkHj+alpha(Vkj-Old_WV(n-1)。old_Vkj); YU_ONk+=a*d_errk; r
35、eturn 1;Delta_H_I(int n) /*(int m,int n)/ int i,j;if(n=1) /*n=1/ for (j=0;j<HN;j+) for (i=0;i<IN;i+)
36、 Wji=Wji+be_errj*Pi; YU_HNj+=be_errj; else if(n>1) for(j=0;j<HN;j+) for(i=0;i<IN;i+) Wji=Wji+b*e_errj*Pi+alpha*(WjiOld_WV(n-1).old_W
37、ji); YU_HNj+=b*e_errj; return 1;double Err_Sum()int m;double total_err=0;for(m=0;mN;m+) total_err+=err_mm; return total_err;void savequan() int i,j,k; int ii,jj,kk;if((fp=fopen(”f:bp权值.txt”,”a”))=NULL)
38、60; /save the result at f:hszbpc。txt/ printf(”Cannot open file strike any key exit!”); getch(); exit(1); fprintf(fp,"Save the result of “权值”(quanzhi) as follows:n");for(i=0;iHN;i+) for(j=0;jIN;j+) fprintf(fp,"
39、;W%dd=fn”,i,j,Wij); fprintf(fp,”n”);for(ii=0;iiON;ii+) for(jj=0;jj<HN;jj+) fprintf(fp,"V%dd=%fn",ii,jj,Viijj); fclose(fp);printf(”nThe result of “权值。txt”(quanzhi) has been saved successfully!nPress any key to continue.。.”);getch();if((fp=fopen(”f:bp阈值.txt&qu
40、ot;,"a"))=NULL) /save the result at f:hszbpc/ printf("Cannot open file strike any key exit!”); getch(); exit(1); fprintf(fp,”Save the result of “输出层的阈值”(huozhi) as follows:n"); for(k=0;k<ON;K+)
41、160; fprintf(fp,"YU_ONd=fn",k,YU_ONk);fprintf(fp,"nSave the result of “隐层的阈值为”(huozhi) as follows:n”); for(kk=0;kkHN;KK+) fprintf(fp,"YU_HN%d=fn",kk,YU_HNkk);fclose(fp);printf("nThe result of “阈值.txt”(huozhi) has been saved successfully!nPress any key t
42、o continue.。”);getch ();/*/*程序入口,即主程序*/*/void main()double Pre_error;double sum_err;int study;int flag;flag=30000;a=0。7;b=0.7;alpha=0.9;study=0;Pre_error=0。0001;/实际值为Pre_error=0.0001;*/Start_Show(); /调用函数,显示开始界面*/GetTrainingData();initial ();do int m; +study; for(m=0;mN;m+)
43、60; input_P(m); input_T(m); H_I_O(); O_I_O(); Err_O_H(m); Err_H_I(); saveWV(m); /*/ Delta_O_H(m);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 培养高效领导者数字化时代的领导力教育
- 基因治疗药物临床试验数据安全及保密合作协议
- 线上商品展示与品牌授权服务合同
- 复杂决策中的风险管理考量试题及答案
- 迭代开发中的用户反馈收集技巧试题及答案
- 精确定位计算机二级VB考试试题及答案要点
- 法律适用中的争议分析试题及答案
- 2025年软考网络管理员常见误区与试题答案
- 新型绿色植物品种联合研发协议
- 肿瘤治疗设备研发项目合伙人协议
- 23G409先张法预应力混凝土管桩
- (完整word版)冰柜投放协议(免投版)
- 妇幼保健学(安徽医科大学)电子教案xl
- 部编版语文二年级下册教案及教学反思(全册)
- [安徽]高速公路改扩建工程交通组织方案(155页)
- 父权制度下埃德娜的精神觉醒-精品文档资料
- 张齐华:《平均数》课件
- 用药情况记录表正式版
- 《铁路线路里程断链设置和管理规定》
- 21世纪音乐教育发展趋势——问题与对策2004年音乐教育国际学术会议在上海音乐学院召开
- 提高钢管桩接头焊接质量图文
评论
0/150
提交评论