人工神经网络框架AForge学习(一)
本示例演示了单层人工神经网络的学习过程.
示例中采用了16条样本数据,3个分类,在经过7-20次的迭代后,误差值与期望最小误差(示例中为0)的差距变为最小.
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5
6using AForge;
7using AForge.Neuro;
8using AForge.Neuro.Learning;
9using AForge.Controls;
10
11namespace AspxOn.AForgeNeuroTest
12{
13 class Program
14 {
15 static void Main(string[] args)
16 {
17 double[,] data = null;
18 int[] classes = null;
19
20 double[,] tempData = new double[200, 2];
21 int[] tempClass = new int[200];
22
23 double learnintRate = 0.1;
24
25 //最大和最小的X值
26 double minX = double.MaxValue;
27 double maxX = double.MinValue;
28
29 //样本数目统计
30 int samples = 0;
31 //分类数目统计
32 int classCount = 0;
33 int[] samplesPerClass = new int[10];
34
35 try
36 {
37 //准备训练样本数据
38 string s = "0.1,0.1,0;0.2,0.3,0;0.3,0.4,0;0.1,0.3,0;0.2,0.5,0;0.1,1,1;0.2,1.1,1;0.3,0.9,1;";
39 s+="0.4,0.8,1;0.2,0.9,1;1,0.4,2;0.9,0.5,2;0.8,0.6,2;0.9,0.4,2;1,0.5,2;1,0.3,2";
40 string[] ss = s.Split(';');
41
42 for (int i = 0; i < ss.Length; i++)
43 {
44 string str = ss[i];
45 string[] strs = str.Split(',');
46
47 //检查数组长度
48 if (strs.Length != 3)
49 throw new Exception("length error!");
50
51 //生成单词
52 tempData[samples, 0] = double.Parse(strs[0]);
53 tempData[samples, 1] = double.Parse(strs[1]);
54 //生成分类
55 tempClass[samples] = int.Parse(strs[2]);
56
57 //跳过分类如果分类值大于等于10
58 if (tempClass[samples] >= 10)
59 {
60 continue;
61 }
62
63 //计算不同分类的总数
64 if (tempClass[samples] >= classCount)
65 classCount += tempClass[samples] + 1;
66
67 //统计每个分类的样本数目
68 samplesPerClass[tempClass[samples]]++;
69
70 //寻找最小值
71 if (tempData[samples, 0] < minX)
72 minX = tempData[samples, 0];
73 //寻找最大值
74 if (tempData[samples, 0] > maxX)
75 maxX = tempData[samples, 0];
76
77 //样本数目累加
78 samples++;
79
80 }
81
82 data = new double[samples, 2];
83 Array.Copy(tempData, 0, data, 0, samples * 2);
84 classes = new int[samples];
85 Array.Copy(tempClass, 0, classes, 0, samples);
86
87
88 }
89 catch
90 {
91 throw new Exception("failed loading data!");
92 }
93
94 /*=============================开始训练=================================*/
95
96 //准备学习样本数据
97 double[][] input = new double[samples][];
98 double[][] output = new double[samples][];
99
100 for (int i = 0; i < samples; i++)
101 {
102 input[i] = new double[2];
103 output[i] = new double[classCount];
104
105 //设置输入值
106 input[i][0] = data[i, 0];
107 input[i][1] = data[i, 1];
108 //设置输出值
109 output[i][classes[i]] = 1;
110
111 }
112
113 //神经网络初始化,创建激活神经网络
114 ActivationNetwork network = new ActivationNetwork(new ThresholdFunction(), 2, classCount);
115 ActivationLayer layer = network[0];
116 //创建导师
117 PerceptronLearning teacher = new PerceptronLearning(network);
118 //设置学习比率
119 teacher.LearningRate = learnintRate;
120
121 //迭代次数
122 int iteration = 1;
123 //停止迭代标志
124 bool neetToStop = false;
125 //是否输出每次权重
126 bool showWeight = true;
127
128 try
129 {
130 //误差值
131 List<double> errors = new List<double>();
132
133 while (!neetToStop)
134 {
135 输出权重
148
149 //将本次误差添加到误差列表
150 double error = teacher.RunEpoch(input, output);
151 errors.Add(error);
152 //输出本次训练误差
153 Console.WriteLine("误差:" + error.ToString());
154 //输出当前迭代次数
155 Console.WriteLine("迭代次数:" + iteration);
156 Console.WriteLine("---------------------------------------------");
157
158 //如果误差等于0,停止训练
159 //此处可预设一个最小值,不一定非是0.当误差小于此最小值时,结束训练。
160 if (error == 0)
161 break;
162
163
164 iteration++;
165 }
166 }
167 catch
168 {
169 throw new Exception("error!");
170 }
171
172 /*=============================结束训练=================================*/
173
174 Console.ReadKey();
175 }
176
177 }
178}
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5
6using AForge;
7using AForge.Neuro;
8using AForge.Neuro.Learning;
9using AForge.Controls;
10
11namespace AspxOn.AForgeNeuroTest
12{
13 class Program
14 {
15 static void Main(string[] args)
16 {
17 double[,] data = null;
18 int[] classes = null;
19
20 double[,] tempData = new double[200, 2];
21 int[] tempClass = new int[200];
22
23 double learnintRate = 0.1;
24
25 //最大和最小的X值
26 double minX = double.MaxValue;
27 double maxX = double.MinValue;
28
29 //样本数目统计
30 int samples = 0;
31 //分类数目统计
32 int classCount = 0;
33 int[] samplesPerClass = new int[10];
34
35 try
36 {
37 //准备训练样本数据
38 string s = "0.1,0.1,0;0.2,0.3,0;0.3,0.4,0;0.1,0.3,0;0.2,0.5,0;0.1,1,1;0.2,1.1,1;0.3,0.9,1;";
39 s+="0.4,0.8,1;0.2,0.9,1;1,0.4,2;0.9,0.5,2;0.8,0.6,2;0.9,0.4,2;1,0.5,2;1,0.3,2";
40 string[] ss = s.Split(';');
41
42 for (int i = 0; i < ss.Length; i++)
43 {
44 string str = ss[i];
45 string[] strs = str.Split(',');
46
47 //检查数组长度
48 if (strs.Length != 3)
49 throw new Exception("length error!");
50
51 //生成单词
52 tempData[samples, 0] = double.Parse(strs[0]);
53 tempData[samples, 1] = double.Parse(strs[1]);
54 //生成分类
55 tempClass[samples] = int.Parse(strs[2]);
56
57 //跳过分类如果分类值大于等于10
58 if (tempClass[samples] >= 10)
59 {
60 continue;
61 }
62
63 //计算不同分类的总数
64 if (tempClass[samples] >= classCount)
65 classCount += tempClass[samples] + 1;
66
67 //统计每个分类的样本数目
68 samplesPerClass[tempClass[samples]]++;
69
70 //寻找最小值
71 if (tempData[samples, 0] < minX)
72 minX = tempData[samples, 0];
73 //寻找最大值
74 if (tempData[samples, 0] > maxX)
75 maxX = tempData[samples, 0];
76
77 //样本数目累加
78 samples++;
79
80 }
81
82 data = new double[samples, 2];
83 Array.Copy(tempData, 0, data, 0, samples * 2);
84 classes = new int[samples];
85 Array.Copy(tempClass, 0, classes, 0, samples);
86
87
88 }
89 catch
90 {
91 throw new Exception("failed loading data!");
92 }
93
94 /*=============================开始训练=================================*/
95
96 //准备学习样本数据
97 double[][] input = new double[samples][];
98 double[][] output = new double[samples][];
99
100 for (int i = 0; i < samples; i++)
101 {
102 input[i] = new double[2];
103 output[i] = new double[classCount];
104
105 //设置输入值
106 input[i][0] = data[i, 0];
107 input[i][1] = data[i, 1];
108 //设置输出值
109 output[i][classes[i]] = 1;
110
111 }
112
113 //神经网络初始化,创建激活神经网络
114 ActivationNetwork network = new ActivationNetwork(new ThresholdFunction(), 2, classCount);
115 ActivationLayer layer = network[0];
116 //创建导师
117 PerceptronLearning teacher = new PerceptronLearning(network);
118 //设置学习比率
119 teacher.LearningRate = learnintRate;
120
121 //迭代次数
122 int iteration = 1;
123 //停止迭代标志
124 bool neetToStop = false;
125 //是否输出每次权重
126 bool showWeight = true;
127
128 try
129 {
130 //误差值
131 List<double> errors = new List<double>();
132
133 while (!neetToStop)
134 {
135 输出权重
148
149 //将本次误差添加到误差列表
150 double error = teacher.RunEpoch(input, output);
151 errors.Add(error);
152 //输出本次训练误差
153 Console.WriteLine("误差:" + error.ToString());
154 //输出当前迭代次数
155 Console.WriteLine("迭代次数:" + iteration);
156 Console.WriteLine("---------------------------------------------");
157
158 //如果误差等于0,停止训练
159 //此处可预设一个最小值,不一定非是0.当误差小于此最小值时,结束训练。
160 if (error == 0)
161 break;
162
163
164 iteration++;
165 }
166 }
167 catch
168 {
169 throw new Exception("error!");
170 }
171
172 /*=============================结束训练=================================*/
173
174 Console.ReadKey();
175 }
176
177 }
178}
输出结果如下: