ImageVerifierCode 换一换
格式:DOCX , 页数:18 ,大小:18.75KB ,
资源ID:8974627      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8974627.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(整理神经网络C++程序.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

整理神经网络C++程序.docx

1、整理神经网络C+程序每名环境影响评价工程师申请登记的类别不得超过2个。(2)安全验收评价。4.将环境影响价值纳入项目的经济分析/*backprop.h* */#ifndef_BACKPROP_H_#define_BACKPROP_H_#define BIGRND 0x7fffffff/*神经网络的数据结构。网络被假定为一个全连接的3层前向结构,每层的单元0是阈值,这意味着真正的神经元编号为1-n*/typedef structint input_n; /*输入层的神经元个数*/int hidden_n; /*隐含层的神经元个数*/int output_n; /*输出层的神经元个数*/doubl

2、e *input_units; /*输入层的神经元*/double *hidden_units; /*隐含层的神经元*/double *output_units; /*输出层的神经元*/double *hidden_delta; /*隐含层的误差*/double *output_delta; /*输出层的误差*/double *target; /*目标向量*/double *input_weights; /*输入层到隐藏层的连接权*/double *hidden_weights; /*隐藏层到输出层的连接权*/ /*下面两个在迭代时使用*/ double *input_prev_weights

3、; /*前次输入层到隐藏层权值的改变*/double *input_prev_weights; /*前次隐藏层到输出层权值的改变*/BPNN;/*用户接口*/*初始化随机数种子*/void bpnn_initialize(int seed);/*创建BP网络*/BPNN *bpnn_create(int n_in,int n_hidden,int n_out);/*释放BP网络所占地内存空间*/void bpnn_free(BPNN *net);/*训练BP网络*/void bpnn_train(BPNN *net,double eta,double momentum,double*eo,do

4、uble *eh);/*前向运算*/void bpnn_feedforward(BPNN *net);/*保存BP网络到文件中*/void bpnn_save(BPNN *net,char *filename);/*从文件中读取BP网络参数*/BPNN *bpnn_read(char *filename);#endif/*backprop.cpp*仅用于学习目的*/#include StdAfx.h#include #include backprop.h#include #include #define ABX(x) (x)0.0?(x):(-(x)/*宏定义:快速拷贝*/#define fa

5、stcopy(to,from,len) register char *_to,*from; register int _i,_l; _to=(char *)(to); _from=(char *)(from); _l=(len); for(_i=0;_i_l;_i+) *_to+=*_from+;/*返回01的双精度随机数*/double drnd() return(double) rand()/(double) BIGRND);/*返回-1.0到1.0之间的双精度随机数*/double dpn1() return(drnd()*2.0)-1.0);/*作用函数,目前是S型函数*/参数:x-自

6、变量的值double squash(double x)return(1.0/(1.0+exp(-x);/*申请1维双精度实数数组*/参数:n-数组的维数double *alloc_1d_dbl(int n) double *new1; new1=(double *)malloc (unsigned)(n*sizeof(double); if(new1=NULL) printf(ALLOC_1D_DBL:Couldnt allocate array of doublesn); return(NULL); return (new1);/*申请2维双精度实数数组*/参数:m-数组的行数/ n-数组的

7、列数double *alloc_2d_dbl(int m,int n) int i; double *new1; new1=(double *)malloc (unsigned)(m*sizeof(double); if(new1=NULL) printf(ALLOC_2D_DBL:Couldnt allocate array of dbl ptrsn); return(NULL); for(i=0;im;i+) new1i=alloc_1d_dbl(n); return (new1);/*随机初始化权值*/参数:w-保存权值的二级指针/ m-数组的行数/ n-数组的列数void bpnn_r

8、andomize_weights(double *w,int m,int n) int i,j; for(i=0;i=m;i+) for(j=0;j=n;j+) wij=dpn1(); /*0初始化权值*/参数:w-保存权值的二级指针/ m-数组的行数/ n-数组的列数void bpnn_zero_weights(double *w,int m,int n) int i,j; for(i=0;i=m;i+) for(j=0;jinput_n=n_in; newnet-hidden_n=n_hidden; newnet-output_n=n_out; newnet-input_units=all

9、oc_1d_dbl(n_in+1); newnet-hidden_units=alloc_1d_dbl(n_hidden+1); newnet-output_units=alloc_1d_dbl(n_out+1); newnet-hidden_delta=alloc_1d_dbl(n_hidden+1); newnet-output_delta=alloc_1d_dbl(n_out+1); newnet-target=alloc_1d_dbl(n_out+1); newnet-input_weights=alloc_2d_dbl(n_in+1,n_hidden+1); newnet-hidde

10、n_weights=alloc_2d_dbl(n_hidden+1,n_out+1); newnet-input_prev_weights=alloc_2d_dbl(n_in+1,n_hidden+1); newnet-hidden_prev_weights=alloc_2d_dbl(n_hidden+1,n_out+1); return(netnet);/*释放BP网络所占地内存空间*/参数:net-需要释放的内存地址void bpnn_free(BPNN *net) int n1,n2,i; n1=net-intput_n; n2=net-hidden_n; free(char *)net

11、-input_units); free(char *)net-hidden_units); free(char *)net-output_units); free(char *)net-hidden_delta); free(char *)net-output_delta); free(char *)net-target); for(i=0;i=n1;i+) free(char *)net-input_weightsi); free(char *)net-input_prev_weightsi); free(char *)net-input_weights); free(char *)net-

12、input_prev_weights); for(i=0;i=n2;i+) free(char *)net-hidden_weightsi); free(char *)net-hidden_prev_weightsi); free(char *)net-hidden_weights); free(char *)net-hidden_prev_weights); free(char *)net);/*创建一个BP网络,并初始化权值*/参数:n_in-输入层个数/ n_hidden-隐含层神经元个数/ n_out-输出层个数BNPP *bnpp_create(int n_in,int n_hidd

13、en,int n_out) BNPP *newnet; newnet=bnpp_internal_create(n_in,n_hidden,n_out);#ifdef INITZERO bnpp_zero_weights(newnet-input_weights,n_in,n_hidden);#else bnpp_randomize_weights(newnet-input_weights,n_in,n_hidden);#endif bnpp_randomize_weights(newnet-hidden_weights,n_hidden,n_out); bnpp_zero_weights(n

14、ewnet-input_prev_weights,n_in,n_hidden); bnpp_zero_weights(newnet-hidden_prev_weights,n_hidden,n_out); return(newnet);/*计算从前一层到后一层的输出*/参数:l1-前一层的神经元/ l2-后一层的神经元/ conn-连接权值/ n1-前一层的神经元个数/ n2-后一层的神经元个数void bpnn_layerforward(double *l1,double *l2,double *conn,int n1,int n2) double sum; int j,k; /*设置阈值*

15、/ l10=1.0;/*对于第二层的每个神经元*/ for(j=1;j=n2;j+) /*计算输入的加权总和*/ sum=0.0; for(k=0;k=n1;k+) sum+=connkj*l1k; l2j=squash(sum); /*输出误差*/参数:delta-误差/ target-目标数组/ output-实际输出数组/ nj-神经元个数/ err-误差综合void bpnn_output_error(double *delta,double *target,double *output,int nj,double *err) int j; double o,t,errsum; err

16、sum=0.0; for(j=1;j=nj;j+) o=outputj; t=targetj; deltaj=o*(1.0-o)*(t-o); errsum+=ABS(deltaj); *err=errsum; /*隐含层误差*/参数:delta_h-隐含层误差数组/ nh-隐含层神经元个数/ delta_0-输出层误差数组/ no-输出层神经元个数/ who-隐含层到输出层的连接权值/ hidden-隐含层的神经元/ err-总误差void bpnn_hidden_error(double *delta_h,int nh,double *delta_o,int no,double *who,

17、double *hidden,double *err) int j,k; double h,sum,errsum; errsum=0.0; for(j=1;j=nh;j+) h=hiddenj; sum=0.0; for(k=1;k=no;k+) sum+=delta_ok*whojk; delta_hj=h*(1.0-h)*sum; errsum+=ABS(delta_hj); *err=errsum;/*调整权值*/参数:delta-误差数组/ ndelta-数组长度/ w-新权值数组/ oldw-旧权值数组/ eta-学习速率/ momentum-学习动量因子void bpnn_adju

