本示例演示了单层人工神经网络的学习过程.
示例中采用了16条样本数据,3个分类,在经过7-20次的迭代后,误差值与期望最小误差(示例中为0)的差距变为最小.
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
System.IO;
5
6
using
AForge;
7
using
AForge.Neuro;
8
using
AForge.Neuro.Learning;
9
using
AForge.Controls;
10
11
namespace
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
输出权重#region 输出权重
136
if (showWeight)
137
{
138
for (int i = 0; i < classCount; i++)
139
{
140
Console.WriteLine("neuron" + i + ";");
141
Console.WriteLine(layer[i][0] + ";");
142
Console.WriteLine(layer[i][1] + ";");
143
Console.WriteLine(layer[i].Threshold.ToString());
144
Console.WriteLine("================================");
145
}
146
}
147
#endregion
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
}

2

3

4

5

6

7

8

9

10

11

12



13

14



15

16



17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36



37

38

39

40

41

42

43



44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59



60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90



91

92

93

94


95

96

97

98

99

100

101



102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129



130

131

132

133

134



135


136

137



138

139



140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168



169

170

171

172


173

174

175

176

177

178

输出结果如下: