BP演算法學習

來源:互聯網
上載者:User

此源碼只供學習只用,VC6.0下調試成功。

源碼為轉載別人部落格。

權值最後無法求出,不知什麼原因。 

#include "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 /*/舊權值儲存-》每次study的權值都儲存下來*/
double P[IN]; /*/單個樣本輸入資料*/
double T[ON]; /*/單個樣本教師資料*/
double W[HN][IN]; /*/輸入層至隱層權值*/
double V[ON][HN]; /*/隱層至輸出層權值*/
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個樣本的總誤差*/
double a; /*/輸出層至隱層的學習效率*/
double b; /*/隱層至輸入層學習效率*/
double alpha;  /*/動量因子,改進型bp演算法使用*/
double d_err[ON];

FILE *fp;
/*定義一個放學習樣本的結構*/
struct {
double input[IN];
double teach[ON];
}Study_Data[N];

/*改進型bp演算法用來儲存每次計算的權值*/
struct {
double old_W[HN][IN];
double old_V[ON][HN];
}Old_WV[Z];

int Start_Show()
{
//clrscr();
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 '訓練樣本.txt'(xunlianyangben.txt)  is /ncorrect,like 'F://BP//訓練樣本.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//訓練樣本.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");
getch();
getch();
//clrscr();
return 1;
}

int 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");
getch();
exit(0);
}

int GetTrainingData()      /*OK*/
{ int m,i,j;
  int datr;

if((fp=fopen("f://bp//訓練樣本.txt","r"))==NULL)         /*讀取訓練樣本*/
 {
  printf("Cannot open file strike any key exit!");
  getch();
  exit(1);
 }

m=0;
i=0;
j=0;
while(fscanf(fp,"%d",&datr)!=EOF)
 {j++;
  if(j<=(N*IN))
   {
   if(i<IN)    
   {
      Study_Data[m].input[i]=datr;
      /*printf("/nthe Study_Datat[%d].input[%d]=%f/n",m,i,Study_Data[m].input[i]);getch();*/  /*use to check the loaded training datas*/
   }
   if(m==(N-1)&&i==(IN-1))
   {
       m=0;
       i=-1;
   }
      if(i==(IN-1))
      {
       m++;
       i=-1;
      }
   }
   else if((N*IN))   
   {
    if(i<ON)     
    {
  Study_Data[m].teach[i]=datr;
  /*printf("/nThe Study_Data[%d].teach[%d]=%f",m,i,Study_Data[m].teach[i]);getch();*/  /*use to check the loaded training datas*/
    }
       if(m==(N-1)&&i==(ON-1))
        printf("/n");

       if(i==(ON-1))
    {m++;
       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;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...");
getch();
 return 1;
}

/*///////////////////////////////////*/
/*初始化權、閾值子程式*/
/*///////////////////////////////////*/
initial()
{int i;
 int ii;
 int j;
 int jj;
 int k;
 int kk;
/*隱層權、閾值初始化*/

 for(i=0;i<HN;i++) {
  for(j=1;j<IN;j++)   {W[i][j]=(double)((rand()/32767.0)*2-1); /*初始化輸入層到隱層的權值,隨機類比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); /*初始化隱層到輸出層的權值,隨機類比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 之間*/
  }
  return 1;
}/*子程式initial()結束*/

/*//////////////////////////////////////////*/
/*第m個學習樣本輸入子程式*/
/*/////////////////////////////////////////*/
int 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個樣本的資料*/
return 1;
}/*子程式input_P(m)結束*/

/*/////////////////////////////////////////*/
/*第m個樣本教師訊號子程式*/
/*/////////////////////////////////////////*/
int input_T(int m)
{int k;

 for(k=0;k<ON;k++)  T[k]=Study_Data[m].teach[k];
return 1;
}/*子程式input_T(m)結束*/

int 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];/*求隱層內積*/
    }

   X[j]=sigma-YU_HN[i];/*求隱層淨輸入,為什麼減隱層的閥值:神經網路*/
   H[j]=1.0/(1.0+exp(-X[j]));/*求隱層輸出 siglon演算法*/
   }
return 1;
}/*子程式H_I_O()結束*/

int 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[k];
  }
 Y[k]=sigma-YU_ON[k];
 O[k]=1.0/(1.0+exp(-Y[k]));
 }
return 1;
}

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;
}

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;
}

int 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;
}

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)
  {
   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;
}

int 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)
 {
  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<HN;m++) {
  total_err+=err_m[m];
 }
return total_err;
}

void savequan()
{ int i,j,k;
  int ii,jj,kk;

if((fp=fopen("f://bp//權值.txt","a"))==NULL)         /*save the result at f:/hsz/bpc/*.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;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 “權值.txt”(quanzhi) has been saved successfully!/nPress any key to continue...");
getch();

if((fp=fopen("f://bp//閾值.txt","a"))==NULL)         /*save the result at f:/hsz/bpc/*/
 {
  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++)   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...");
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;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(m);                             /*(m,study)*/
    Delta_H_I(m);                              /*(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");
    getch();
    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();
}

下面是作者給的結果:

權值.txt

{Save the result of “權值”(quanzhi) as follows:
W[0][0]=0.350578
W[0][1]=-1.008697
W[0][2]=-0.962250
W[1][0]=0.055661
W[1][1]=-0.372367
W[1][2]=-0.890795
W[2][0]=0.129752
W[2][1]=-0.332591
W[2][2]=-0.521561

V[0][0]=-2.932654
V[0][1]=-3.720583
V[0][2]=-2.648183
V[1][0]=2.938970
V[1][1]=1.633281
V[1][2]=1.944077

}

閾值.txt

{Save the result of “輸出層的閾值”(huozhi) as follows:
YU_ON[0]=-4.226843
YU_ON[1]=1.501791

Save the result of “隱層的閾值為”(huozhi) as follows:
YU_HN[0]=-0.431459
YU_HN[1]=0.452127
YU_HN[2]=0.258449

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.