📄 神经网络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 + -