18、st_weights(double *delta,int ndelta,double *ly,int nly,double *w,double *oldw,double eta,double momentum) double new_dw; int k,j; ly0=1.0; for(j=1;j=ndelta;j+) for(k=0;kinput_n; hid=net-hidden_n; out=net-output_n; /*Feed forward input activations.*/ bpnn_layerforward(net-input_units,net-hidden_units

19、,net-input_weights,in,hid); bpnn_layerforward(net-hidden_units,net-output_units,net-hidden_weights,hid,out);/*训练BP网络*/参数:net-BP网/ eta-学习速率/ momentum-学习动量因子/ eo-输出层误差/ eh-隐含层误差void bpnn_train(BPNN *net,double eta,double momentum,double *eo,double *eh) int in,hid,out; double out_err,hid,err; in=net-in

20、put_n; hid=net-hidden_n; out=net-output_n; /*前向输入激活*/ bpnn_layerforward(net-input_units,net-hidden_units,net-input_weights,in,hid); bpnn_layerforward(net-hidden_units,net-output_units,net-hidden_weights,hid,out); /*计算隐含层和输出层误差*/ bpnn_output_error(net-output_delta,net-target,net-output_units,out,&out

21、_err); bpnn_hidden_error(net-hidden_delta,hid,net-output_delta,out,net-hidden_weights,net-hidden_units,&hid_err); *eo=out_err; *eh=hid_err; /*调整输入层和隐含层权值*/ bpnn_adjust_weights(net-output_delta,out,net_hidden_units,hid,net-hidden_weights,net-hidden_prev_weights,eta,momentum); bpnn_adjust_weights(net-

22、hidden_delta,hid,net_input_units,in,net-input_weights,net-input_prev_weights,eta,momentum);/*保存BP网络*/参数:net-待保存的网络/ filename-文件名void bpnn_save(BPNN *net,char *filename) int n1,n2,n3,i,j,memcnt; double dvalue, *w; char *mem; FILE *fd; if(fd=fopen(filename,w)=NULL) printf(BPNN_SAVE:Cannot creat %sn,fi

23、lename); return; n1=net-input_n;n2=net-hidden_n;n3=net-output_n; printf(Saving %dx%dx%dx network to %sn,n1,n2,n3,filename); fflush(stdout); fwrite(char *)&n1,sizeof(int),1,fd); fwrite(char *)&n2,sizeof(int),1,fd); fwrite(char *)&n3,sizeof(int),1,fd); memcnt=0; w=net-input_weights; mem=(char *)malloc

24、(unsigned)(n1+1)*(n2+1)*sizeof(double); for(i=0;i=n1;i+) for(j=0;jhidden_weights; mem=(char *)malloc(unsigned)(n2+1)*(n3+1)*sizeof(double); for(i=0;i=n2;i+) for(j=0;j=n3;j+) dvalue=wij; fastcopy(&memmemcnt,&dvalue,sizeof(double); memcnt+=sizeof(double); fwrite(mem,(n2+1)*(n3+1)*sizeof(double),1,fd);

25、 free(mem); fclose(fd); return;/*从文件中读取BP网络*/参数:filename-输入的文件名/返回:BP网络结构BPNN *bpnn_read(char *filename) char *mem; BPNN *new1; int n1,n2,n3,i,j,memcnt; FILE *fd; if (fd=fopen(filename,r)=NULL) return(NULL); printf(Reading%sn,filename);fflush(stdout); fread(char*)&n1,sizeof(int),1,fd); fread(char*)&n2,sizeof(int),1,fd); fread(char*)&n3,sizeof(int),1,fd); new1=bpnn_internal_create(n1,n2,n3); printf(%scontains a %dx%dx%dx networkn,filename,n1,n2,n3); pr

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1