BP神经网络的C语言汇总2.docx
- 文档编号:12690888
- 上传时间:2023-04-21
- 格式:DOCX
- 页数:83
- 大小:32.29KB
BP神经网络的C语言汇总2.docx
《BP神经网络的C语言汇总2.docx》由会员分享,可在线阅读,更多相关《BP神经网络的C语言汇总2.docx(83页珍藏版)》请在冰豆网上搜索。
BP神经网络的C语言汇总2
#include"stdio.h"
#include"stdlib.h"
#include"time.h"
#include"math.h"
/*********************************************
inpoints为输入神经元个数,可改变
outpoints为输出神经元个数
defaultpoints为隐层神经元个数
datagrough为样本数据个数
**********************************************
******以下数据定义可以修改*****/
#defineA0
#definea1
#defineb1
#definec1
#defineALFA0.85
#defineBETA0.2//学习率0~1
#defineTotal20000
#defineinpoints9
#defineoutpoints5
#definedefaultpoints28
#definedatagrough44
#defineforecastdata4
/**********定义所需变量********/
doubleInpointData[datagrough][inpoints],OutpointData[datagrough][outpoints];/*输入输出数据*/
doubleInpointData_MAX[inpoints],InpointData_MIN[inpoints];/*每个因素最大数据*/
doubleOutpointData_MAX[outpoints],OutpointData_MIN[outpoints];/*每个因素最小数据*/
doublew[defaultpoints][inpoints],limen[defaultpoints],v[outpoints][defaultpoints];/*连接权值、阈值*/
doubledlta_w[defaultpoints][inpoints],dlta_limen[defaultpoints],dlta_v[outpoints][defaultpoints];/*连接权、阈值修正值*/
doubledefaultOutpoint[defaultpoints],Outpoint_dp[outpoints],Outpoint_ep[datagrough];
/**************************读数据文件******************************/
voidReadData()
{
FILE*fp1,*fp2;
inti,j;
if((fp1=fopen("D:
\\data\\训练输入.txt","r"))==NULL)
{
printf("1cannotopenthefile\n");
exit(0);
}
for(i=0;i for(j=0;j fscanf(fp1,"%lf",&InpointData[i][j]); fclose(fp1); if((fp2=fopen("D: \\data\\训练输出.txt","r"))==NULL) { printf("2cannotopenthefile\n"); exit(0); } for(i=0;i for(j=0;j fscanf(fp2,"%lf",&OutpointData[i][j]); fclose(fp2); } /*****************************************************/ /*****************************************归一化******************************************************/ voidunitary() { inti,j; intk=0; for(j=0;j { InpointData_MAX[j]=InpointData[0][j]; InpointData_MIN[j]=InpointData[0][j]; for(i=0;i if(InpointData_MAX[j] InpointData_MAX[j]=InpointData[i][j]; elseif(InpointData_MIN[j]>InpointData[i][j]) InpointData_MIN[j]=InpointData[i][j]; } for(j=0;j { OutpointData_MAX[j]=OutpointData[0][j]; OutpointData_MIN[j]=OutpointData[0][j]; for(i=0;i if(OutpointData_MAX[j] OutpointData_MAX[j]=OutpointData[i][j]; elseif(OutpointData_MIN[j]>OutpointData[i][j]) OutpointData_MIN[j]=OutpointData[i][j]; } /***************将数据归一处理,处理之后的数据全部在[0,1]之间*************************/ for(j=0;j for(i=0;i if(InpointData_MAX[j]==0) InpointData[i][j]=0; else InpointData[i][j]=(InpointData[i][j]-InpointData_MIN[j]+A)/(InpointData_MAX[j]-InpointData_MIN[j]+A); for(j=0;j for(i=0;i if(OutpointData_MAX[j]==0) OutpointData[i][j]=0; else OutpointData[i][j]=(OutpointData[i][j]-OutpointData_MIN[j]+A)/(OutpointData_MAX[j]-OutpointData_MIN[j]+A); } /*****************************************************/ /*********************初始化,随机赋初值**************************/ voidInitialization() { inti,j; srand((unsigned)time(NULL));//头文件名#include for(i=0;i for(j=0;j { w[i][j]=(rand()*2.0/RAND_MAX-1)/2; dlta_w[i][j]=0; } for(i=0;i { limen[i]=(rand()*2.0/RAND_MAX-1)/2; dlta_limen[i]=0; } for(i=0;i for(j=0;j { v[i][j]=(rand()*2.0/RAND_MAX-1)/2; dlta_v[i][j]=0; } } /**********************求单样本的计算输出误差*******************************/ voidout_sub1(intt) { inti,j; doubledefaultInpoint[defaultpoints]; doubleOutpoint_y[outpoints]; Outpoint_ep[t]=0; for(i=0;i { doublesum=0; for(j=0;j sum+=w[i][j]*InpointData[t][j]; defaultInpoint[i]=sum+limen[i]; defaultOutpoint[i]=1/(a+b*exp(-1*c*defaultInpoint[i]));//求O[i] } for(j=0;j { Outpoint_y[j]=0; for(i=0;i Outpoint_y[j]+=v[j][i]*defaultOutpoint[i]; Outpoint_dp[j]=OutpointData[t][j]-Outpoint_y[j]; Outpoint_ep[t]+=Outpoint_dp[j]*Outpoint_dp[j]/2; } } /*****************************反算权值******************************************/ voidout_sub2(intt) { inti,j,k; doubles; for(i=0;i { s=0; for(j=0;j { dlta_v[j][i]=ALFA*dlta_v[j][i]+BETA*Outpoint_dp[j]*defaultOutpoint[i];// s+=v[j][i]*Outpoint_dp[j]; v[j][i]+=dlta_v[j][i]; } dlta_limen[i]=ALFA*dlta_limen[i]+BETA*defaultOutpoint[i]*(1-defaultOutpoint[i])*s;// limen[i]+=dlta_limen[i]; for(k=0;k { dlta_w[i][k]=ALFA*dlta_w[i][k]+BETA*defaultOutpoint[i]*(1-defaultOutpoint[i])*s*InpointData[t][k];// w[i][k]=w[i][k]+dlta_w[i][k]; } } } /*******************************************************/ voidforecast() { inti,j,t,k=0; doublee,e1[forecastdata]={0};//训练误差 doublesss; doubleInputData_x[forecastdata][inpoints],tp[forecastdata][outpoints]; doubledefInpoint,defOutpoint[defaultpoints],y[forecastdata][outpoints];//y[forecastdata][outpoints]为网络检验输出 FILE*fp1,*fp3; if((fp1=fopen("D: \\data\\预测输入.txt","r"))==NULL)//检验数据输入 { printf("3cannotopenthefile\n"); exit(0); } for(i=0;i for(j=0;j fscanf(fp1,"%lf",&InputData_x[i][j]); fclose(fp1); if((fp3=fopen("D: \\data\\预测输出.txt","r"))==NULL)//实际检验结果输出 { printf("31cannotopenthefile\n"); exit(0); } for(i=0;i for(j=0;j fscanf(fp3,"%lf",&tp[i][j]); fclose(fp3); for(j=0;j for(i=0;i if(InpointData_MAX[j]==0) InputData_x[i][j]=0; else InputData_x[i][j]=(InputData_x[i][j]-InpointData_MIN[j]+A)/(InpointData_MAX[j]-InpointData_MIN[j]+A); for(j=0;j for(i=0;i if(OutpointData_MAX[j]==0) tp[i][j]=0; else tp[i][j]=(tp[i][j]-OutpointData_MIN[j]+A)/(OutpointData_MAX[j]-OutpointData_MIN[j]+A); do { Initialization();//初始化连接权值w[i][j],limen[i],v[k][i] k=0; do { e=0; for(t=0;t { out_sub1(t);//正向计算网络输出 out_sub2(t);//反向计算,修正权值 e+=Outpoint_ep[t];//计算输出误差 } k++; }while((k sss=0;//中间参数 for(t=0;t { e1[t]=0; for(i=0;i { doublesum=0; for(j=0;j sum+=w[i][j]*InputData_x[t][j]; defInpoint=sum+limen[i]; defOutpoint[i]=1/(a+b*exp(-1*c*defInpoint)); } for(j=0;j { y[t][j]=0; for(i=0;i y[t][j]+=v[j][i]*defOutpoint[i]; e1[t]+=(y[t][j]-tp[t][j])*(y[t][j]-tp[t][j])/2; y[t][j]=y[t][j]*(OutpointData_MAX[j]-OutpointData_MIN[j]+A)+OutpointData_MIN[j]-A; } sss+=e1[t]; } sss=sss/forecastdata; printf("%lf%lf\n",e,sss); }while(sss>0.12); } /********************************************************/ voidmain() { inti,j,k; FILE*fp2; ReadData();//读训练数据: 输入和输出 unitary();//归一化,将输入输出数据归一,结果在[0,1]中 forecast();//检验误差 if((fp2=fopen("D: \\data\\计算权值.txt","w"))==NULL)//文件输出训练好的权值 { printf("6cannotopenthefile\n"); exit(0); } for(i=0;i { for(k=0;k fprintf(fp2,"%lf",w[i][k]); fprintf(fp2,"\n"); } fprintf(fp2,"\n"); for(i=0;i fprintf(fp2,"%lf",limen[i]); fprintf(fp2,"\n\n"); for(i=0;i { for(j=0;j fprintf(fp2,"%lf",v[j][i]); fprintf(fp2,"\n"); } fclose(fp2); } 看到学生用Matlab做了BP神经网络应用的程序,突然想起自己在上学时写了个BP的C语言版,现在真不知道当时是怎么写出来的,这里放上来做一个纪念。 #include #include #include #include #include #include #include #defineMAXWEIGHT((float)0.3) #defineSCALEWEIGHT((float)32767) #definenInputNodes4 #definenHiddenNodes8 #definenOutputNodes3 #definenPatterns15 #definenIterations5000 #defineESC27 #defineERRORLEVEL0.0001 #defineITEMS8 typedeffloat*PFLOAT; typedefPFLOATVECTOR; typedefPFLOAT*MATRIX; voidVectorAllocate(VECTOR*vector,intnCols); voidAllocateCols(PFLOATmatrix[],intnRows,intnCols); voidMatrixAllocate(MATRIX*pmatrix,intnRows,intnCols); voidMatrixFree(MATRIXmatrix,intnRows); MATRIXout0; MATRIXout1; MATRIXdelta1; MATRIXdelw1; MATRIXw1; MATRIXout2; MATRIXdelta2; MATRIXdelw2; MATRIXw2; MATRIXtarget; //VECTORPatternID; voidmain() { floateta=0.15, alpha=0.075; intnReportErrors=100; floatErrorLevel=ERRORLEVEL; charMonitorError=0; floaterror; registerinth; registerinti; registerintj; intp, q; //r; FILE*fpPattern, *fpWeightsOut, *fpResults, *fpError, *fpTest; MatrixAllocate(&out0,nPatterns,nInputNodes); MatrixAllocate(&out1,nPatterns,nHiddenNodes); MatrixAllocate(&out2,nPatterns,nOutputNodes); MatrixAllocate(&delta2,nPatterns,nOutputNodes); MatrixAllocate(&delw2,nOutputNodes,nHiddenNodes+1); MatrixAllocate(&w2,nOutputNodes,nHiddenNodes+1); MatrixAllocate(&delta1,nPatterns,nHiddenNodes); MatrixAllocate(&delw1,nHiddenNodes,nInputNodes+1); MatrixAllocate(&w1,nHiddenNodes,nInputNodes+1); MatrixAllocate(&target,nPatterns,nOutputNodes); //VectorAllocate(&PatternID,nPatterns); //初始化权值---------------------------------- srand((unsigned)time(NULL)); for(h=0;h { for(i=0;i<=nInputNodes;i++) { floatfrand=rand(); w1[h][i]=MAXWEIGHT*(1.0-2*frand/SCALEWEIGHT); delw1[h][i]=0.0; } } for(j=0;j for(h=0;h<=nHiddenNodes;h++) { floatfrand=rand(); w2[j][h]=MAXWEIGH
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- BP 神经网络 语言 汇总