?? 神經網絡bp算法(c程序).htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0038)http://hshu.blogchina.com/4806075.html -->
<HTML><HEAD><TITLE>神經網絡BP算法(C程序)-->岸< Harbor</TITLE>
<META http-equiv=Content-Type content="text/html; charset=GBK">
<META http-equiv=Pragma content=no-cache>
<META http-equiv=Cache-Control content=no-cache>
<META http-equiv=Expires content=0>
<META
content="趕著蝸牛上街神經網絡BP算法(C程序)2006年十大瘋狂預言 博客 博客中國 博客動力 blog blogdriver blogger 中國"
name=description>
<META
content=">岸< Harbor 趕著蝸牛上街神經網絡BP算法(C程序)2006年十大瘋狂預言 博客 博客中國 博客動力 blog blogdriver blogger 中國"
name=keywords><LINK href="神經網絡BP算法(C程序).files/diary.css" type=text/css
rel=stylesheet>
<SCRIPT language=JavaScript src="神經網絡BP算法(C程序).files/UBB.js"></SCRIPT>
<SCRIPT src="神經網絡BP算法(C程序).files/blog.js" type=text/javascript></SCRIPT>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY>
<DIV id=container>
<DIV id=header>
<H1 class=title><A href="http://hshu.blogchina.com/index.html">>岸<
Harbor</A></H1></DIV>
<DIV id=category><A title=上一篇
href="http://hshu.blogchina.com/4717910.html">趕著蝸牛上街</A>- -| <A
href="http://hshu.blogchina.com/index.html">回首頁</A> | <A
href="http://hshu.blogchina.com/catalog_2006.html">2006年索引</A> | - -<A title=下一篇
href="http://hshu.blogchina.com/4821351.html">2006年十大瘋狂預言</A></DIV>
<DIV class=entity>
<H2
class=diaryTitle>神經網絡BP算法(C程序)</H2>
<P>
<P>文件輸入輸出目錄為:F:\BP\</P>
<P>訓練樣本文件名:訓練樣本.txt</P>
<P>值為:</P>
<P>1<BR>1<BR>-1<BR>1<BR>-1<BR>1<BR>0<BR>1<BR>0<BR>1</P>
<P>輸出文件名為:閾值.txt 權值.txt</P>
<P>=========================</P>
<P>#include "stdlib.h"<BR>#include "math.h"<BR>#include "conio.h"<BR>#include
"stdio.h"<BR>#define N 2 /*/學習樣本個數*/<BR>#define IN 3 /*/輸入層神經元數目*/<BR>#define HN
3 /*/隱層神經元數目*/<BR>#define ON 2 /*/輸出層神經元數目*/<BR>#define Z 20
/*/舊權值保存-》每次study的權值都保存下來*/<BR>double P[IN]; /*/單個樣本輸入數據*/<BR>double T[ON];
/*/單個樣本教師數據*/<BR>double W[HN][IN]; /*/輸入層至隱層權值*/<BR>double V[ON][HN];
/*/隱層至輸出層權值*/<BR>double X[HN]; /*/隱層的輸入*/<BR>double Y[ON]; /*/輸出層的輸入*/<BR>double
H[HN]; /*/隱層的輸出*/<BR>double O[ON]; /*/輸出層的輸出*/<BR>double YU_HN[HN];
/*/隱層的閾值*/<BR>double YU_ON[ON]; /*/輸出層的閾值*/<BR>double err_m[N];
/*/第m個樣本的總誤差*/<BR>double a; /*/輸出層至隱層的學習效率*/<BR>double b;
/*/隱層至輸入層學習效率*/<BR>double alpha; /*/動量因子,改進型bp算法使用*/<BR>double
d_err[ON];</P>
<P>FILE *fp;<BR>/*定義一個放學習樣本的結構*/<BR>struct {<BR>double input[IN];<BR>double
teach[ON];<BR>}Study_Data[N];</P>
<P>/*改進型bp算法用來保存每次計算的權值*/<BR>struct {<BR>double old_W[HN][IN];<BR>double
old_V[ON][HN];<BR>}Old_WV[Z];</P>
<P><BR>int
Start_Show()<BR>{<BR>clrscr();<BR>printf("\n
***********************\n");<BR>printf("
* Welcome to use
*\n");<BR>printf("
* this program of
*\n");<BR>printf("
* calculating the BP
*\n");<BR>printf("
*
model!
*\n");<BR>printf("
* Happy every day!
*\n");<BR>printf("
***********************\n");<BR>printf("\n\nBefore starting,please read the
follows carefully:\n\n");<BR>printf(" 1.Please ensure the Path
of the '訓練樣本.txt'(xunlianyangben.txt) is \ncorrect,like
'F:\BP\訓練樣本.txt'!\n");<BR>printf(" 2.The calculating results
will be saved in the Path of 'F:\\BP\\'!\n");<BR>printf("
3.The program will load 10 datas when running from
'F:\\BP\\訓練樣本.txt'!\n");<BR>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");<BR>printf("\n\n\n");<BR>printf("Now press any key to
start...\n");<BR>getch();<BR>getch();<BR>clrscr();<BR>}</P>
<P>int
End_Show()<BR>{<BR>printf("\n\n---------------------------------------------------\n");<BR>printf("The
program has reached the end successfully!\n\nPress any key to
exit!\n\n");<BR>printf("\n
***********************\n");<BR>printf("
* This is the end
*\n");<BR>printf("
* of the program
which*\n");<BR>printf("
* can calculate the
BP*\n");<BR>printf("
*
model!
*\n");<BR>printf("
***********************\n");<BR>printf("
* Thanks for using!
*\n");<BR>printf("
* Happy every day!
*\n");<BR>printf("
***********************\n");<BR>getch();<BR>exit(0);<BR>}</P>
<P>GetTrainingData() /*OK*/<BR>{ int
m,i,j;<BR> int datr;</P>
<P>if((fp=fopen("f:\\bp\\訓練樣本.txt","r"))==NULL)
/*讀取訓練樣本*/<BR> {<BR> printf("Cannot open file strike any key
exit!");<BR> getch();<BR> exit(1);<BR> }</P>
<P>m=0;<BR>i=0;<BR>j=0;<BR>while(fscanf(fp,"%d",&datr)!=EOF)<BR> {j++;<BR>
if(j<=(N*IN))<BR> {if(i<IN)<BR />
{<BR>
Study_Data[m].input[i]=datr;<BR> /*printf("\nthe
Study_Datat[%d].input[%d]=%f\n",m,i,Study_Data[m].input[i]);getch();*/
/*use to check the loaded training datas*/<BR>
}<BR>
if(m==(N-1)&&i==(IN-1))<BR>
{<BR>
m=0;<BR>
i=-1;<BR> }<BR>
if(i==(IN-1))<BR>
{<BR>
m++;<BR>
i=-1;<BR> }<BR> }<BR> else
if((N*IN)<J&&J<=(N*(IN+ON)))<BR /> {if(i<ON)<BR
/>
{Study_Data[m].teach[i]=datr;<BR>
/*printf("\nThe
Study_Data[%d].teach[%d]=%f",m,i,Study_Data[m].teach[i]);getch();*/ /*use
to check the loaded training datas*/<BR>
}<BR>
if(m==(N-1)&&i==(ON-1))<BR>
printf("\n");</P>
<P> if(i==(ON-1))<BR>
{m++;<BR>
i=-1;<BR> }<BR> }<BR>
i++;<BR> }<BR>fclose(fp);<BR>printf("\nThere are [%d] datats that have been
loaded successfully!\n",j);</P>
<P><BR>/*show the data which has been loaded!*/<BR>printf("\nShow the data which
has been loaded as follows:\n");<BR>for(m=0;m<N;M++)<BR
/> {for(i=0;i<IN;I++)<BR />
{printf("\nStudy_Data[%d].input[%d]=%f",m,i,Study_Data[m].input[i]);<BR>
}<BR> for(j=0;j<ON;J++)<BR />
{printf("\nStudy_Data[%d].teach[%d]=%f",m,j,Study_Data[m].teach[j]);<BR>
}<BR> }<BR>printf("\n\nPress any key to start
calculating...");<BR>getch();<BR> return 1;<BR>}</P>
<P><BR>/*///////////////////////////////////*/<BR>/*初始化權、閾值子程序*/<BR>/*///////////////////////////////////*/<BR>initial()<BR>{int
i;<BR> int ii;<BR> int j;<BR> int jj;<BR> int
k;<BR> int kk;<BR>/*隱層權、閾值初始化*/</P>
<P> for(i=0;i<HN;I++)<BR /> {<BR> for(j=1;j<IN;J++)<BR
/> {W[i][j]=(double)((rand()/32767.0)*2-1); /*初始化輸入層到隱層的權值,隨機模擬0 和 1
-1 */<BR>
printf("w[%d][%d]=%f\n",i,j,W[i][j]);<BR> }<BR>
}<BR> for(ii=0;ii<ON;II++)<BR /> {<BR> for(jj=0;jj<HN;JJ++)<BR
/> {V[ii][jj]= (double)((rand()/32767.0)*2-1); /*初始化隱層到輸出層的權值,隨機模擬0
和 1 -1*/<BR>
printf("V[%d][%d]=%f\n",ii,jj,V[ii][jj]);<BR> }<BR>
}<BR> for(k=0;k<HN;K++)<BR /> {<BR> YU_HN[k] =
(double)((rand()/32767.0)*2-1); /*隱層閾值初始化 ,-0.01 ~ 0.01 之間*/<BR>
printf("YU_HN[%d]=%f\n",k,YU_HN[k]);<BR>
}<BR> for(kk=0;kk<ON;KK++)<BR /> {<BR> YU_ON[kk] =
(double)((rand()/32767.0)*2-1); /*輸出層閾值初始化 ,-0.01 ~ 0.01 之間*/<BR>
}<BR> return 1;<BR>}/*子程序initial()結束*/</P>
<P><BR>/*//////////////////////////////////////////*/<BR>/*第m個學習樣本輸入子程序*/<BR>/*/////////////////////////////////////////*/<BR>input_P(int
m)<BR>{ int i,j;</P>
<P> for(i=0;i<IN;I++)<BR />
{P[i]=Study_Data[m].input[i];<BR>
printf("P[%d]=%f\n",i,P[i]);<BR> }<BR>/*獲得第m個樣本的數據*/<BR>return
1;<BR>}/*子程序input_P(m)結束*/</P>
<P>/*/////////////////////////////////////////*/<BR>/*第m個樣本教師信號子程序*/<BR>/*/////////////////////////////////////////*/<BR>input_T(int
m)<BR>{int k;</P>
<P> for(k=0;k<ON;K++)<BR /> T[k]=Study_Data[m].teach[k];<BR>return
1;<BR>}/*子程序input_T(m)結束*/</P>
<P><BR>H_I_O()<BR>{<BR> double sigma;<BR> int
i,j;<BR> for(j=0;j<HN;J++)<BR /> {<BR>
sigma=0;<BR> for(i=0;i<IN;I++)<BR />
{sigma+=W[j][i]*P[i];/*求隱層內積*/<BR> }</P>
<P> X[j]=sigma-YU_HN[i];/*求隱層凈輸入,為什么減隱層的閥值*/<BR>
H[j]=1.0/(1.0+exp(-X[j]));/*求隱層輸出 siglon算法*/<BR> }<BR>return
1;<BR>}/*子程序H_I_O()結束*/</P>
<P>
<P>O_I_O()<BR>{int k;<BR> int j;<BR> double
sigma;<BR> for(k=0;k<ON;K++)<BR /> {<BR> sigma=0.0;<BR>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -