-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrain.m
More file actions
281 lines (212 loc) · 9.28 KB
/
Copy pathTrain.m
File metadata and controls
281 lines (212 loc) · 9.28 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
% Train.m
% This class is used to train the network.
% first, clear all junk.
close all;
clear;
clc;
% the fundamental principle of the training is to run a feed-forward update
% and then apply backpropagation. (for each line of sample).
% A total iteration through all ~700 total records = 1 epoch.
rawData = load('rawdata.dt'); % first load the dataset:
maxRecords = size(rawData,1); % we know max number of records in the data is:
trainLimit = 350;
validationLimit=175;
testLimit=174;
% section the different data sets. OR pick randomly? probably better.
%trainingSet = rawData(1:trainLimit, 1:11);
%validationSet = rawData(trainLimit+1:trainLimit+validationLimit, 1:11);
%testSet = rawData(trainLimit+validationLimit+1:trainLimit+validationLimit+testLimit, 1:11);
% test the effect of learning rate.
for nHidNeuron = 6:6 % for no hidden neurons.
acc = figure;
SetGlobalHidden(nHidNeuron);
maxEpochs = 30; % max Epochs to run.
epochError = zeros(1, maxEpochs);
avgTestScoreAtEpoch = zeros(1, maxEpochs);
accuracyAtEpoch = zeros(1, maxEpochs);
validationError = zeros(1, maxEpochs);
initialError = 0;
NN1 = NeuralNetwork(9,nHidNeuron,2); % create network.
NN1.SetLearnRate(0.02);
fprintf('== Initial Network ==\n');
NN1.debugLayer(2);
% before we run lets start timer.
tTicker = tic;
boolValidationBelowThreshold = false;
if nHidNeuron > 4
validationThreshold = 0.02; % only detect minima once we go lower than this.
else
validationThreshold = 0.07; %0.9;
end
for nEpoch = 0:maxEpochs
sumE = 0;
sumV = 0;
randomRecord = 0;
randomValidation = 0;
for nRecord = 1:trainLimit
%randomRecord = rawData(round(RangedRandom(1,trainLimit)),1:11);
randomRecord = rawData(nRecord,1:11);
if nEpoch > 0
% for every record feed forward and back-prop.
NN1.Update(randomRecord); % forward update
NN1.BackPropagate(randomRecord); % back-prop
end
[ sqrs1, sqrs2 ] = NN1.getSquareErrors(randomRecord);
% calc square errors after backprop.
% instead lets get the average amount of square error per epoch
% sum the errors.
%sqrE1(nEpoch, nRecord) = sqrs1;
%sqrE2(nEpoch, nRecord) = sqrs1;
sumE = sumE + ( sqrs1 + sqrs2 );
end
% after each epoch, we must also calculate the error for the validation
% samples.
for nValidationRecord = 1:validationLimit
%randomValidation = rawData(round(RangedRandom(trainLimit+1,trainLimit+validationLimit)),1:11);
randomValidation = rawData(trainLimit+nValidationRecord,1:11);
% now test the record on the training thus far:
% return the square errors on our validation sample:
[ sqrsV1 , sqrsV2 ] = NN1.getSquareErrors(randomValidation);
sumV = sumV + (sqrsV1 + sqrsV2);
end
if nEpoch > 0
epochError(nEpoch) = sumE ./ trainLimit;
validationError(nEpoch) = sumV ./ validationLimit;
else
initialError = sumE ./ trainLimit; % possibly not needed.
if ((sumV ./ validationLimit) < validationThreshold)
validationThreshold = sumV ./ validationLimit;
end
end
fprintf('== After Epoch %d ==\n', nEpoch);
NN1.debugLayer(2);
xAxis = 1:nEpoch;
%axis auto;
% medfilt1
% real time plot
%subplot(2,1,1);
%plot(xAxis, (epochError(1:nEpoch)), 'b', xAxis, (validationError(1:nEpoch)), 'r');
%grid on;
%xlabel('Epoch Number');
%ylabel('Sum of Square Output errors');
%title('Plot of learning characteristics (smoothing filter)');
%legend('Training Error', 'Validation Error');
%drawnow;
% it has been determined that the terminal condition for epoch count:
% if the validationError drops below 0.05 and then proceeds to rise
% above it once again we can exit the epoch loop.
finalEpoch = nEpoch;
% test for minimums
if nEpoch > 0
if validationError(nEpoch) < validationThreshold
boolValidationBelowThreshold = true;
validationThreshold = min(validationError(1:nEpoch));
else
if boolValidationBelowThreshold == true
% if this is satisfied then we can break.
finalEpoch = nEpoch;
break; % break from current nEpoch.
end
%validationThreshold = min(validationError(1:nEpoch));
end
end
% after training to epoch nEpoch.
% after testing the epochs, lets look at test cases.
testScores = zeros(1);
randomTest = 0;
failedTests = 0;
for nTestRecord=1:testLimit
% for each test record
%randomTest = rawData(round(RangedRandom(trainLimit+validationLimit+1,trainLimit+validationLimit+testLimit)),1:11);
% aternatively test every single testData:
randomTest = rawData(trainLimit+validationLimit+nTestRecord,1:11);
% now test the record on the training thus far:
% return the square errors on our validation sample:
[ sqrsT1 , sqrsT2 ] = NN1.getSquareErrors(randomTest);
testScores(nTestRecord) = (sqrsT1 + sqrsT2);
if testScores(nTestRecord) >= 0.5
failedTests = failedTests + 1;
end
end
if nEpoch > 0
accuracyAtEpoch(nEpoch) = round((1 - failedTests./testLimit).*100);
end
% test score average =
if nEpoch > 0
avgTestScoreAtEpoch(nEpoch) = sum(testScores) ./ testLimit;
end
% real time plot
subplot(2,1,1);
plot(xAxis, (epochError(1:nEpoch)), 'b', xAxis, (validationError(1:nEpoch)), 'r', xAxis, avgTestScoreAtEpoch(1:nEpoch), '-g');
grid on;
xlabel('Epoch Number');
ylabel('Sum of Square Output errors');
[ tbest , itbest ] = min(avgTestScoreAtEpoch(1:nEpoch));
title(sprintf('Plot of learning characteristics for %d hidden neurons at alpha=%0.3f.\nBest Test Error is: %f at Epoch %d',NN1.noHiddenNeurons,NN1.GetLearnRate(), tbest, itbest));
legend('Training Error', 'Validation Error', 'Test Error');
%drawnow;
averageAccuracy(1:nEpoch) = mean(accuracyAtEpoch(1:nEpoch));
subplot(2,1,2);
plot(xAxis, accuracyAtEpoch(1:nEpoch), 'r', xAxis, averageAccuracy(1:nEpoch), 'g');
grid on;
xlabel('Epoch Number');
ylabel('Test Prediction Accuracy (%%)');
[ xbest, ibest ] = max(accuracyAtEpoch(1:nEpoch));
title(sprintf('Plot of test set prediction accuracy\nBest accuracy is %d%% at epoch %d', xbest, ibest));
text(ibest, xbest, sprintf('Max: %d%% -> ', xbest), 'HorizontalAlignment','right');
drawnow;
end
% after the train is done, save the elapsed time.
fprintf('===\nTraining of %d epochs completed in ~ %d minutes\n===\n', finalEpoch, round(toc(tTicker) ./ 60));
% after testing the epochs, lets look at test cases.
% since test scores is not done in every iteration:
% comment out.
% testScores = zeros(1);
% randomTest = 0;
% failedTests = 0;
%
% for nTestRecord=1:testLimit
% % for each test record
% randomTest = rawData(round(RangedRandom(trainLimit+validationLimit+1,trainLimit+validationLimit+testLimit)),1:11);
%
% % now test the record on the training thus far:
% % return the square errors on our validation sample:
% [ sqrsT1 , sqrsT2 ] = NN1.getSquareErrors(randomTest);
%
% testScores(nTestRecord) = (sqrsT1 + sqrsT2);
% if testScores(nTestRecord) > 0.5
% failedTests = failedTests + 1;
% end
% end
%
% % anything above 1 is bad.
%
% acc = figure;
% subplot(2,1,1);
% plot(xAxis, smooth(epochError(1:nEpoch)), 'b', xAxis, (validationError(1:nEpoch)), 'r');
% grid on;
% xlabel('Epoch Number');
% ylabel('Sum of Square Output errors');
% title('Plot of learning characteristics (smoothing filter)');
% legend('Training Error', 'Validation Error');
% drawnow;
%
%
% accuracy = round((1 - failedTests./testLimit).*100);
%
% % test score average =
%
% testScoreAverage = sum(testScores) ./ testLimit;
%
% subplot(2,1,2);
% plot(testScores);
% grid on;
% xlabel('Test Number');
% ylabel('Sum of Square Output errors');
% title(sprintf('Plot of test results for %d hidden neurons. \nAverage Sum of Square Test Errors = %.3f\nTests < 0.5 = %d %%', NN1.noHiddenNeurons, testScoreAverage, accuracy));
% drawnow;
print(acc, sprintf('%d neurons - a %.03f - time %d mins.png', NN1.noHiddenNeurons, NN1.GetLearnRate(), round(toc(tTicker) ./ 60) ), '-dpng');
%
save(sprintf('data-lastrun-alpha-%.03f-hiddens-%d.dat', NN1.GetLearnRate(), NN1.noHiddenNeurons));
% acc=figure('visible','off');
end