forked from Alexflex/HNNC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlpTrain.m
223 lines (185 loc) · 7.96 KB
/
mlpTrain.m
1
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
function [net, errors] = mlpTrain(net, dataSets)
% Îáó÷åíèå ÌÑÏ
%
% [net, errors] = mlpTrain(net, dataSets)
%
% Arguments
% net - ÌÑÏ
% dataSet - ñòðóêòóðà, îïèñûâàþùàÿ îáó÷àþùåå/òåñòîâîå/êîíòðîëüíîå ìí-âî
%
% net - îáó÷åííûé ÌÑÏ
% errors - ñòðóêòóðà, îïèñûâàþùàÿ ïðîöåññ îáó÷åíèÿ ÌÑÏ
%
% Example
% [net, errors] = mlpTrain(net, dataSets);
%
% See also
%
% Revisions
% Author: Vulfin Alex, Date: 17/11/2010
% Supervisor: Vulfin Alex, Date: 17/11/2010
% Author: (Next revision author), Date: (Next revision date)
% Èíèöèàëèçàöèÿ îáó÷åíèÿ
k = 1; % ñ÷åò÷èê ýïîõ îáó÷åíèÿ
alldone = false; % ôëàã çàâåðøåíèÿ îáó÷åíèÿ
tic; % òàéìåð íà÷àëà îáó÷åíèÿ
% ñîõðàíåíèå ïðîöåññà îáó÷åíèÿ
errors.test.RE = [];
errors.test.CE = [];
errors.test.NE = [];
errors.test.CR = [];
errors.training.RE = [];
errors.training.CE = [];
errors.training.NE = [];
errors.training.CR = [];
errors.validation.RE = [];
errors.validation.CE = [];
errors.validation.NE = [];
errors.validation.CR = [];
% Ïåðåìåøèâàíèå ïðèìåðîâ â êàæäîì íåïóñòîì ìíîæåñòâå
if(dataSets.hasValidation)
dataSets.validation.index = randperm(dataSets.validation.count);
end
if(dataSets.hasTest)
dataSets.test.index = randperm(dataSets.test.count);
end
dataSets.training.index = randperm(dataSets.training.count);
% ========================================================================
% Öèêë ïî ýïîõàì îáó÷åíèÿ
% ========================================================================
while ~alldone
% Ïåðåìåøèâàíèå ïðèìåðîâ â êàæäîì íåïóñòîì ìíîæåñòâå
if(dataSets.hasValidation && mod(k, net.countShuffleSet) == 0)
dataSets.validation.index = randperm(dataSets.validation.count);
end
if(dataSets.hasTest && mod(k, net.countShuffleSet) == 0)
dataSets.test.index = randperm(dataSets.test.count);
end
if(mod(k, net.countShuffleSet) == 0)
dataSets.training.index = randperm(dataSets.training.count);
end
% Öèêë ïî îáó÷àþùèì ïðèìåðàì
for p = 1:dataSets.training.count
% Ïîëó÷èòü ïðèìåð èç îáó÷àþùåãî ìíîæåñòâà
[input, output, class] = mlpDsGetExample(dataSets.training, p, false);
% Ïðÿìîé ïðîõîä è îøèáêà îáó÷åíèÿ äëÿ ïîñëåäíåãî ñëîÿ
net = mlpEval(net, input, output, class);
% Îáðàòíûé ïðîõîä: ðàñ÷åò ëîêàëüíûõ ãðàäèåíòîâ
net = mlpCalcLocalG(net, input);
% Îáðàòíûé ïðîõîä - êîððåêòèðîâêà âåñîâ
net = mlpCorrectWeights(net);
end
% îöåíêà íîðìû ãðàäèåíòà ïî ñëîÿì
dnorm = mlpEstimateG(net);
% îöåíêà âûõîäîâ ñåòè ïîñëå îäíîãî öèêëà êîððåêòèðîâêè âåñîâ íà
% îáó÷àþùåì ìíîæåñòâå
[net, errors.training.RE(k), errors.training.CE(k), ...
errors.training.NE(k), errors.training.CR(k)] = ...
mlpEvalSet(net, dataSets.training);
% îöåíêà âûõîäîâ ñåòè ïîñëå îäíîãî öèêëà êîððåêòèðîâêè âåñîâ íà
% òåñòîâîì ìíîæåñòâå
if(dataSets.hasTest)
[net, errors.test.RE(k), errors.test.CE(k), ...
errors.test.NE(k), errors.test.CR(k)] = ...
mlpEvalSet(net, dataSets.test);
end
% îöåíêà âûõîäîâ ñåòè ïîñëå îäíîãî öèêëà êîððåêòèðîâêè âåñîâ íà
% êîíòðîëüíîì ìíîæåñòâå
if(dataSets.hasValidation)
[net, errors.validation.RE(k), errors.validation.CE(k), ...
errors.validation.NE(k), errors.validation.CR(k)] = ...
mlpEvalSet(net, dataSets.validation);
end
% âûâîä ëîãà îáó÷åíèÿ ñåòè è ñîõðàíåíèå ñàìîé ñåòè
if(mod(k, net.printLog) == 0)
printMessage('\n\tEpochs: %g', k);
printMessage('\n\t\tTraining:\tre(ce) = %-10g (%-10g) | numErrors = %-5g | correct = %-5.2f', ...
errors.training.RE(k), errors.training.CE(k), ...
errors.training.NE(k), errors.training.CR(k));
if(dataSets.hasTest)
printMessage('\n\t\tTest:\t\tre(ce) = %-10g (%-10g) | numErrors = %-5g | correct = %-5.2f', ...
errors.test.RE(k), errors.test.CE(k), ...
errors.test.NE(k), errors.test.CR(k));
end
if(dataSets.hasValidation)
printMessage('\n\t\tValidation:\tre(ce) = %-10g (%-10g) | numErrors = %-5g | correct = %-5.2f', ...
errors.validation.RE(k), errors.validation.CE(k), ...
errors.validation.NE(k), errors.validation.CR(k));
end
end
% óñëîâèå äîñðî÷íîãî âûõîäà èç öèêëà ïî äîñòèæåíèè ïîðîãà íà
% êîíòðîëüíîì ìíîæåñòâå.
% Ïîñòðîåíèå ëèíåéíîé àïïðîêñèìàöèè ïîñëåäíèõ net.numEstimatedEpoch
% çíà÷åíèé îøèáêè íà ïðîâåðî÷íîì ìíîæåñòâå
if(dataSets.hasValidation && k > net.numEstimatedEpoch)
x = k - net.numEstimatedEpoch:1:k;
y = errors.validation.RE(k - net.numEstimatedEpoch:k);
pol = polyfit(x, y, 1);
% íàêëîí ïðÿìîé, àïïðîêñèìèðóþùåé ïîñëåäíèå lenEstimatedEpoch òî÷åê
if(pol(1) > 0)
net.countValidationFail = net.countValidationFail + 1;
% åñëè âîçðàñòàíèå îøèáêè íå ÿâëÿåòñÿ òåíäåöèåé, òî îáíóëèòü
% ñ÷åò÷èê îøèáîê. Åñëè æå îøèáêè èäóò ïîäðÿä, òî ïî ïðåâûøåíèè
% ïîðîãà maxValidationFail îáó÷åíèå ïðåêðàòèòñÿ
elseif ((pol(1) < 0) && ...
(net.countValidationFail > 0))
net.countValidationFail = 0;
printMessage('\n\tÎáíóëåí ñ÷åò÷èê ïðîâàëîâ íà ïðîâåðî÷íîì ìíîæåñòâå');
end
end
% ïðîâåðêà êðèòåðèåâ îñòàíîâà ïðîöåññà îáó÷åíèÿ
if(k > net.numEpoch)
alldone = true;
printMessage('\n\tÄîñòèãíóòî ìàêñèìàëüíîå êîëè÷åñòâî ýïîõ');
end
if(dnorm < net.minGradValue)
alldone = true;
printMessage('\n\tÄîñòèãíóòî ìèíèìàëüíîå çíà÷åíèå ãðàäèåíòà');
end
if(net.countValidationFail > net.maxValidationFail)
alldone = true;
printMessage('\n\tÐàííèé îñòàíîâ íà ïðîâåðî÷íîì ìíîæåñòâå');
end
if(dataSets.hasValidation && ...
errors.validation.RE(k) < net.validationStopThreshold)
alldone = true;
printMessage('\n\tÄîñòèãíóòî ìèíèìàëüíîå çíà÷åíèå íà ïðîâåðî÷íîì ìíîæåñòâå');
end
if(errors.training.RE(k) < net.goal)
alldone = true;
printMessage('\n\tÄîñòèãíóòî ìèíèìàëüíîå çíà÷åíèå öåëåâîé ôóêíöèè íà îáó÷àþùåì ìíîæåñòâå');
end
% íàðàùèâàåì ñ÷åò÷èê ýïîõ
k = k + 1;
end
% ========================================================================
% îöåíêà âûõîäîâ ñåòè ïîñëå îáó÷åíèÿ
[net, errors.training.regression, errors.training.classification, ...
errors.training.numErrors, errors.training.correct] = ...
mlpEvalSet(net, dataSets.training);
if(dataSets.hasValidation)
[net, errors.validation.regression, errors.validation.classification, ...
errors.validation.numErrors, errors.validation.correct] = ...
mlpEvalSet(net, dataSets.validation);
end
if(dataSets.hasTest)
[net, errors.test.regression, errors.test.classification, ...
errors.test.numErrors, errors.test.correct] = ...
mlpEvalSet(net, dataSets.test);
end
% âûâîä äëèòåëüíîñòè ïðîöåññà îáó÷åíèÿ è îøèáêè íà ìíîæåñòâàõ
printMessage('\n\tTraining completed: %g s', toc);
printMessage('\n\tTraining:\tre(ce) = %-10g (%-10g) | numErrors = %-5g | correct = %-5.2f', ...
errors.training.regression, errors.training.classification, ...
errors.training.numErrors, errors.training.correct);
if(dataSets.hasValidation)
printMessage('\n\tValidation:\tre(ce) = %-10g (%-10g) | numErrors = %-5g | correct = %-5.2f', ...
errors.validation.regression, errors.validation.classification, ...
errors.validation.numErrors, errors.validation.correct);
end
if(dataSets.hasTest)
printMessage('\n\tTest:\t\tre(ce) = %-10g (%-10g) | numErrors = %-5g | correct = %-5.2f', ...
errors.test.regression, errors.test.classification, ...
errors.test.numErrors, errors.test.correct);
end
end % of function