欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    bp神经网络详细步骤C#实现.doc

    • 资源ID:34136021       资源大小:51.50KB        全文页数:13页
    • 资源格式: DOC        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    bp神经网络详细步骤C#实现.doc

    如有侵权,请联系网站删除,仅供学习与交流bp神经网络详细步骤C#实现【精品文档】第 13 页using System;using System.Collections.Generic;using System.Linq;using System.Text;using System;using System.IO;using System.Text;namespace BpANNet/ <summary>/ BpNet 的摘要说明。/ </summary>public class BpNetpublic int inNum;/输入节点数int hideNum;/隐层节点数public int outNum;/输出层节点数public int sampleNum;/样本总数Random R;double x;/输入节点的输入数据double x1;/隐层节点的输出double x2;/输出节点的输出double o1;/隐层的输入double o2;/输出层的输入public double , w;/权值矩阵w,这是输入层与隐藏层之间的权值矩阵public double , v;/权值矩阵V,这是隐藏层与输出层之间的权值矩阵public double , dw;/权值矩阵wpublic double , dv;/权值矩阵Vpublic double rate;/学习率public double b1;/隐层阈值矩阵public double b2;/输出层阈值矩阵public double db1;/隐层阈值矩阵public double db2;/输出层阈值矩阵double pp;/隐藏层的误差double qq;/输出层的误差double yd;/输出层的教师数据,所谓教师数据就是实际数据而已!public double e;/均方误差double in_rate;/归一化比例系数/用于确定隐藏层的神经细胞数public int computeHideNum(int m,int n)double s=Math.Sqrt(0.43*m*n+0.12*n*n+2.54*m+0.77*n+0.35)+0.51;int ss=Convert.ToInt32(s);return (s-(double)ss)>0.5) ? ss+1:ss;public BpNet(double , p,double , t)/ 构造函数逻辑R=new Random();this.inNum=p.GetLength(1);this.outNum=t.GetLength(1);this.hideNum=computeHideNum(inNum,outNum);/this.hideNum=18;this.sampleNum=p.GetLength(0);Console.WriteLine("输入节点数目: "+inNum);Console.WriteLine("隐层节点数目:"+hideNum);Console.WriteLine("输出层节点数目:"+outNum);Console.ReadLine();/将这些矩阵规定好矩阵大小x=new doubleinNum;x1=new doublehideNum;x2=new doubleoutNum;o1=new doublehideNum;o2=new doubleoutNum;w = new doubleinNum, hideNum;/权值矩阵w,这是输入层与隐藏层之间的权值矩阵v=new doublehideNum,outNum;dw=new doubleinNum,hideNum;dv=new doublehideNum,outNum;/阈值b1=new doublehideNum;b2=new doubleoutNum;db1=new doublehideNum;db2=new doubleoutNum;/误差pp = new doublehideNum;/隐藏层的误差qq = new doubleoutNum;/输出层的误差yd = new doubleoutNum;/输出层的教师数据/初始化wfor(int i=0;i<inNum;i+)for(int j=0;j<hideNum;j+)/NextDouble返回一个介于 0.0 和 1.0 之间的随机数。wi,j=(R.NextDouble()*2-1.0)/2;/初始化vfor(int i=0;i<hideNum;i+)for(int j=0;j<outNum;j+)vi,j=(R.NextDouble()*2-1.0)/2;rate=0.8;e=0.0;in_rate=1.0; /训练函数public void train(double , p,double , t)e=0.0;/求p,t中的最大值double pMax=0.0;/sampleNum为样本总数for(int isamp=0;isamp<sampleNum;isamp+)/inNum是输入层的节点数(即神经细胞数)for(int i=0;i<inNum;i+)if(Math.Abs(pisamp,i)>pMax)pMax=Math.Abs(pisamp,i);for(int j=0;j<outNum;j+)if(Math.Abs(tisamp,j)>pMax)pMax=Math.Abs(tisamp,j);in_rate=pMax;/end isampfor(int isamp=0;isamp<sampleNum;isamp+)/数据归一化for(int i=0;i<inNum;i+)xi=pisamp,i/in_rate;for(int i=0;i<outNum;i+)ydi=tisamp,i/in_rate;/计算隐层的输入和输出for(int j=0;j<hideNum;j+)o1j=0.0;for(int i=0;i<inNum;i+)o1j+=wi,j*xi;/“权值”*“输入”的那个累加的过程/这个b1j就是隐藏层的阈值,阈值就是一个输入为“-1”的累加值x1j=1.0/(1.0+Math.Exp(-o1j-b1j);/计算输出层的输入和输出for(int k=0;k<outNum;k+)o2k=0.0;for(int j=0;j<hideNum;j+)o2k+=vj,k*x1j;x2k=1.0/(1.0+Math.Exp(-o2k-b2k);/计算输出层误差和均方差for(int k=0;k<outNum;k+)/ydk是输出层的教师数据,所谓教师数据就是实际应该输出的数据而已qqk=(ydk-x2k)*x2k*(1.0-x2k);e+=(ydk-x2k)*(ydk-x2k);/更新V,V矩阵是隐藏层与输出层之间的权值for(int j=0;j<hideNum;j+)vj,k+=rate*qqk*x1j;/计算隐层误差for(int j=0;j<hideNum;j+)/PP矩阵是隐藏层的误差ppj=0.0;/算法参考我的视频截图for(int k=0;k<outNum;k+)ppj+=qqk*vj,k;ppj=ppj*x1j*(1-x1j);/更新Wfor(int i=0;i<inNum;i+)wi,j+=rate*ppj*xi;/更新b2,输出层的阈值for(int k=0;k<outNum;k+)b2k+=rate*qqk;/更新b1,隐藏层的阈值for(int j=0;j<hideNum;j+)b1j+=rate*ppj;/end isampe=Math.Sqrt(e);/均方差/ adjustWV(w,dw);/ adjustWV(v,dv);/end trainpublic void adjustWV(double , w,double, dw)for(int i=0;i<w.GetLength(0);i+)for(int j=0;j<w.GetLength(1);j+)wi,j+=dwi,j;public void adjustWV(double w,double dw)for(int i=0;i<w.Length;i+)wi+=dwi;/数据仿真函数public double sim(double psim)for(int i=0;i<inNum;i+)xi= psimi/in_rate;/in_rate为归一化系数for(int j=0;j<hideNum;j+)o1j=0.0;for(int i=0;i<inNum;i+)o1j=o1j+wi,j*xi;x1j=1.0/(1.0+Math.Exp(-o1j-b1j);for(int k=0;k<outNum;k+)o2k=0.0;for(int j=0;j<hideNum;j+)o2k=o2k+vj,k*x1j;x2k=1.0/(1.0+Math.Exp(-o2k-b2k);x2k=in_rate*x2k; return x2; /end sim/保存矩阵w,vpublic void saveMatrix(double , w,string filename)StreamWriter sw=File.CreateText(filename);for(int i=0;i<w.GetLength(0);i+)for(int j=0;j<w.GetLength(1);j+)sw.Write(wi,j+" ");sw.WriteLine();sw.Close();/保存矩阵b1,b2public void saveMatrix(double b,string filename)StreamWriter sw=File.CreateText(filename);for(int i=0;i<b.Length;i+)sw.Write(bi+" ");sw.Close();/读取矩阵W,Vpublic void readMatrixW(double , w,string filename)StreamReader sr;try sr = new StreamReader(filename,Encoding.GetEncoding("gb2312"); String line;int i=0;while (line = sr.ReadLine() != null) string s1=line.Trim().Split(' ');for(int j=0;j<s1.Length;j+)wi,j=Convert.ToDouble(s1j);i+;sr.Close();catch (Exception e) / Let the user know what went wrong.Console.WriteLine("The file could not be read:");Console.WriteLine(e.Message);/读取矩阵b1,b2public void readMatrixB(double b,string filename)StreamReader sr;try  sr = new StreamReader(filename,Encoding.GetEncoding("gb2312"); String line;int i=0; while (line = sr.ReadLine() != null) bi=Convert.ToDouble(line);i+;sr.Close();catch (Exception e) / Let the user know what went wrong.Console.WriteLine("The file could not be read:");Console.WriteLine(e.Message); /end bpnet /end namespace/主调用程序namespace BpANNet/ <summary>/ Class1 的摘要说明。/ </summary>class Class1/ <summary>/ 应用程序的主入口点。/ </summary>STAThreadstatic void Main(string args)/0.1399,0.1467,0.1567,0.1595,0.1588,0.1622,0.1611,0.1615,0.1685,0.1789,0.1790/ double , p1=new double,0.05,0.02,0.09,0.11,0.12,0.20,0.15,0.22,0.20,0.25,0.75,0.75,0.80,0.83,0.82,0.80,0.90,0.89,0.95,0.89,0.09,0.04,0.1,0.1,0.14,0.21,0.18,0.24,0.22,0.28,0.77,0.78,0.79,0.81,0.84,0.82,0.94,0.93,0.98,0.99;/ double , t1=new double,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1;/p1是输入的信息,一共5组,输入层为六个节点,p156double , p1=new double,0.1399,0.1467,0.1567,0.1595,0.1588,0.1622,0.1467,0.1567,0.1595,0.1588,0.1622,0.1611,0.1567,0.1595,0.1588,0.1622,0.1611,0.1615,0.1595,0.1588,0.1622,0.1611,0.1615,0.1685,0.1588,0.1622,0.1611,0.1615,0.1685,0.1789;/t1是输出信息,一共6组,t161double , t1=new double,0.1622,0.1611,0.1615,0.1685,0.1789,0.1790;BpNet bp=new BpNet(p1,t1);int study=0;dostudy+;bp.train(p1,t1);/ bp.rate=0.95-(0.95-0.3)*study/50000;/ Console.Write("第 "+ study+"次学习: ");/ Console.WriteLine(" 均方差为 "+bp.e);while(bp.e>0.001 && study <50000);Console.Write("第 "+ study+"次学习: ");Console.WriteLine(" 均方差为 "+bp.e);bp.saveMatrix(bp.w,"w.txt");bp.saveMatrix(bp.v,"v.txt");bp.saveMatrix(bp.b1,"b1.txt");bp.saveMatrix(bp.b2,"b2.txt");/ double , p2=new double,0.05,0.02,0.09,0.11,0.12,0.20,0.15,0.22,0.20,0.25,0.75,0.75,0.80,0.83,0.82,0.80,0.90,0.89,0.95,0.89,0.09,0.04,0.1,0.1,0.14,0.21,0.18,0.24,0.22,0.28,0.77,0.78,0.79,0.81,0.84,0.82,0.94,0.93,0.98,0.99;double , p2=new double,0.1399,0.1467,0.1567,0.1595,0.1588,0.1622,0.1622,0.1611,0.1615,0.1685,0.1789,0.1790;int aa=bp.inNum;int bb=bp.outNum;int cc=p2.GetLength(0);double p21=new doubleaa;double t2=new doublebb;for(int n=0;n<cc;n+)for(int i=0;i<aa;i+)p21i=p2n,i;t2=bp.sim(p21);for(int i=0;i<t2.Length;i+)Console.WriteLine(t2i+" ");Console.ReadLine(); 

    注意事项

    本文(bp神经网络详细步骤C#实现.doc)为本站会员(豆****)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开