




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/*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; /*输出层的神经元个数*/double *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; /*前次输入层到隐藏层权值的改变*/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,double *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 fastcopy(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-自变量的值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-数组的列数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_randomize_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=alloc_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-hidden_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-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-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_hidden,int n_out)BNPP *newnet;newnet=bnpp_internal_create(n_in,n_hidden,n_out);#ifdef INITZERObnpp_zero_weights(newnet-input_weights,n_in,n_hidden);#elsebnpp_randomize_weights(newnet-input_weights,n_in,n_hidden);#endifbnpp_randomize_weights(newnet-hidden_weights,n_hidden,n_out);bnpp_zero_weights(newnet-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;/*设置阈值*/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;errsum=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,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_adjust_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,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-input_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_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-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,filename);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(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);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(i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年新零售背景下实体书店顾客体验升级研究报告
- 2025至2030年中国减肥药行业市场深度分析及未来发展趋势预测报告
- 解析卷北师大版9年级数学上册期末试题附答案详解【轻巧夺冠】
- 解析卷山东省乐陵市中考数学真题分类(位置与坐标)汇编章节测试试题(含答案解析)
- 解析卷人教版8年级数学下册《平行四边形》定向攻克试题(含详细解析)
- 2025版水利工程地质勘察合同范本
- 2025办公空间租赁合同(含装修及维护条款)
- 2025年度润滑油产品回收与再利用合同
- 2025年度专业图形设计电脑租赁合同范本
- 2025年度餐饮企业员工职业培训合同范本
- 构建专家委员会的初步方案
- DB37-T 5317-2025《旋挖成孔灌注桩施工技术规程》
- 装修公司全包装修合同
- (完整版)保安培训课件
- DB37/T 5132-2019 建筑机电工程抗震技术规程
- 个性化医疗决策模型-深度研究
- 2025-2030年中国汽车起重机市场前景规划及投资潜力分析报告
- Oracle财务系统应付账款模块操作手册
- 体检营销话术与技巧培训
- 泰山版(2025版)小学信息技术第5册教学计划
- 广东省佛山市顺德区2023-2024学年七年级(上)期末数学试卷(含答案)
评论
0/150
提交评论