📄 神经网络bp算法(c程序).htm
字号:
for(j=0;j<HN;J++)<BR /> {<BR> sigma+=V[k][j]*H[k];<BR>
}<BR> Y[k]=sigma-YU_ON[k];<BR> O[k]=1.0/(1.0+exp(-Y[k]));<BR> }<BR>return
1;<BR>}</P>
<P><BR>int Err_O_H(int m)<BR>{int k;<BR>double abs_err[ON];<BR>double
sqr_err=0;<BR>for (k=0;k<ON;K++)<BR /> {<BR>
abs_err[k]=T[k]-O[k];<BR> sqr_err+=(abs_err[k])*(abs_err[k]);<BR>
d_err[k]=abs_err[k]*O[k]*(1.0-O[k]);<BR> err_m[m]=sqr_err/2;<BR>
}<BR>return 1;<BR>}</P>
<P>
<P>double e_err[HN];<BR>int Err_H_I()<BR>{<BR> int j,k;<BR> double
sigma;<BR> for(j=0;j<HN;J++)<BR /> {<BR> sigma=0.0;<BR>
for(k=0;k<ON;K++)<BR /> {<BR>
sigma=d_err[k]*V[k][j];<BR>
}<BR> e_err[j]=sigma*H[j]*(1-H[j]);<BR> }<BR>return 1;<BR>}</P>
<P><BR>saveWV(int m)<BR>{int i;<BR> int ii;<BR> int j;<BR> int
jj;<BR> for(i=0;i<HN;I++)<BR /> {<BR>
for(j=0;j<IN;J++)<BR /> {<BR>
Old_WV[m].old_W[i][j] = W[i][j];<BR> }<BR>
}<BR> for(ii=0;ii<ON;II++)<BR /> {<BR>
for(jj=0;jj<HN;JJ++)<BR /> {<BR>
Old_WV[m].old_V[ii][jj] = V[ii][jj];<BR> }<BR>
}<BR>return 1;<BR>}</P>
<P><BR>int Delta_O_H(int
n)
/*(int m,int n)*/<BR>{int k,j;<BR> if(n<1) /*n<=1*/<BR>
{<BR> for (k=0;k<ON;K++)<BR />
{<BR> for (j=0;j<HN;J++)<BR
/> {<BR>
V[k][j]=V[k][j]+a*d_err[k]*H[j];<BR>
}<BR> YU_ON[k]+=a*d_err[k];<BR>
}<BR> }<BR> else if(n>1)<BR> {<BR> for
(k=0;k<ON;K++)<BR /> {<BR> for
(j=0;j<HN;J++)<BR />
{<BR>
V[k][j]=V[k][j]+a*d_err[k]*H[j]+alpha*(V[k][j]-Old_WV[(n-1)].old_V[k][j]);<BR>
}<BR> YU_ON[k]+=a*d_err[k];<BR>
}<BR> }<BR>return 1;<BR>}</P>
<P>Delta_H_I(int
n)
/*(int m,int n)*/<BR>{ int i,j;</P>
<P>if(n<=1) /*n<=1*/<BR> {<BR> for
(j=0;j<HN;J++)<BR /> {<BR> for (i=0;i<IN;I++)<BR
/> {<BR>
W[j][i]=W[j][i]+b*e_err[j]*P[i];<BR>
}<BR> YU_HN[j]+=b*e_err[j];<BR>
}<BR> }<BR>else if(n>1)<BR> {<BR> for(j=0;j<HN;J++)<BR
/> {<BR> for(i=0;i<IN;I++)<BR
/> {<BR>
W[j][i]=W[j][i]+b*e_err[j]*P[i]+alpha*(W[j][i]-Old_WV[(n-1)].old_W[j][i]);<BR>
}<BR> YU_HN[j]+=b*e_err[j];<BR>
}<BR> }<BR>return 1;<BR>}</P>
<P>
<P>double Err_Sum()<BR>{int m;<BR>double total_err=0;<BR>for(m=0;m<N;M++)<BR
/> {<BR> total_err+=err_m[m];<BR> }<BR>return
total_err;<BR>}</P>
<P>
<P>void savequan()<BR>{ int i,j,k;<BR> int ii,jj,kk;</P>
<P>if((fp=fopen("f:\\bp\\权值.txt","a"))==NULL)
/*save the result at f:\hsz\bpc\*.txt*/<BR> {<BR> printf("Cannot open
file strike any key exit!");<BR> getch();<BR>
exit(1);<BR> }</P>
<P>fprintf(fp,"Save the result of “权值”(quanzhi) as
follows:\n");<BR>for(i=0;i<HN;I++)<BR /> {<BR> for(j=0;j<IN;J++)<BR
/>
fprintf(fp,"W[%d][%d]=%f\n",i,j,W[i][j]);<BR> }<BR>fprintf(fp,"\n");<BR>for(ii=0;ii<ON;II++)<BR
/> {<BR> for(jj=0;jj<HN;JJ++)<BR />
fprintf(fp,"V[%d][%d]=%f\n",ii,jj,V[ii][jj]);<BR>
}<BR>fclose(fp);<BR>printf("\nThe result of “权值.txt”(quanzhi) has been saved
successfully!\nPress any key to continue...");<BR>getch();</P>
<P><BR>if((fp=fopen("f:\\bp\\阈值.txt","a"))==NULL)
/*save the result at f:\hsz\bpc\*/<BR> {<BR> printf("Cannot open file
strike any key exit!");<BR> getch();<BR>
exit(1);<BR> }<BR>fprintf(fp,"Save the result of “输出层的阈值”(huozhi) as
follows:\n");<BR> for(k=0;k<ON;K++)<BR />
fprintf(fp,"YU_ON[%d]=%f\n",k,YU_ON[k]);</P>
<P>fprintf(fp,"\nSave the result of “隐层的阈值为”(huozhi) as
follows:\n");<BR> for(kk=0;kk<HN;KK++)<BR />
fprintf(fp,"YU_HN[%d]=%f\n",kk,YU_HN[kk]);</P>
<P>fclose(fp);<BR>printf("\nThe result of “阈值.txt”(huozhi) has been saved
successfully!\nPress any key to continue...");<BR>getch();<BR>}</P>
<P>/**********************/<BR>/**程序入口,即主程序**/<BR>/**********************/</P>
<P>void main()<BR>{double Pre_error;<BR>double sum_err;<BR>int study;<BR>int
flag;<BR>flag=30000;<BR>a=0.7;<BR>b=0.7;<BR>alpha=0.9;<BR>study=0;<BR>Pre_error=0.0001;/*实际值为Pre_error=0.0001;*/</P>
<P>Start_Show();<BR>GetTrainingData();<BR>initial();</P>
<P>do<BR> {int m;<BR> ++study;<BR> for(m=0;m<N;M++)<BR
/> {<BR> input_P(m);<BR>
input_T(m);<BR> H_I_O();<BR>
O_I_O();<BR> Err_O_H(m);<BR>
Err_H_I();<BR>
saveWV(m);
/****************/<BR>
Delta_O_H(m);
/*(m,study)*/<BR>
Delta_H_I(m);
/*(m,study)*/<BR> }<BR> sum_err=Err_Sum();<BR>
printf("sum_err=%f\n",sum_err);<BR>
printf("Pre_error=%f\n\n",Pre_error);</P>
<P> if(study>flag)<BR> {<BR>
printf("\n*******************************\n");<BR> printf("The
program is ended by itself because of error!\nThe learning times is
surpassed!\n");<BR>
printf("*****************************\n");<BR>
getch();<BR> break;<BR> }</P>
<P> }while (sum_err>Pre_error);</P>
<P>printf("\n****************\n");<BR>printf("\nThe program have studyed for
[%d]
times!\n",study);<BR>printf("\n****************\n");<BR>savequan();
/*save the results*/<BR>End_Show();<BR>}</P>
<P>==========================</P>
<P>权值.txt</P>
<P>{Save the result of “权值”(quanzhi) as
follows:<BR>W[0][0]=0.350578<BR>W[0][1]=-1.008697<BR>W[0][2]=-0.962250<BR>W[1][0]=0.055661<BR>W[1][1]=-0.372367<BR>W[1][2]=-0.890795<BR>W[2][0]=0.129752<BR>W[2][1]=-0.332591<BR>W[2][2]=-0.521561</P>
<P>V[0][0]=-2.932654<BR>V[0][1]=-3.720583<BR>V[0][2]=-2.648183<BR>V[1][0]=2.938970<BR>V[1][1]=1.633281<BR>V[1][2]=1.944077</P>
<P>}</P>
<P>阈值.txt</P>
<P>{Save the result of “输出层的阈值”(huozhi) as
follows:<BR>YU_ON[0]=-4.226843<BR>YU_ON[1]=1.501791</P>
<P>Save the result of “隐层的阈值为”(huozhi) as
follows:<BR>YU_HN[0]=-0.431459<BR>YU_HN[1]=0.452127<BR>YU_HN[2]=0.258449</P>
<P>}</P>
<P>==================================</P>
<P>以上程序为VC++的程序改制而成!</P>
<P>欢迎提宝贵意见!</P>
<P>欢迎交流学习经验!</P>
<P><IMG alt=" " src="神经网络BP算法(C程序).files/mailaddr.gif"
onload="function anonymous() { function anonymous() { function anonymous() { function anonymous() { function anonymous() { img_auto_size(this,450,true); } } } } }"
align=baseline></P>
<P></P>
<P class=diaryFoot>【作者: <A
onclick="window.open('http://publishblog.blogchina.com/blog/postMessage.b?receiver=194821','发送短消息','width=520, height=455')"
href="javascript:void(0);">叶落随风</A>】【访问统计:
<SCRIPT language=JavaScript src="神经网络BP算法(C程序).files/PageServlet"></SCRIPT>
】【2006年04月3日 星期一 21:55】【 <A
href="javascript:void(keyit=window.open('http://blogmark.blogchina.com/jsp/key/quickaddkey.jsp?k='+encodeURI('神经网络BP算法(C程序)')+'&u='+encodeURI('http://hshu.blogchina.com/hshu/4806075.html')+'&c='+encodeURI(''),'keyit','scrollbars=no,width=500,height=430,status=no,resizable=yes'));keyit.focus();">加入博采</A>】【<A
href="javascript:window.print();">打印</A>】 </TD></P></DIV>
<DIV class=operation><A name=trackback>
<H3>Trackback</H3></A>
<P class=trackback>你可以使用这个链接引用该篇文章
http://publishblog.blogchina.com/blog/tb.b?diaryID=4806075 </P></DIV>
<DIV class=operation><A name=comment>
<H3>回复</H3></A>
<TABLE cellSpacing=0 cellPadding=0 width=700 border=0>
<TBODY></TBODY></TABLE></DIV>
<DIV class=operation>
<TABLE class=comment cellSpacing=0 cellPadding=0 width=700 border=0>
<FORM id=replyForm method=post><INPUT type=hidden value=193619 name=blogID>
<INPUT type=hidden value=4806075 name=diaryID> <INPUT type=hidden value=hshu
name=blogDomino>
<SCRIPT>
if(getCookie('userID') == null){
document.write('<tr><td width="70">发布人:</td>');
document.write('<td width="150"> <input name="remark.authorNameFUI" type="text" size="20" class="inputStyle" maxlength="20"></td>');
document.write('<td width="70">邮箱:</td>');
document.write('<td width="435"> <input name="remark.authorEmail" type="text" size="20" class="inputStyle" maxlength="40"></td>');
document.write('</tr><tr><td>主 页:</td>');
document.write('<td colspan="3"> <input name="remark.authorURL" type="text" class="inputStyle" value="HTTP://" size="63" maxlength="100"></td></tr>');
}else{
document.write('<input type="hidden" name="remark.authorNameFUI" value="Blogchina网友">');
}
</SCRIPT>
<TBODY>
<TR>
<TD width=70>验证码:</TD>
<TD><INPUT class=inputStyle maxLength=4 name=validateCode></TD>
<TD> <IMG src="神经网络BP算法(C程序).files/getValidateImg.gif"
border=0></TD></TR>
<TR align=left>
<TD colSpan=4>评论内容:<BR><TEXTAREA class=textStyle id=remark name=remark.remarkFUI rows=8 cols=60> </TEXTAREA>
</TD></TR>
<TR align=left>
<TD colSpan=4> <INPUT onclick=reply() type=button value=提交>
<INPUT type=reset value=重置> </TD></TR></FORM></TBODY></TABLE></DIV></DIV>
<SCRIPT src="神经网络BP算法(C程序).files/extend3.js" type=text/javascript></SCRIPT>
<DIV id=footer><A href="http://blog.bokee.com/">2003-2004 BOKEE.COM All rights
reserved</A><BR><A href="http://www.blogdriver.com/">Powered by BlogDriver
2.1</A> </DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -