?? bp2.txt
字號(hào):
//***************************************************************//
// BP neural network //
//***************************************************************//
//訓(xùn)練樣本如下
// 1
// 1
// -1
// 0
// 1
// 1
// -1
// 1
// 0
// 1
// 每五行為一組 前三個(gè)是數(shù)據(jù)數(shù)據(jù) 后兩個(gè)是教師信號(hào)
#include "stdlib.h"
#include "math.h"
#include "stdio.h"
#define N 2 /*/學(xué)習(xí)樣本個(gè)數(shù)*/
#define IN 3 /*/輸入層神經(jīng)元數(shù)目*/
#define HN 3 /*/隱層神經(jīng)元數(shù)目*/
#define ON 2 /*/輸出層神經(jīng)元數(shù)目*/
#define Z 20 /*/舊權(quán)值保存-》每次study的權(quán)值都保存下來(lái)*/
double P[IN]; /*/單個(gè)樣本輸入數(shù)據(jù)*/
double T[ON]; /*/單個(gè)樣本教師數(shù)據(jù)*/
double W[HN][IN]; /*/輸入層至隱層權(quán)值*/
double V[ON][HN]; /*/隱層至輸出層權(quán)值*/
double X[HN]; /*/隱層的輸入*/
double Y[ON]; /*/輸出層的輸入*/
double H[HN]; /*/隱層的輸出*/
double O[ON]; /*/輸出層的輸出*/
double YU_HN[HN]; /*/隱層的閾值*/
double YU_ON[ON]; /*/輸出層的閾值*/
double err_m[N]; /*/第m個(gè)樣本的總誤差*/
double a; /*/輸出層至隱層的學(xué)習(xí)效率*/
double b; /*/隱層至輸入層學(xué)習(xí)效率*/
double alpha; /*/動(dòng)量因子,改進(jìn)型bp算法使用*/
double d_err[ON];
FILE *fp;
/*定義一個(gè)放學(xué)習(xí)樣本的結(jié)構(gòu)*/
struct {
double input[IN];
double teach[ON];
}Study_Data[N];
/*改進(jìn)型bp算法用來(lái)保存每次計(jì)算的權(quán)值*/
struct {
double old_W[HN][IN];
double old_V[ON][HN];
}Old_WV[Z];
void Start_Show()
{
printf("\n ***********************\n");
printf(" * Welcome to use *\n");
printf(" * this program of *\n");
printf(" * calculating the BP *\n");
printf(" * model! *\n");
printf(" * Happy every day! *\n");
printf(" ***********************\n");
printf("\n\nBefore starting,please read the follows carefully:\n\n");
printf(" 1.Please ensure the Path of the '訓(xùn)練樣本.txt'(xunlianyangben.txt) is \ncorrect,like 'F:\\BP\\訓(xùn)練樣本.txt'!\n");
printf(" 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\\訓(xùn)練樣本.txt'!\n");
printf(" 4.The program of BP can study itself for no more than 30000 times.\nAnd surpassing the number,the program will be ended by itself in\npreventing running infinitely because of error!\n");
printf("\n\n\n");
printf("Now press any key to start...\\n");
}
void End_Show()
{
printf("\n\n---------------------------------------------------\n");
printf("The program has reached the end successfully!\n\nPress any key to exit!\n\n");
printf("\n ***********************\n");
printf(" * This is the end *\n");
printf(" * of the program which*\n");
printf(" * can calculate the BP*\n");
printf(" * model! *\n");
printf(" ***********************\n");
printf(" * Thanks for using! *\n");
printf(" * Happy every day! *\n");
printf(" ***********************\n");
exit(0);
}
GetTrainingData() /*OK*/
{
int m,i,j;
int datr;
if((fp=fopen("e:\\BP_DATA.txt","r"))==NULL) /*讀取訓(xùn)練樣本*/
{
printf("Cannot open file strike any key exit!");
exit(1);
}
m=0;
i=0;
j=0;
while(fscanf(fp,"%d",&datr)!=EOF)
{
if (i<IN)
{
Study_Data[m].input[i]=datr;
i++;
}
else
{
Study_Data[m].teach[j]=datr;
j++;
if (j==ON)
{
m++;
i=0;
j=0;
}
}
}
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;m<N;m++)
{
for(i=0;i<IN;i++)
{
printf("\nStudy_Data[%d].input[%d]=%f",m,i,Study_Data[m].input[i]);
}
for(j=0;j<ON;j++)
{
printf("\nStudy_Data[%d].teach[%d]=%f",m,j,Study_Data[m].teach[j]);
}
}
printf("\n\nPress any key to start calculating...\n\n");
return 1;
}
/*///////////////////////////////////*/
/*初始化權(quán)、閾值子程序*/
/*///////////////////////////////////*/
initial()
{
int i;
int ii;
int j;
int jj;
int k;
int kk;
/*隱層權(quán)、閾值初始化*/
for(i=0;i<HN;i++)
{
for(j=0;j<IN;j++)
{
W[i][j]=(double)((rand()/32767.0)*2-1); /*初始化輸入層到隱層的權(quán)值,隨機(jī)模擬0 和 1 -1 */
printf("w[%d][%d]=%f\n",i,j,W[i][j]);
}
}
for(ii=0;ii<ON;ii++)
{
for(jj=0;jj<HN;jj++)
{
V[ii][jj]= (double)((rand()/32767.0)*2-1); /*初始化隱層到輸出層的權(quán)值,隨機(jī)模擬0 和 1 -1*/
printf("V[%d][%d]=%f\n",ii,jj,V[ii][jj]);
}
}
for(k=0;k<HN;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;kk<ON;kk++)
{
YU_ON[kk] = (double)((rand()/32767.0)*2-1); /*輸出層閾值初始化 ,-0.01 ~ 0.01 之間*/
printf("YU_ON[%d]=%f\n",kk,YU_ON[kk]);
}
return 1;
}/*子程序initial()結(jié)束*/
/*//////////////////////////////////////////*/
/*第m個(gè)學(xué)習(xí)樣本輸入子程序*/
/*/////////////////////////////////////////*/
input_P(int m)
{
int i;
for(i=0;i<IN;i++)
{
P[i]=Study_Data[m].input[i];
printf("P[%d]=%f\n",i,P[i]);
}
/*獲得第m個(gè)樣本的數(shù)據(jù)*/
return 1;
}/*子程序input_P(m)結(jié)束*/
/*/////////////////////////////////////////*/
/*第m個(gè)樣本教師信號(hào)子程序*/
/*/////////////////////////////////////////*/
input_T(int m)
{
int k;
for(k=0;k<ON;k++)
{
T[k]=Study_Data[m].teach[k];
}
return 1;
}/*子程序input_T(m)結(jié)束*/
//隱層輸出 H
H_I_O()
{
double sigma;
int i,j;
for(j=0;j<HN;j++)
{
sigma=0;
for(i=0;i<IN;i++)
{
sigma+=W[j][i]*P[i]; /*求隱層內(nèi)積*/
}
X[j]=sigma-YU_HN[i]; /*求隱層凈輸入,為什么減隱層的閥值*/
H[j]=1.0/(1.0+exp(-X[j])); /*求隱層輸出 siglon算法*/
}
return 1;
}/*子程序H_I_O()結(jié)束*/
//輸出層輸出
O_I_O()
{
int k;
int j;
double sigma;
for(k=0;k<ON;k++)
{
sigma=0.0;
for(j=0;j<HN;j++)
{
sigma+=V[k][j]*H[j];
}
Y[k]=sigma-YU_ON[k];
O[k]=1.0/(1.0+exp(-Y[k]));
}
return 1;
}
//輸出層誤差計(jì)算
int Err_O_H(int m)
{
int k;
double abs_err[ON];
double sqr_err=0;
for (k=0;k<ON;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;
}
return 1;
}
//隱層誤差計(jì)算
double e_err[HN];
int Err_H_I()
{
int j,k;
double sigma;
for(j=0;j<HN;j++)
{
sigma=0.0;
for(k=0;k<ON;k++)
{
sigma=d_err[k]*V[k][j];
}
e_err[j]=sigma*H[j]*(1-H[j]);
}
return 1;
}
saveWV(int m)
{
int i;
int ii;
int j;
int jj;
for(i=0;i<HN;i++)
{
for(j=0;j<IN;j++)
{
Old_WV[m].old_W[i][j] = W[i][j];
}
}
for(ii=0;ii<ON;ii++)
{
for(jj=0;jj<HN;jj++)
{
Old_WV[m].old_V[ii][jj] = V[ii][jj];
}
}
return 1;
}
//--------------------------------------------
// 隱層到輸出層權(quán)值調(diào)整
//--------------------------------------------
int Delta_O_H(int n) /*(int m,int n)*/
{
int k,j;
if(n<1) /*n<=1*/
{
for (k=0;k<ON;k++)
{
for (j=0;j<HN;j++)
{
V[k][j]=V[k][j]+a*d_err[k]*H[j];
}
YU_ON[k]+=a*d_err[k];
}
}
else if(n>1) //在權(quán)值調(diào)整中假如姿態(tài)項(xiàng)
{
for (k=0;k<ON;k++)
{
for (j=0;j<HN;j++)
{
V[k][j]=V[k][j]+a*d_err[k]*H[j]+alpha*(V[k][j]-Old_WV[(n-1)].old_V[k][j]);
}
YU_ON[k]+=a*d_err[k];
}
}
return 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++)
{
W[j][i]=W[j][i]+b*e_err[j]*P[i];
}
YU_HN[j]+=b*e_err[j];
}
}
else if(n>1) //在權(quán)值調(diào)整中假如姿態(tài)項(xiàng)
{
for(j=0;j<HN;j++)
{
for(i=0;i<IN;i++)
{
W[j][i]=W[j][i]+b*e_err[j]*P[i]+alpha*(W[j][i]-Old_WV[(n-1)].old_W[j][i]);
}
YU_HN[j]+=b*e_err[j];
}
}
return 1;
}
double Err_Sum()
{
int m;
double total_err=0;
for(m=0;m<N;m++)
{
total_err+=err_m[m];
}
return total_err;
}
void savequan()
{
int i,j,k;
int ii,jj,kk;
if((fp=fopen("e:\\BP_WEIGHT.txt","a"))==NULL) /*save the result at f:\hsz\bpc\*.txt*/
{
printf("Cannot open file strike any key exit!");
exit(1);
}
fprintf(fp,"Save the result of “權(quán)值”(quanzhi) as follows:\n");
for(i=0;i<HN;i++)
{
for(j=0;j<IN;j++)
fprintf(fp,"W[%d][%d]=%f\n",i,j,W[i][j]);
}
fprintf(fp,"\n");
for(ii=0;ii<ON;ii++)
{
for(jj=0;jj<HN;jj++)
fprintf(fp,"V[%d][%d]=%f\n",ii,jj,V[ii][jj]);
}
fclose(fp);
printf("\nThe result of 'BP_WEIGHT.txt'(quanzhi) has been saved successfully!\nPress any key to continue...");
if((fp=fopen("e:\\BP_YUZHI.txt","a"))==NULL) /*save the result at f:\hsz\bpc\*/
{
printf("Cannot open file strike any key exit!");
exit(1);
}
fprintf(fp,"Save the result of “輸出層的閾值”(huozhi) as follows:\n");
for(k=0;k<ON;k++)
fprintf(fp,"YU_ON[%d]=%f\n",k,YU_ON[k]);
fprintf(fp,"\nSave the result of “隱層的閾值為”(huozhi) as follows:\n");
for(kk=0;kk<HN;kk++)
fprintf(fp,"YU_HN[%d]=%f\n",kk,YU_HN[kk]);
fclose(fp);
printf("\nThe result of “閾值.txt”(huozhi) has been saved successfully!\nPress any key to continue...");
}
/**********************/
/**程序入口,即主程序**/
/**********************/
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;/*實(shí)際值為Pre_error=0.0001;*/
Start_Show();
GetTrainingData();
initial();
do
{
int m;
++study;
for(m=0;m<N;m++)
{
input_P(m);
input_T(m);
H_I_O();
O_I_O();
Err_O_H(m);
Err_H_I();
saveWV(m); /****************/
Delta_O_H(2); /*(m,study)*/
Delta_H_I(2); /*(m,study)*/
}
sum_err=Err_Sum();
printf("sum_err=%f\n",sum_err);
printf("Pre_error=%f\n\n",Pre_error);
if(study>flag)
{
printf("\n*******************************\n");
printf("The program is ended by itself because of error!\nThe learning times is surpassed!\n");
printf("*****************************\n");
break;
}
}while (sum_err>Pre_error);
printf("\n****************\n");
printf("\nThe program have studyed for [%d] times!\n",study);
printf("\n****************\n");
savequan(); /*save the results*/
End_Show();
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -