声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2377|回复: 2

[人工智能] 神经网络BP算法

[复制链接]
发表于 2006-11-22 22:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
神经网络BP算法(vC++程序)
                                       

文件输入输出目录为:F:\BP\

训练样本文件名:训练样本.txt

值为:

1
1
-1
1
-1
1
0
1
0
1


输出文件名为:阈值.txt    权值.txt

=========================

  1. #include "stdlib.h"
  2. #include "math.h"
  3. #include "conio.h"
  4. #include "stdio.h"
  5. #define N 2 /*/学习样本个数*/
  6. #define IN 3 /*/输入层神经元数目*/
  7. #define HN 3 /*/隐层神经元数目*/
  8. #define ON 2 /*/输出层神经元数目*/
  9. #define Z 20 /*/旧权值保存-》每次study的权值都保存下来*/
  10. double P[IN]; /*/单个样本输入数据*/
  11. double T[ON]; /*/单个样本教师数据*/
  12. double W[HN][IN]; /*/输入层至隐层权值*/
  13. double V[ON][HN]; /*/隐层至输出层权值*/
  14. double X[HN]; /*/隐层的输入*/
  15. double Y[ON]; /*/输出层的输入*/
  16. double H[HN]; /*/隐层的输出*/
  17. double O[ON]; /*/输出层的输出*/
  18. double YU_HN[HN]; /*/隐层的阈值*/
  19. double YU_ON[ON]; /*/输出层的阈值*/
  20. double err_m[N]; /*/第m个样本的总误差*/
  21. double a; /*/输出层至隐层的学习效率*/
  22. double b; /*/隐层至输入层学习效率*/
  23. double alpha;  /*/动量因子,改进型bp算法使用*/
  24. double d_err[ON];

  25. FILE *fp;
  26. /*定义一个放学习样本的结构*/
  27. struct {
  28. double input[IN];
  29. double teach[ON];
  30. }Study_Data[N];

  31. /*改进型bp算法用来保存每次计算的权值*/
  32. struct {
  33. double old_W[HN][IN];
  34. double old_V[ON][HN];
  35. }Old_WV[Z];


  36. int Start_Show()
  37. {
  38. clrscr();
  39. printf("\n                       ***********************\n");
  40. printf("                       *    Welcome to use   *\n");
  41. printf("                       *  this program of    *\n");
  42. printf("                       *  calculating the BP *\n");
  43. printf("                       *      model!         *\n");
  44. printf("                       *   Happy every day!  *\n");
  45. printf("                       ***********************\n");
  46. printf("\n\nBefore starting,please read the follows carefully:\n\n");
  47. printf("    1.Please ensure the Path of the '训练样本.txt'(xunlianyangben.txt) is \ncorrect,like 'F:\BP\训练样本.txt'!\n");
  48. printf("    2.The calculating results will be saved in the Path of 'F:\\BP\\'!\n");
  49. printf("    3.The program will load 10 datas when running from 'F:\\BP\\训练样本.txt'!\n");
  50. 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");
  51. printf("\n\n\n");
  52. printf("Now press any key to start...\n");
  53. getch();
  54. getch();
  55. clrscr();
  56. }

  57. int End_Show()
  58. {
  59. printf("\n\n---------------------------------------------------\n");
  60. printf("The program has reached the end successfully!\n\nPress any key to exit!\n\n");
  61. printf("\n                       ***********************\n");
  62. printf("                       *    This is the end  *\n");
  63. printf("                       * of the program which*\n");
  64. printf("                       * can calculate the BP*\n");
  65. printf("                       *      model!         *\n");
  66. printf("                       ***********************\n");
  67. printf("                       *  Thanks for using!  *\n");
  68. printf("                       *   Happy every day!  *\n");
  69. printf("                       ***********************\n");
  70. getch();
  71. exit(0);
  72. }

  73. GetTrainingData()      /*OK*/
  74. { int m,i,j;
  75.   int datr;

  76. if((fp=fopen("f:\\bp\\训练样本.txt","r"))==NULL)         /*读取训练样本*/
  77. {
  78.   printf("Cannot open file strike any key exit!");
  79.   getch();
  80.   exit(1);
  81. }

  82. m=0;
  83. i=0;
  84. j=0;
  85. while(fscanf(fp,"%d",&datr)!=EOF)
  86. {j++;
  87.   if(j<=(N*IN))
  88.    {if(i     {
  89.       Study_Data[m].input[i]=datr;
  90.       /*printf("\nthe Study_Datat[%d].input[%d]=%f\n",m,i,Study_Data[m].input[i]);getch();*/  /*use to check the loaded training datas*/
  91.       }
  92.     if(m==(N-1)&&i==(IN-1))
  93.       {
  94.        m=0;
  95.        i=-1;
  96.       }
  97.     if(i==(IN-1))
  98.       {
  99.        m++;
  100.        i=-1;
  101.       }
  102.    }
  103.    else if((N*IN)    {if(i      {Study_Data[m].teach[i]=datr;
  104.        /*printf("\nThe Study_Data[%d].teach[%d]=%f",m,i,Study_Data[m].teach[i]);getch();*/  /*use to check the loaded training datas*/
  105.        }
  106.      if(m==(N-1)&&i==(ON-1))
  107.       printf("\n");

  108.      if(i==(ON-1))
  109.       {m++;
  110.        i=-1;
  111.       }
  112.     }
  113.   i++;
  114. }
  115. fclose(fp);
  116. printf("\nThere are [%d] datats that have been loaded successfully!\n",j);


  117. /*show the data which has been loaded!*/
  118. printf("\nShow the data which has been loaded as follows:\n");
  119. for(m=0;m {for(i=0;i   {printf("\nStudy_Data[%d].input[%d]=%f",m,i,Study_Data[m].input[i]);
  120.    }
  121.   for(j=0;j   {printf("\nStudy_Data[%d].teach[%d]=%f",m,j,Study_Data[m].teach[j]);
  122.    }
  123. }
  124. printf("\n\nPress any key to start calculating...");
  125. getch();
  126. return 1;
  127. }


  128. /*///////////////////////////////////*/
  129. /*初始化权、阈值子程序*/
  130. /*///////////////////////////////////*/
  131. initial()
  132. {int i;
  133. int ii;
  134. int j;
  135. int jj;
  136. int k;
  137. int kk;
  138. /*隐层权、阈值初始化*/

  139. for(i=0;i {
  140.   for(j=1;j   {W[i][j]=(double)((rand()/32767.0)*2-1); /*初始化输入层到隐层的权值,随机模拟0 和 1 -1 */
  141.     printf("w[%d][%d]=%f\n",i,j,W[i][j]);
  142.    }
  143.   }
  144. for(ii=0;ii {
  145.   for(jj=0;jj   {V[ii][jj]= (double)((rand()/32767.0)*2-1); /*初始化隐层到输出层的权值,随机模拟0 和 1 -1*/
  146.     printf("V[%d][%d]=%f\n",ii,jj,V[ii][jj]);
  147.    }
  148.   }
  149. for(k=0;k {
  150.   YU_HN[k] = (double)((rand()/32767.0)*2-1);  /*隐层阈值初始化 ,-0.01 ~ 0.01 之间*/
  151.   printf("YU_HN[%d]=%f\n",k,YU_HN[k]);
  152.   }
  153. for(kk=0;kk {
  154.   YU_ON[kk] = (double)((rand()/32767.0)*2-1); /*输出层阈值初始化 ,-0.01 ~ 0.01 之间*/
  155.   }
  156.   return 1;
  157. }/*子程序initial()结束*/


  158. /*//////////////////////////////////////////*/
  159. /*第m个学习样本输入子程序*/
  160. /*/////////////////////////////////////////*/
  161. input_P(int m)
  162. { int i,j;

  163.   for(i=0;i  {P[i]=Study_Data[m].input[i];
  164.    printf("P[%d]=%f\n",i,P[i]);
  165.   }
  166. /*获得第m个样本的数据*/
  167. return 1;
  168. }/*子程序input_P(m)结束*/

  169. /*/////////////////////////////////////////*/
  170. /*第m个样本教师信号子程序*/
  171. /*/////////////////////////////////////////*/
  172. input_T(int m)
  173. {int k;

  174. for(k=0;k  T[k]=Study_Data[m].teach[k];
  175. return 1;
  176. }/*子程序input_T(m)结束*/


  177. H_I_O()
  178. {
  179. double sigma;
  180. int i,j;
  181. for(j=0;j  {
  182.    sigma=0;
  183.    for(i=0;i    {sigma+=W[j][i]*P[i];/*求隐层内积*/
  184.     }

  185.    X[j]=sigma-YU_HN[i];/*求隐层净输入,为什么减隐层的阀值*/
  186.    H[j]=1.0/(1.0+exp(-X[j]));/*求隐层输出 siglon算法*/
  187.    }
  188. return 1;
  189. }/*子程序H_I_O()结束*/


  190. O_I_O()
  191. {int k;
  192. int j;
  193. double sigma;
  194. for(k=0;k {
  195.   sigma=0.0;
  196.   for(j=0;j  {
  197.    sigma+=V[k][j]*H[k];
  198.   }
  199. Y[k]=sigma-YU_ON[k];
  200. O[k]=1.0/(1.0+exp(-Y[k]));
  201. }
  202. return 1;
  203. }


  204. int Err_O_H(int m)
  205. {int k;
  206. double abs_err[ON];
  207. double sqr_err=0;
  208. for (k=0;k  {
  209.   abs_err[k]=T[k]-O[k];
  210.   sqr_err+=(abs_err[k])*(abs_err[k]);
  211.   d_err[k]=abs_err[k]*O[k]*(1.0-O[k]);
  212.   err_m[m]=sqr_err/2;
  213.   }
  214. return 1;
  215. }


  216. double e_err[HN];
  217. int Err_H_I()
  218. {
  219. int j,k;
  220. double sigma;
  221. for(j=0;j {
  222.   sigma=0.0;
  223.   for(k=0;k  {
  224.    sigma=d_err[k]*V[k][j];
  225.    }
  226. e_err[j]=sigma*H[j]*(1-H[j]);
  227. }
  228. return 1;
  229. }


  230. saveWV(int m)
  231. {int i;
  232. int ii;
  233. int j;
  234. int jj;
  235. for(i=0;i  {
  236.    for(j=0;j    {
  237.      Old_WV[m].old_W[i][j] = W[i][j];
  238.     }
  239.   }
  240. for(ii=0;ii  {
  241.    for(jj=0;jj    {
  242.      Old_WV[m].old_V[ii][jj] = V[ii][jj];
  243.     }
  244.   }
  245. return 1;
  246. }


  247. int Delta_O_H(int n)                 /*(int m,int n)*/
  248. {int k,j;
  249. if(n<1)  /*n<=1*/
  250.   {
  251.    for (k=0;k    {
  252.      for (j=0;j      {
  253.        V[k][j]=V[k][j]+a*d_err[k]*H[j];
  254.       }
  255.      YU_ON[k]+=a*d_err[k];
  256.     }
  257.   }
  258. else if(n>1)
  259.   {
  260.    for (k=0;k    {
  261.      for (j=0;j      {
  262.        V[k][j]=V[k][j]+a*d_err[k]*H[j]+alpha*(V[k][j]-Old_WV[(n-1)].old_V[k][j]);
  263.       }
  264.      YU_ON[k]+=a*d_err[k];
  265.     }
  266.   }
  267. return 1;
  268. }

  269. Delta_H_I(int n)               /*(int m,int n)*/
  270. { int i,j;

  271. if(n<=1)   /*n<=1*/
  272. {
  273.   for (j=0;j   {
  274.     for (i=0;i     {
  275.       W[j][i]=W[j][i]+b*e_err[j]*P[i];
  276.      }
  277.     YU_HN[j]+=b*e_err[j];
  278.    }
  279. }
  280. else if(n>1)
  281. {
  282.   for(j=0;j   {
  283.     for(i=0;i     {
  284.       W[j][i]=W[j][i]+b*e_err[j]*P[i]+alpha*(W[j][i]-Old_WV[(n-1)].old_W[j][i]);
  285.      }
  286.     YU_HN[j]+=b*e_err[j];
  287.    }
  288. }
  289. return 1;
  290. }


  291. double Err_Sum()
  292. {int m;
  293. double total_err=0;
  294. for(m=0;m {
  295.   total_err+=err_m[m];
  296. }
  297. return total_err;
  298. }


  299. void savequan()
  300. { int i,j,k;
  301.   int ii,jj,kk;

  302. if((fp=fopen("f:\\bp\\权值.txt","a"))==NULL)         /*save the result at f:\hsz\bpc\*.txt*/
  303. {
  304.   printf("Cannot open file strike any key exit!");
  305.   getch();
  306.   exit(1);
  307. }

  308. fprintf(fp,"Save the result of “权值”(quanzhi) as follows:\n");
  309. for(i=0;i {
  310.   for(j=0;j  fprintf(fp,"W[%d][%d]=%f\n",i,j,W[i][j]);
  311. }
  312. fprintf(fp,"\n");
  313. for(ii=0;ii {
  314.   for(jj=0;jj  fprintf(fp,"V[%d][%d]=%f\n",ii,jj,V[ii][jj]);
  315.   }
  316. fclose(fp);
  317. printf("\nThe result of “权值.txt”(quanzhi) has been saved successfully!\nPress any key to continue...");
  318. getch();


  319. if((fp=fopen("f:\\bp\\阈值.txt","a"))==NULL)         /*save the result at f:\hsz\bpc\*/
  320. {
  321.   printf("Cannot open file strike any key exit!");
  322.   getch();
  323.   exit(1);
  324. }
  325. fprintf(fp,"Save the result of “输出层的阈值”(huozhi) as follows:\n");
  326. for(k=0;k   fprintf(fp,"YU_ON[%d]=%f\n",k,YU_ON[k]);

  327. fprintf(fp,"\nSave the result of “隐层的阈值为”(huozhi) as follows:\n");
  328. for(kk=0;kk  fprintf(fp,"YU_HN[%d]=%f\n",kk,YU_HN[kk]);

  329. fclose(fp);
  330. printf("\nThe result of “阈值.txt”(huozhi) has been saved successfully!\nPress any key to continue...");
  331. getch();
  332. }

  333. /**********************/
  334. /**程序入口,即主程序**/
  335. /**********************/

  336. void main()
  337. {double Pre_error;
  338. double sum_err;
  339. int study;
  340. int flag;
  341. flag=30000;
  342. a=0.7;
  343. b=0.7;
  344. alpha=0.9;
  345. study=0;
  346. Pre_error=0.0001;/*实际值为Pre_error=0.0001;*/

  347. Start_Show();
  348. GetTrainingData();
  349. initial();

  350. do
  351. {int m;
  352.   ++study;
  353.   for(m=0;m   {
  354.     input_P(m);
  355.     input_T(m);
  356.     H_I_O();
  357.     O_I_O();
  358.     Err_O_H(m);
  359.     Err_H_I();
  360.     saveWV(m);           /****************/
  361.     Delta_O_H(m);                             /*(m,study)*/
  362.     Delta_H_I(m);                              /*(m,study)*/
  363.    }
  364.   sum_err=Err_Sum();
  365.   printf("sum_err=%f\n",sum_err);
  366.   printf("Pre_error=%f\n\n",Pre_error);

  367.   if(study>flag)
  368.    {
  369.     printf("\n*******************************\n");
  370.     printf("The program is ended by itself because of error!\nThe learning times is surpassed!\n");
  371.     printf("*****************************\n");
  372.     getch();
  373.     break;
  374.    }

  375. }while (sum_err>Pre_error);

  376. printf("\n****************\n");
  377. printf("\nThe program have studyed for [%d] times!\n",study);
  378. printf("\n****************\n");
  379. savequan();        /*save the results*/
  380. End_Show();
  381. }
复制代码

==========================

权值.txt

  1. {Save the result of “权值”(quanzhi) as follows:
  2. W[0][0]=0.350578
  3. W[0][1]=-1.008697
  4. W[0][2]=-0.962250
  5. W[1][0]=0.055661
  6. W[1][1]=-0.372367
  7. W[1][2]=-0.890795
  8. W[2][0]=0.129752
  9. W[2][1]=-0.332591
  10. W[2][2]=-0.521561

  11. V[0][0]=-2.932654
  12. V[0][1]=-3.720583
  13. V[0][2]=-2.648183
  14. V[1][0]=2.938970
  15. V[1][1]=1.633281
  16. V[1][2]=1.944077

  17. }
复制代码


阈值.txt

  1. {Save the result of “输出层的阈值”(huozhi) as follows:
  2. YU_ON[0]=-4.226843
  3. YU_ON[1]=1.501791

  4. Save the result of “隐层的阈值为”(huozhi) as follows:
  5. YU_HN[0]=-0.431459
  6. YU_HN[1]=0.452127
  7. YU_HN[2]=0.258449

  8. }
复制代码


==================================

以上程序为VC++的程序改制而成!

[ 本帖最后由 风花雪月 于 2006-11-29 07:53 编辑 ]

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2006-11-23 09:33 | 显示全部楼层
算法很简单,编写和调试程序却不太容易,楼主辛苦了。
发表于 2006-12-28 14:55 | 显示全部楼层

赞一个

向你学习
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-21 01:28 , Processed in 0.052884 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表