《2022年改进的BP神经网络算法》由会员分享,可在线阅读,更多相关《2022年改进的BP神经网络算法(8页珍藏版)》请在金锄头文库上搜索。
1、#include stdio.h #include stdlib.h #include time.h #include math.h /* inpoints 为输入神经元个数,可改变outpoints 为输出神经元个数defaultpoints 为隐层神经元个数datagrough 为样本数据个数* *以下数据定义可以修改*/ #define A 0 #define a 1 #define b 1 #define c 1 #define ALFA 0.85 #define BETA 0.2 /学习率 01 #define Total 20000 #define inpoints 9 #defi
2、ne outpoints 5 #define defaultpoints 28 #define datagrough 44 #define forecastdata 4 /*定义所需变量*/ double InpointDatadatagroughinpoints,OutpointDatadatagroughoutpoints; /* 输入输出数据*/ double InpointData_MAXinpoints,InpointData_MINinpoints; /* 每个因素最大数据*/ double OutpointData_MAXoutpoints,OutpointData_MINout
3、points; /* 每个因素最小数据*/ double wdefaultpointsinpoints,limendefaultpoints,voutpointsdefaultpoints; /* 连接权值、阈值*/ double dlta_wdefaultpointsinpoints,dlta_limendefaultpoints,dlta_voutpointsdefaultpoints; /* 连接权、阈值修正值*/ double defaultOutpointdefaultpoints,Outpoint_dpoutpoints,Outpoint_epdatagrough; /*读数据文件
4、 */ void ReadData() FILE *fp1,*fp2; int i,j; 训练输入 .txt,r)=NULL) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 8 页 - - - - - - - - - printf(1can not open the filen); exit(0); for(i=0;idatagrough;i+) for(j=0;jinpoints;j+) fscanf(fp1,%lf,&InpointDataij); fclose(f
5、p1); 训练输出 .txt,r)=NULL) printf(2can not open the filen); exit(0); for(i=0;idatagrough;i+) for(j=0;joutpoints;j+) fscanf(fp2,%lf,&OutpointDataij); fclose(fp2); /*/ /*归一化*/ void unitary() int i,j; int k=0; for(j=0;jinpoints;j+) /找出每列的最大、最小值存放在数组InpointData_MAXj、InpointData_MINj中 InpointData_MAXj=Inpoi
6、ntData0j; InpointData_MINj=InpointData0j; for(i=0;idatagrough;i+) if(InpointData_MAXjInpointDataij) InpointData_MINj=InpointDataij; for(j=0;joutpoints;j+) /找出每列的最大、最小值存放在数组OutpointData_MAXj、OutpointData_MINj中 OutpointData_MAXj=OutpointData0j; OutpointData_MINj=OutpointData0j; for(i=0;idatagrough;i+)
7、 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 8 页 - - - - - - - - - if(OutpointData_MAXjOutpointDataij) OutpointData_MINj=OutpointDataij; /*将 数 据 归 一 处 理 , 处 理 之 后 的 数 据 全 部 在 0 , 1 之 间*/ for(j=0;jinpoints;j+) for(i=0;idatagrough;i+) if(InpointData_MAXj=0) In
8、pointDataij=0; else InpointDataij=(InpointDataij-InpointData_MINj+A)/(InpointData_MAXj-InpointData_MINj+A); for(j=0;joutpoints;j+) for(i=0;idatagrough;i+) if(OutpointData_MAXj=0) OutpointDataij=0; else OutpointDataij=(OutpointDataij-OutpointData_MINj+A)/(OutpointData_MAXj-OutpointData_MINj+A); /*/ /
9、*初始化,随机赋初值*/ void Initialization() int i,j; srand(unsigned)time(NULL); /头文件名#include for(i=0;idefaultpoints;i+) /给输入层到隐层的连接权赋随机值LianJie_wij, 这些值在 0,1 for(j=0;jinpoints;j+) wij=(rand()*2.0/RAND_MAX-1)/2; dlta_wij=0; for(i=0;idefaultpoints;i+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名
10、师精心整理 - - - - - - - 第 3 页,共 8 页 - - - - - - - - - limeni=(rand()*2.0/RAND_MAX-1)/2; dlta_limeni=0; for(i=0;ioutpoints;i+) /给隐层到输出层的连接权赋初值for(j=0;jdefaultpoints;j+) vij=(rand()*2.0/RAND_MAX-1)/2; dlta_vij=0; /*求单样本的计算输出误差*/ void out_sub1(int t) int i,j; double defaultInpointdefaultpoints; double Outp
11、oint_youtpoints; Outpoint_ept=0; for(i=0;idefaultpoints;i+) double sum=0; for(j=0;jinpoints;j+) sum+=wij*InpointDatatj; defaultInpointi=sum+limeni; defaultOutpointi=1/(a+b*exp(-1*c*defaultInpointi);/求 Oi for(j=0;joutpoints;j+)/求 Yi Outpoint_yj=0; for(i=0;idefaultpoints;i+) Outpoint_yj+=vji*defaultOu
12、tpointi; Outpoint_dpj=OutpointDatatj-Outpoint_yj; Outpoint_ept+=Outpoint_dpj*Outpoint_dpj/2; /*反算权值*/ void out_sub2(int t) int i,j,k; double s; for(i=0;idefaultpoints;i+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 8 页 - - - - - - - - - s=0; for(j=0;joutpoin
13、ts;j+) dlta_vji=ALFA*dlta_vji+BETA*Outpoint_dpj*defaultOutpointi; / s+=vji*Outpoint_dpj; vji+=dlta_vji; dlta_limeni=ALFA*dlta_limeni+BETA*defaultOutpointi*(1-defaultOutpointi)*s;/ limeni+=dlta_limeni; for(k=0;kinpoints;k+) dlta_wik=ALFA*dlta_wik+BETA*defaultOutpointi*(1-defaultOutpointi)*s*InpointDa
14、tatk;/ wik=wik+dlta_wik; /*/ void forecast() int i,j,t,k=0; double e,e1forecastdata=0; /训练误差double sss; double InputData_xforecastdatainpoints,tpforecastdataoutpoints; double defInpoint,defOutpointdefaultpoints,yforecastdataoutpoints;/yforecastdataoutpoints为 网络检验输出FILE *fp1,*fp3; 预测输入 .txt,r)=NULL)
15、/检验数据输入 printf(3can not open the filen); exit(0); for(i=0;iforecastdata;i+) for(j=0;jinpoints;j+) fscanf(fp1,%lf,&InputData_xij); fclose(fp1); 预测输出 .txt,r)=NULL) /实际检验结果输出 printf(31can not open the filen); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 8 页 - -
16、- - - - - - - exit(0); for(i=0;iforecastdata;i+) for(j=0;joutpoints;j+) fscanf(fp3,%lf,&tpij); fclose(fp3); for(j=0;jinpoints;j+) / 检验数据归一化for(i=0;iforecastdata;i+) if(InpointData_MAXj=0) InputData_xij=0; else InputData_xij=(InputData_xij-InpointData_MINj+A)/(InpointData_MAXj-InpointData_MINj+A); fo
17、r(j=0;joutpoints;j+) for(i=0;iforecastdata;i+) if(OutpointData_MAXj=0) tpij=0; else tpij=(tpij-OutpointData_MINj+A)/(OutpointData_MAXj-OutpointData_MINj+A); do Initialization(); / 初始化连接权值wij,limeni,vki k=0; do e=0; for(t=0;tdatagrough;t+) out_sub1(t); /正向计算网络输出out_sub2(t); /反向计算,修正权值e+=Outpoint_ept;
18、 /计算输出误差 k+; while(k0.1); sss=0; /中间参数for(t=0;tforecastdata;t+) e1t=0; for(i=0;idefaultpoints;i+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 8 页 - - - - - - - - - double sum=0; for(j=0;jinpoints;j+) sum+=wij*InputData_xtj; defInpoint=sum+limeni; defOutpoint
19、i=1/(a+b*exp(-1*c*defInpoint); for(j=0;joutpoints;j+) ytj=0; for(i=0;i0.12); /*/ void main() int i,j,k; FILE *fp2; ReadData(); /读训练数据:输入和输出unitary(); /归一化,将输入输出数据归一,结果在0,1中forecast(); /检验误差计算权值 .txt,w)=NULL) /文件输出训练好的权值 printf(6can not open the filen); exit(0); for(i=0;idefaultpoints;i+) for(k=0;kin
20、points;k+) fprintf(fp2, %lf ,wik); fprintf(fp2,n); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 8 页 - - - - - - - - - fprintf(fp2,n); for(i=0;idefaultpoints;i+) fprintf(fp2, %lf ,limeni); fprintf(fp2,nn); for(i=0;idefaultpoints;i+) for(j=0;joutpoints;j+) fprintf(fp2, %lf ,vji); fprintf(fp2,n); fclose(fp2); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 8 页 - - - - - - - - -