程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++實現的BP神經網絡(代碼)

C++實現的BP神經網絡(代碼)

編輯:關於C++

#pragma hdrstop
#include <stdio.h>
#include <iostream.h>

const A=30.0;
const B=10.0;
const MAX=500; //最大訓練次數
const COEF=0.0035; //網絡的學習效率
const BCOEF=0.001; //網絡的閥值調整效率
const ERROR=0.002; //網絡訓練中的允許誤差
const ACCURACY=0.0005; //網絡要求精度
double sample[41][4]={{0,0,0,0},{5,1,4,19.020},{5,3,3,14.150},
                                                          {5,5,2,14.360},{5,3,3,14.150},{5,3,2,15.390},
                                                          {5,3,2,15.390},{5,5,1,19.680},{5,1,2,21.060},
                                                          {5,3,3,14.150},{5,5,4,12.680},{5,5,2,14.360},
                                                          {5,1,3,19.610},{5,3,4,13.650},{5,5,5,12.430},
                                                          {5,1,4,19.020},{5,1,4,19.020},{5,3,5,13.390},
                                                          {5,5,4,12.680},{5,1,3,19.610},{5,3,2,15.390},
                                                          {1,3,1,11.110},{1,5,2,6.521},{1,1,3,10.190},
                                                          {1,3,4,6.043},{1,5,5,5.242},{1,5,3,5.724},
                                                          {1,1,4,9.766},{1,3,5,5.870},{1,5,4,5.406},
                                                          {1,1,3,10.190},{1,1,5,9.545},{1,3,4,6.043},
                                                          {1,5,3,5.724},{1,1,2,11.250},{1,3,1,11.110},
                                                          {1,3,3,6.380},{1,5,2,6.521},{1,1,1,16.000},
                                                          {1,3,2,7.219},{1,5,3,5.724}};


  double w[4][10][10],wc[4][10][10],b[4][10],bc[4][10];
  double o[4][10],netin[4][10],d[4][10],differ; //單個樣本的誤差
  double is; //全體樣本均方差
  int count,a;

  void netout(int m,int n); //計算網絡隱含層和輸出層的輸出
  void calculd(int m,int n); //計算網絡的反向傳播誤差
  void calcalwc(int m,int n); //計算網絡權值的調整量
  void calcaulbc(int m,int n); //計算網絡閥值的調整量
  void changew(int m,int n); //調整網絡權值
  void changeb(int m,int n); //調整網絡閥值
  void clearwc(int m,int n); //清除網絡權值變化量wc
  void clearbc(int m,int n); //清除網絡閥值變化量bc
  void initialw(void); //初始化NN網絡權值W
  void initialb(void); //初始化NN網絡閥值
  void calculdiffer(void); //計算NN網絡單個樣本誤差
  void calculis(void); //計算NN網絡全體樣本誤差
  void trainNN(void); //訓練NN網絡


  /*計算NN網絡隱含層和輸出層的輸出   */
  void netout(int m,int n)
  {
      int i,j,k;

      //隱含層各節點的的輸出
      for (j=1,i=2;j<=m;j++) //m為隱含層節點個數
      {
          netin[i][j]=0.0;
          for (k=1;k<=3;k++) //隱含層的每個節點均有三個輸入變量
              netin[i][j]=netin[i][j]+o[i-1][k]*w[i][k][j];
          netin[i][j]=netin[i][j]-b[i][j];
        o[i][j]=A/(1+exp(-netin[i][j]/B));
      }

      //輸出層各節點的輸出
      for (j=1,i=3;j<=n;j++)
      {
          netin[i][j]=0.0;
          for (k=1;k<=m;k++)
              netin[i][j]=netin[i][j]+o[i-1][k]*w[i][k][j];
          netin[i][j]=netin[i][j]-b[i][j];
          o[i][j]=A/(1+exp(-netin[i][j]/B));
      }
  }


  /*計算NN網絡的反向傳播誤差*/
  void calculd(int m,int n)
  {
        int i,j,k;
        double   t;
        a=count-1;
      d[3][1]=(o[3][1]-sample[a][3])*(A/B)*exp(-netin[3][1]/B)/pow(1+exp(-netin[3][1]/B),2);

        //隱含層的誤差
        for (j=1,i=2;j<=m;j++)
        {
            t=0.00;
            for (k=1;k<=n;k++)
                t=t+w[i+1][j][k]*d[i+1][k];
                  d[i][j]=t*(A/B)*exp(-netin[i][j]/B)/pow(1+exp(-netin[i][j]/B),2);
        }
  }


  /*計算網絡權值W的調整量*/
  void calculwc(int m,int n)
  {
      int i,j,k;

    //   輸出層(第三層)與隱含層(第二層)之間的連接權值的調整
      for (i=1,k=3;i<=m;i++)
      {
          for (j=1;j<=n;j++)
          {
              wc[k][i][j]=-COEF*d[k][j]*o[k-1][i]+0.5*wc[k][i][j];
          }
        //   printf("\n");
      }
      //隱含層與輸入層之間的連接權值的調整
      for (i=1,k=2;i<=m;i++)
      {
          for (j=1;j<=m;j++)
          {
              wc[k][i][j]=-COEF*d[k][j]*o[k-1][i]+0.5*wc[k][i][j];
          }
      //     printf("\n");
      }

  }

  /*計算網絡閥值的調整量*/
  void calculbc(int m,int n)
  {
      int   j;
      for (j=1;j<=m;j++)
      {
          bc[2][j]=BCOEF*d[2][j];
      }
      for (j=1;j<=n;j++)
      {
          bc[3][j]=BCOEF*d[3][j];
      }
  }

  /*調整網絡權值*/
  void changw(int m,int n)
  {
      int i,j;
      for (i=1;i<=3;i++)
          for (j=1;j<=m;j++)
          {
              w[2][i][j]=0.9*w[2][i][j]+wc[2][i][j];
              //為了保證系統有較好的魯棒性,計算權值時乘慣性系數0.9
              printf("w[2][%d][%d]=%f\n",i,j,w[2][i][j]);
          }
      for (i=1;i<=m;i++)
          for (j=1;j<=n;j++)
          {
              w[3][i][j]=0.9*w[3][i][j]+wc[3][i][j];
              printf("w[3][%d][%d]=%f\n",i,j,w[3][i][j]);
          }
  }

  /*調整網絡閥值*/
  void changb(int m,int n)
  {
      int   j;
      for (j=1;j<=m;j++)
          b[2][j]=b[2][j]+bc[2][j];

      for (j=1;j<=n;j++)
          b[3][j]=b[3][j]+bc[3][j];
  }

  /*清除網絡權值變化量wc*/
  void clearwc(void)
  {
      for (int i=0;i<4;i++)
          for (int   j=0;j<10;j++)
              for (int   k=0;k<10;k++)
                  wc[i][j][k]=0.00;
  }

  /*清除網絡閥值變化量*/
  void clearbc(void)
  {
      for (int i=0;i<4;i++)
          for (int   j=0;j<10;j++)
              bc[i][j]=0.00;
  }

  /*初始化網絡權值W*/
  void initialw(void)
  {
      int i,j,k,x;
      double   weight;
      for (i=0;i<4;i++)
          for (j=0;j<10;j++)
              for (k=0;k<10;k++)
              {
                  randomize();
                  x=100+random(400);
                  weight=(double)x/5000.00;
                  w[i][j][k]=weight;
              }
  }


  /*初始化網絡閥值*/
  void initialb(void)
  {
      int i,j,x;
      double   fazhi;
      for (i=0;i<4;i++)
        for (j=0;j<10;j++)
        {
            randomize();
            for (int   k=0;k<12;k++)
            {
                x=100+random(400);
            }
            fazhi=(double)x/50000.00;
            b[i][j]=fazhi;
        }
  }

  /*計算網絡單個樣本誤差*/
  void calculdiffer(void)
  {
      a=count-1;
      differ=0.5*(o[3][1]-sample[a][3])*(o[3][1]-sample[a][3]);
  }

  void calculis(void)
  {
      int i;
      is=0.0;
      for (i=0;i<=19;i++)
      {
          o[1][1]=sample[i][0];
          o[1][2]=sample[i][1];
          o[1][3]=sample[i][2];
          netout(8,1);
          is=is+(o[3][1]-sample[i][3])*(o[3][1]-sample[i][3]);
      }
      is=is/20;
  }

  /*訓練網絡*/
  void trainNN(void)
  {
      long   int   time;
      int i,x[4];
      initialw();
      initialb();

      for (time=1;time<=MAX;time++)
      {
          count=0;
          while(count<=40)
          {
              o[1][1]=sample[count][0];
              o[1][2]=sample[count][1];
              o[1][3]=sample[count][2];

              count=count+1;
              clearwc();
              clearbc();
              netout(8,1);
              calculdiffer();
              while(differ>ERROR)
              {
                  calculd(8,1);
                  calculwc(8,1);
                  calculbc(8,1);
                  changw(8,1);
                  changb(8,1);
                  netout(8,1);
                  calculdiffer();
              }
          }
          printf("This   is   %d   times   training   NN...\n",time);
          calculis();
          printf("is==%f\n",is);
          if   (is<ACCURACY)   break;
      }
  }


  //---------------------------------------------------------------------------

  #pragma   argsused
  int main(int   argc,   char*   argv[])
  {
      double   result;
      int m,test[4];
      char   ch='y';
      cout<<"Please   wait   for   the   train   of   NN:"<<endl;
      trainNN();
      cout<<"Now,this   modular   network   can   work   for   you."<<endl;

      while(ch=='y'   ||   ch=='Y')
      {
        cout<<"Please   input   data   to   be   tested."<<endl;
          for (m=1;m<=3;m++)
          cin>>test[m];
          ch=getchar();
          o[1][1]=test[1];
          o[1][2]=test[2];
          o[1][3]=test[3];
          netout(8,1);
          result=o[3][1];
          printf("Final   result   is   %f.\n",result);
          printf("Still   test?[Yes]   or   [No]\n");
          ch=getchar();
      }


                  return 0;
  }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved