版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、转载:在<<C# BP神经网络 类与实例>>这篇文章中,笔者转载了网友提供的一个类。非常感谢这位网友的无私奉献,使笔者得以快速的完成了软件的一个功能。貌似我找到的是网友对原作改动过的版本,使得该类存在着一些不足,笔者对其进行了相应改进。1. 未保存训练样本的结果,使得每次使用时都要重新训练数据,耗费时间:虽然类中已经提供了保存w、v、b1、b2数组的方法,但其构造函数却有问题,同时没有保存in_rate inNum HideNum outNum 这些下面会用到的数据。对此笔者进行了以下改进:新建了一个无参的构造函数用来预测新样本时实例化对象;提供sa
2、veParas、readParas方法用于保存和读取这些参数;提供initial方法用来建立预测过程中用到的动态数组2. 类中用伪随机数来初始化w、v数组,使得每次建立新对象时w、v会不同。笔者对该算法没有深入研究,不清楚为什么这样设计。笔者把R = new Random(); 这行代码进行了修改R = new Random(32); /加了一个参数,使产生的伪随机序列相同下面是我修改后的类和示例:using System;using System.Collections.Generic;using System.Linq;using System.Text;using
3、System.IO;namespace BpANNet / <summary> / BpNet 的摘要说明。 / </summary> public class BpNet public int inNum;/输入节点数 int hideNum;/隐层节点数 public int outNum;/输出层节点数 public int sampleNum;/样本总数 Random R; double x;/输入节点的输入数据 double x1;/隐层节点的输出 double x2;/输出节点的输出 double o1;/隐层的输入 double o2;/输出层的输入 pu
4、blic double, w;/权值矩阵w public double, v;/权值矩阵V public double, dw;/权值矩阵w public double, dv;/权值矩阵V public double rate;/学习率 public double b1;/隐层阈值矩阵 public double b2;/输出层阈值矩阵 public double db1;/隐层阈值矩阵 public double db2;/输出层阈值矩阵 double pp;/输出层的误差 double qq;/隐层的误差 double yd;/输出层的教师数据 public double e;/均方误差
5、 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(32); /加了一个参
6、数,使产生的伪随机序列相同 this.inNum = p.GetLength(1); /数组第二维大小为 输入节点数 this.outNum = t.GetLength(1); /输出节点数 this.hideNum = computeHideNum(inNum, outNum); /隐藏节点数,不知其原理 this.sampleNum = p.GetLength(0); /数组第一维大小为 Console.WriteLine("输入节点数目: " + inNum); Console.WriteLine("隐层节点数目:" + hideNum); Con
7、sole.WriteLine("输出层节点数目:" + outNum); Console.ReadLine(); x = new doubleinNum; x1 = new doublehideNum; x2 = new doubleoutNum; o1 = new doublehideNum; o2 = new doubleoutNum; w = new doubleinNum, hideNum; v = new doublehideNum, outNum; dw = new doubleinNum, hideNum; dv = new doublehideNum, ou
8、tNum; b1 = new doublehideNum; b2 = new doubleoutNum; db1 = new doublehideNum; db2 = new doubleoutNum; pp = new doublehideNum; qq = new doubleoutNum; yd = new doubleoutNum; /初始化w for (int i = 0; i < inNum; i+) for (int j = 0; j < hideNum; j+) wi, j = (R.NextDouble() * 2 - 1.0) / 2; /初始化v for (i
9、nt 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; for (int isamp = 0; isamp < sampleNum; isamp+) for (int i = 0; i < i
10、nNum; 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 isamp for (int isamp = 0; isamp < sampleNum; isamp+) /数据归一化 for (int i = 0; i < inNum; i+) xi = pisamp
11、, 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; x1j = 1.0 / (1.0 + Math.Exp(-o1j - b1j); /计算输出层的输入和输出 for (int k = 0; k < outNum; k+) o2k = 0.0; for (int j
12、= 0; j < hideNum; j+) o2k += vj, k * x1j; x2k = 1.0 / (1.0 + Math.Exp(-o2k - b2k); /计算输出层误差和均方差 for (int k = 0; k < outNum; k+) qqk = (ydk - x2k) * x2k * (1.0 - x2k); e += (ydk - x2k) * (ydk - x2k); /更新V for (int j = 0; j < hideNum; j+) vj, k += rate * qqk * x1j; /计算隐层误差 for (int j = 0; j &
13、lt; hideNum; j+) ppj = 0.0; for (int k = 0; k < outNum; k+) ppj += qqk * vj, k; ppj = ppj * x1j * (1 - x1j); /更新W for (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; /e
14、nd isamp e = Math.Sqrt(e); / adjustWV(w,dw); / adjustWV(v,dv); /end train public 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+)
15、wi += dwi; public BpNet() /仿真函数 用的构造函数 /数据仿真函数 public double sim(double psim) /in_rate inNum HideNum outNum for (int i = 0; i < inNum; i+) xi = psimi / 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(-o1
16、j - 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,v public void saveMatrix(double, w, string filename) StreamWriter sw = new StreamWriter(filena
17、me); 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,b2 public void saveMatrix(double b, string filename) StreamWriter sw = new StreamWriter(filename); for (int i = 0; i < b.Length; i+) sw.W
18、rite(bi + " "); sw.Close(); /保存参数 in_rate inNum HideNum outNum public void saveParas(string filename) try StreamWriter sw = new StreamWriter(filename); string str = inNum.ToString() + " " + hideNum.ToString() + " " + outNum.ToString() + " " + in_rate.ToString(
19、); sw.WriteLine(str); sw.Close(); catch (Exception e) / Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); /读回参数 in_rate inNum HideNum outNum, tjt 预测新数据 public void readParas(string filename) StreamReader sr; try sr = new Stre
20、amReader(filename); String line; if(line = sr.ReadLine() != null) string strArr = line.Split(' '); this.inNum = Convert.ToInt32(strArr0); this.hideNum = Convert.ToInt32(strArr1); this.outNum = Convert.ToInt32(strArr2); this.in_rate = Convert.ToDouble(strArr3); sr.Close(); catch (Exception e)
21、 / Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); public void initial() / 建立一些中间数组 tjt 预测新数据 x = new doubleinNum; x1 = new doublehideNum; x2 = new doubleoutNum; o1 = new doublehideNum; o2 = new doubleoutNum; w = new double
22、inNum, hideNum; 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; /读取矩阵W,V public void rea
23、dMatrixW(double, w, string filename) StreamReader sr; try sr = new StreamReader(filename); 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)
24、 / Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); /读取矩阵b1,b2 public void readMatrixB(double b, string filename) StreamReader sr; try sr = new StreamReader(filename); String line; while (line = sr.ReadLine() != null) int i
25、= 0; string s1 = line.Trim().Split(' '); for (int j = 0; j < s1.Length; j+) bi = 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); /end bpnet /end namesp
26、aceusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace BpANNet / <summary> / Class1 的摘要说明。 / </summary> class Class1 static void Main(string args) double, p1 = new double, 0.1399, 0.1467, 0.1567, 0.1595, 0.1588, 0.1622 , 0.1467, 0.1567, 0.1595, 0.1
27、588, 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 ; double, t1 = new double, 0.1622 , 0.1611 , 0.1615 , 0.1685 , 0.1789 , 0.1790 ; BpNet bp = new BpNet(p1, t1); int study = 0; do study+; bp.train(p1, t1); 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.s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 协调性功能训练
- 2025事业单位招聘考试公共基础知识题库附答案详解(满分必刷)
- 焊接实训总结报告
- 电子签到入场形式介绍
- 新版光伏产业技能竞赛理论试题库及答案
- 手术中患者宣教
- 《指南录后序》课件
- 2025版转移性癌症常见症状及护理护航
- 2025年护理学基础知识考试试题库及答案(共610题)
- 肾内科慢性肾脏病进展监测
- 2025年秋沪科版八年级数学上册 第12章 函数与一次函数 综合测试卷(含答案)
- 2025年中小学生安全知识知识竞赛试题库及答案
- 2025年执业药师《中药学综合知识与技能》考试真题及答案解析
- 打印机基础知识课件
- 2025年广西公需真题卷及答案
- 医院拔牙后的护理常规
- 隐蔽工程报验管理办法
- 矿井电子围栏管理制度
- 产品批次追溯管理制度
- 甲亢教学课件
- 呼吸系统体格检查规范
评论
0/150
提交评论