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