【vc++精品源码栏目提醒】:网学会员为广大网友收集整理了,[分享]BP神经网络VC源代码 - 讲义教程,希望对大家有所帮助!
分享BP神经网络VC源代码 include ltconio.hgt include ltmath.hgt include ltstdlib.hgt include ltstdio.hgt define Beta 1.0 define NAME_MAX 80 double bpw_ihw_hopatttargnnhhppmmfinfileW resultFcritiontotalsa double w_ih/ weight from input to hidden / double w_ho/ weight from hidden to output / double patt/ input patterns / double targ/ desired outputs / intnn/ input dim / int hh/ hidden dim / int pp/ output dim / int mm/ training patterns number / intfin/ flag for terminate / charfileW/ weightFile / charresultF/ resultFile Name / doublecrition/ minimum mean square / int total int sa/ flag for saving weights / double output int ijsnum_pnum_dmismis_trmis_te double err char MsgNAME_MAXMsg1NAME_MAXMsg2NAME_MAX Msg3NAME_MAXMsg4NAME_MAXMsg5NAME_MAX FILE fp FILE cor extern int maximumdouble int //extern void drawbpdouble double intintint void feedwarddouble double double double intintint double errSqudouble double int outputdouble callocppsizeofdouble err0.0 for i0iltmmi feedwardw_ihw_hopattoutputnnhhpp err errSquoutputtargpp / errsqrterr/ppmm mean-square-root / errerr/ppmm / mean-square / if errltcritionsa1 / draw bp / //drawbpw_ihw_honnhhpp if errltcrition fin1 if fpfopenfileWquotwtquotNULL fprintfstderrquotcannot open s file for writingnquotfileW exit1 sprintfMsgquotweight from input to hiddenquot fprintffpquotsnquotMsg for j0jlthhj for s0sltnn1s fprintffpquotftquotw_ihjs fprintffpquotnquot sprintfMsgquotweight from hidden to outputquot fprintffpquotsnquotMsg for j0jltppj for s0slthh1s fprintffpquotftquotw_hojs fprintffpquotnquot fclosefp if corfopenresultFquotwtquotNULL fprintfstderrquotcannot open s for writingnquotresultF exit1 sprintfMsgquotIndexquot sprintfMsg1quotClass_Getquot sprintfMsg2quotClass_Desquot sprintfMsg3quotOut_1quot sprintfMsg4quotOut_2quot sprintfMsg5quotOut_3quot fprintfcorquot10s 10s 10s 10s 10s 10snquotMsgMsg1Msg2Msg3Msg4Msg5 mis0 mis_tr0 mis_te0 for i0ilttotali feedwardw_ihw_hopattoutputnnhhpp num_pmaximumoutputpp num_dmaximumtargpp if num_pnum_d if iltmm mis_tr else mis_te mis fprintfcorquot10.3d 10.1d 10.1dquotinum_pnum_d for j0jltppj fprintfcorquot10.6fquotoutputj fprintfcorquotnquot fprintfcorquotTotal number of misclassfication: dnquotmis fprintfcorquot for traing: dt for test: dnquotmis_trmis_te fclosecor gotoxy572 printfquotError:6.6fquoterr gotoxy573 printfquottraing:3.3d test:3.3dquotmis_trmis_te freeoutput returnerr void feedwardwihwhoinoutn1h1p1 double wih/ weight from input to hidden / double who/ weight from hidden to output / double in/ input vector / double out/ output vector / intn1/ input dim / int h1/ hidden dim / int p1/ output dim / int ij double sum double out_h double sigmoiddouble out_hdouble calloch1sizeofdouble / calculate the outputs of hidden neurons / for i0ilth1i sum0.0 for j0jltn1j sum wihjinj sum wihn1 out_hsigmoidsum / calculate the network output / for i0iltp1i sum 0.0 for j0jlth1j sum whojout_hj sum whoh1 outsigmoidsum freeout_h double errSquoutale double ou/ practical output / double ta/ desired output / int le/ output dim / int i double difsum sum0.0 for i0iltlei difou-ta sum difdif returnsum double sigmoidx double x xtanhxBeta1.0/2 returnx -------------------------------------------------------------------------------- BP算法中的权值和阈值的调整程序 include quotiostream.hquot include quotiomanip.hquot define N 20 //学习样本个数 define IN 1 //输入层神经元数目 define HN 8 //隐层神经元数目 define ON 1 //输出层神经元数目 double PIN //单个样本输入数据 double TON //单个样本教师数据 double WHNIN //输入层至隐层权值 double VONHN //隐层至输出层权值 double XHN //隐层的输入 double YON //输出层的输入 double HHN //隐层的输出 double OON //输出层的输出 double sitaHN //隐层的阈值 double gamaON //输出层的阈值 double err_mN //第m个样本的总误差 double alpha //输出层至隐层的学习效率 double beta //隐层至输入层学习效率 //定义一个放学习样本的结构 struct double inputIN double teachON Study_DataNIN /////////////////////////// //初始化权、阈值子程序///// /////////////////////////// initial float sgn float rnd int ij //隐层权、阈值初始化// //sgnpow-1random100 sgnrand rndsgnrand100 Wj rnd/100//隐层权值初始化。上一篇:Telnet服务器VC2010代码