神經(jīng)網(wǎng)絡(luò)BP算法程序C語言.doc_第1頁
神經(jīng)網(wǎng)絡(luò)BP算法程序C語言.doc_第2頁
神經(jīng)網(wǎng)絡(luò)BP算法程序C語言.doc_第3頁
神經(jīng)網(wǎng)絡(luò)BP算法程序C語言.doc_第4頁
神經(jīng)網(wǎng)絡(luò)BP算法程序C語言.doc_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、神經(jīng)網(wǎng)絡(luò)BP算法(C程序) 文件輸入輸出目錄為:F:BP訓(xùn)練樣本文件名:訓(xùn)練樣本。txt值為:1 1 1 1 -1 1 0 1 0 1輸出文件名為:閾值。txt    權(quán)值.txtinclude ”stdlib。h"include ”math.h"#include "conio。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

2、 /*舊權(quán)值保存,每次study的權(quán)值都保存下來*/double PIN; /單個(gè)樣本輸入數(shù)據(jù)/double TON; /單個(gè)樣本輸出數(shù)據(jù)*/double WHNIN; /*/輸入層至隱層權(quán)值/double VONHN; /隱層至輸出層權(quán)值*/double XHN; /隱層的輸入*/double YON; /輸出層的輸入/double HHN; /*/隱層的輸出/double OON; /*/輸出層的輸出*/double YU_HNHN; /隱層的閾值/double YU_ONON; /*/輸出層的閾值*/double err mN; /*/第m個(gè)樣本的總誤差*/double a; /輸出層至

3、隱層的學(xué)習(xí)效率/double b; /*/隱層至輸入層學(xué)習(xí)效率*/double alpha;  /動(dòng)量因子,改進(jìn)型bp算法使用*/double d errON;文檔為個(gè)人收集整理,來源于網(wǎng)絡(luò)文檔為個(gè)人收集整理,來源于網(wǎng)絡(luò)FILE fp;/*定義一個(gè)放學(xué)習(xí)樣本的結(jié)構(gòu)*/struct double inputIN;double teachON;Study_DataN;/改進(jìn)型bp算法用來保存每次計(jì)算的權(quán)值*/struct double old_WHNIN;double old_VONHN;Old_WVZ;顯示開始界面int Start_Show()clrscr();printf(&quo

4、t;n                       *n");printf("                       * &

5、#160;  Welcome to use   *n");printf("                         this program of    n”);printf(”        

6、0;                calculating the BP *n”);printf(”                             model! 

7、0;       n");printf(”                       *   Happy every day!  n");printf(”         

8、60;             *n”);printf("nn Before starting, please read the follows carefully:nn”);printf("    1。Please ensure the Path of the 訓(xùn)練樣本。txt(xunlianyangben。txt) is n correct, like 'F:BP訓(xùn)練樣本.txt!n");printf(&

9、quot;    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

10、times。n And surpassing the number, the program will be ended by itself inn preventing running infinitely because of error!n");printf(”nnn”);printf(”Now press any key to start。n”);getch();getch();clrscr();文檔為個(gè)人收集整理,來源于網(wǎng)絡(luò)文檔為個(gè)人收集整理,來源于網(wǎng)絡(luò)顯示結(jié)束界面int End_Show()printf("nn-n”);printf("The prog

11、ram has reached the end successfully!nn Press any key to exit!nn");printf(”n                       *n");printf("          

12、60;                This is the end  *n”);printf(”                       of the program which*n”);printf(" 

13、;                      can calculate the BPn”);printf(”                        &

14、#160;    model!         n");printf("                       *n");printf(”        

15、                 Thanks for using!  n”);printf("                       *   Happy every day!

16、0; *n”);printf("                       *n”);getch();exit(0);文檔為個(gè)人收集整理,來源于網(wǎng)絡(luò)個(gè)人收集整理,勿做商業(yè)用途獲取訓(xùn)練樣本GetTrainingData()      /OK/ int m,i,j;  int datr;if(fp=fopen(

17、"f:bp訓(xùn)練樣本.txt”,”r”))=NULL)         /*讀取訓(xùn)練樣本*/    printf(”Cannot open file and strike any key exit!”);  getch();  exit(1);  m=0;i=0;j=0;while(fscanf(fp,"%d”,&datr)!=EOF)  j+;  if(j=(NIN)) /N為學(xué)習(xí)樣本個(gè)數(shù);IN為輸入層神經(jīng)元數(shù)目/&#

18、160;  if(iIN)           Study_Datam。inputi=datr;      /printf(”nthe Study_Datat%d.inputd=%fn”,m,i,Study_Datam.inputi);getch();/  /use to check the loaded training datas*/         

19、if(m=(N1)&&i=(IN1)             m=0;       i=-1;          if(i=(IN1)             m+;     

20、;  i=1;            else if(NIN)<J&&J=(N(IN+ON))    if(iON)      Study_Datam.teachi=datr;       /printf(”nThe Study_Data%d。teach%d=%f”,m,i,Study_Datam.teachi);getch();*/&

21、#160; /*use to check the loaded training datas/            if(m=(N1)i=(ON-1))      printf(”n”);個(gè)人收集整理,勿做商業(yè)用途本文為互聯(lián)網(wǎng)收集,請勿用作商業(yè)用途     if(i=(ON-1)      m+;     

22、  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;mN;M+) for(i=0;

23、iIN;I+)   printf(”nStudy_Datad。inputd=%f",m,i,Study_Datam。inputi);     for(j=0;jON;J+)   printf("nStudy_Data%d.teach%d=f",m,j,Study_Datam。teachj);    printf("nnPress any key to start calculating。.");getch(); return 1;/初始

24、化權(quán)、閾值子程序/*/initial()int i; int ii; int j; int jj; int k; int kk;/隱層權(quán)、閾值初始化/ for(i=0;iHN;i+)   for(j=1;jIN;j+)   Wij=(double)((rand()/32767。0)*2-1); /*初始化輸入層到隱層的權(quán)值,隨機(jī)模擬0 和 1 -1 /    printf(”wdd=fn”,i,j,Wij);      for

25、(ii=0;ii<ON;II+)   for(jj=0;jj<HN;JJ+)   Viijj= (double)(rand()/32767。0)*2-1); /初始化隱層到輸出層的權(quán)值,隨機(jī)模擬0 和 1 1*/    printf("Vd%d=%fn",ii,jj,Viijj);      for(k=0;k<HN;K+)   YU_HNk = (double)((rand()/32767.0)*21);  /隱層閾

26、值初始化 ,0。01 0.01 之間*/  printf(”YU_HN%d=fn”,k,YU_HNk);   for(kk=0;kk<ON;KK+)   YU_ONkk = (double)((rand()/32767。0)21); /輸出層閾值初始化 ,-0。01 0。01 之間*/    return 1;/*子程序initial()結(jié)束/文檔為個(gè)人收集整理,來源于網(wǎng)絡(luò)本文為互聯(lián)網(wǎng)收集,請勿用作商業(yè)用途/*/*/*第m個(gè)學(xué)習(xí)樣本輸入子程序*/*/input_P(int m) int i,j;  for(i

27、=0;i<IN;I+)  Pi=Study_Datam.inputi;   printf(”Pd=%fn",i,Pi);  /*獲得第m個(gè)樣本的數(shù)據(jù)*/return 1;/子程序input_P(m)結(jié)束*/*/*第m個(gè)樣本教師信號子程序/*/input_T(int m)int k; for(k=0;kON;k+)  Tk=Study_Datam.teachk;return 1;/*子程序input_T(m)結(jié)束*/H_I_O() double sigma; int i,j; for(j=0

28、;j<HN;j+)     sigma=0;   for(i=0;i<IN;i+)    sigma+=Wji*Pi;/*求隱層內(nèi)積/       Xj=sigma-YU_HNi;/*求隱層凈輸入,為什么減隱層的閥值*/   Hj=1.0/(1。0+exp(-Xj));/*求隱層輸出 siglon算法*/   return 1;/*子程序H_I_O()結(jié)束/O_I_O()int k; int j;

29、0;double sigma; for(k=0;k<ON;k+)   sigma=0。0;  for(j=0;j<HN;j+)     sigma+=VkjHk;   Yk=sigmaYU_ONk; Ok=1.0/(1。0+exp(-Yk); return 1;int Err_O_H(int m)int k;double abs_errON;double sqr_err=0;for (k=0;k<ON;k+)    abs_errk=Tk-Ok;&

30、#160; sqr_err+=(abs_errk)(abs_errk);  d_errk=abs_errkOk*(1。0Ok);  err_mm=sqr_err/2;  return 1;double e_errHN;int Err_H_I() int j,k; double sigma; for(j=0;jHN;j+)   sigma=0。0;  for(k=0;kON;k+)     sigma+=d_errk*Vkj;    e_errj=

31、sigma*Hj*(1-Hj); return 1;saveWV(int m)int i; int ii; int j; int jj; for(i=0;iHN;i+)     for(j=0;jIN;j+)         Old_WVm.old_Wij = Wij;       for(ii=0;iiON;ii+)     for(jj=0;jjHN;jj+)&#

32、160;        Old_WVm。old_Viijj = Viijj;      return 1;int Delta_O_H(int n)                 /*(int m,int n)*/int k,j; if(n<1)  /n<=1/     for

33、 (k=0;k<ON;k+)         for (j=0;jHN;j+)             Vkj=Vkj+a*d_errkHj;           YU_ONk+=ad_errk;       else if(n>1)   

34、0; for (k=0;kON;k+)         for (j=0;j<HN;j+)             Vkj=Vkj+ad_errkHj+alpha(Vkj-Old_WV(n-1)。old_Vkj);           YU_ONk+=a*d_errk;      r

35、eturn 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+)          

36、 Wji=Wji+be_errj*Pi;         YU_HNj+=be_errj;    else if(n>1)   for(j=0;j<HN;j+)       for(i=0;i<IN;i+)           Wji=Wji+b*e_errj*Pi+alpha*(WjiOld_WV(n-1).old_W

37、ji);         YU_HNj+=b*e_errj;    return 1;double Err_Sum()int m;double total_err=0;for(m=0;mN;m+)   total_err+=err_mm; return total_err;void savequan() int i,j,k;  int ii,jj,kk;if((fp=fopen(”f:bp權(quán)值.txt”,”a”))=NULL)  

38、60;      /save the result at f:hszbpc。txt/   printf(”Cannot open file strike any key exit!”);  getch();  exit(1); fprintf(fp,"Save the result of “權(quán)值”(quanzhi) as follows:n");for(i=0;iHN;i+)   for(j=0;jIN;j+)  fprintf(fp,"

39、;W%dd=fn”,i,j,Wij); fprintf(fp,”n”);for(ii=0;iiON;ii+)   for(jj=0;jj<HN;jj+)  fprintf(fp,"V%dd=%fn",ii,jj,Viijj);  fclose(fp);printf(”nThe result of “權(quán)值。txt”(quanzhi) has been saved successfully!nPress any key to continue.。.”);getch();if((fp=fopen(”f:bp閾值.txt&qu

40、ot;,"a"))=NULL)         /save the result at f:hszbpc/   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+)&#

41、160;  fprintf(fp,"YU_ONd=fn",k,YU_ONk);fprintf(fp,"nSave the result of “隱層的閾值為”(huozhi) as follows:n”); for(kk=0;kkHN;KK+)  fprintf(fp,"YU_HN%d=fn",kk,YU_HNkk);fclose(fp);printf("nThe result of “閾值.txt”(huozhi) has been saved successfully!nPress any key t

42、o 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;/實(shí)際值為Pre_error=0.0001;*/Start_Show(); /調(diào)用函數(shù),顯示開始界面*/GetTrainingData();initial ();do int m;  +study;  for(m=0;mN;m+) 

43、60;     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);   

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論