-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestTrainBasic.cpp
More file actions
375 lines (313 loc) · 12.5 KB
/
testTrainBasic.cpp
File metadata and controls
375 lines (313 loc) · 12.5 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* @file testTrainBasic.cpp
* @brief Tests of basic training.
*
*/
#include <iostream>
#include <boost/test/unit_test.hpp>
#include "test.hpp"
BOOST_AUTO_TEST_SUITE(basictrain)
/** \addtogroup testtrainbasic Training a plain unmodulated network
* \ingroup tests
* @{
*/
/**
* \brief Test training.
* This just checks that the network trains.
*/
BOOST_AUTO_TEST_CASE(trainparams) {
const int NUMEXAMPLES=1000;
ExampleSet e(NUMEXAMPLES,1,1 ,1); // 100 examples at 1 input, 1 output, 1 mod level
double recipNE = 1.0/(double)NUMEXAMPLES;
// generate examples of the identity function y=x, from 0 to 1.
// This will train but will be a bit iffy at the ends!
for(double i=0;i<NUMEXAMPLES;i++){
double v = i*recipNE;
*(e.getInputs(i)) = v;
*(e.getOutputs(i)) = v;
e.setH(i,0);
}
// set up a net which conforms to those examples with 3 hidden nodes.
Net *net = NetFactory::makeNet(NetType::PLAIN,e,3);
// eta=1, lots of iterations
Net::SGDParams params(1,10000000);
// use half of the data as CV examples, 1000 CV cycles, 10 slices.
// Don't shuffle the CV examples on epoch. Also, store the best net
// and make sure we end up with that.
params.crossValidation(e,0.5,1000,10,false).storeBest().setSeed(0);
// do the training and get the MSE of the best net.
double mse = net->trainSGD(e,params);
printf("%f\n",mse);
// assert that it's a sensible value
BOOST_REQUIRE(mse>0);
BOOST_REQUIRE(mse<0.005);
#if 0
for(double i=0;i<NUMEXAMPLES;i++){
double v = i*recipNE;
double o = *(net->run(&v));
printf(" %f,%f\n",v,o);
}
#endif
delete net;
}
/**
* \brief another test without cross-validation which attempts to emulate the Angort
* test.ang program.
*/
BOOST_AUTO_TEST_CASE(trainparams2) {
const int NUMEXAMPLES=100;
ExampleSet e(NUMEXAMPLES*2,1,1,1); // 100 examples at 1 input, 1 output, 1 modulator level
double recipNE = 1.0/(double)NUMEXAMPLES;
for(double i=0;i<NUMEXAMPLES*2;i+=2){
double v = (i/2)*recipNE;
*(e.getInputs(i)) = v;
*(e.getOutputs(i)) = v;
*(e.getInputs(i+1)) = v;
*(e.getOutputs(i+1)) = v;
e.setH(i,0);
e.setH(i+1,1);
}
Net *net = NetFactory::makeNet(NetType::PLAIN,e,2);
// eta=1, 10000000 iterations. No CV.
Net::SGDParams params(1,10000000);
params.storeBest();
// do the training and get the MSE of the best net.
double mse = net->trainSGD(e,params);
printf("%f\n",mse);
#if 0
for(double i=0;i<NUMEXAMPLES;i++){
double v = i*recipNE;
double o = *(net->run(&v));
printf("%f -> %f\n",v,o);
}
#endif
// assert that it's a sensible value
BOOST_REQUIRE(mse>0);
BOOST_REQUIRE(mse<0.005);
delete net;
}
//! [addition]
/**
* \brief Construct an addition model from scratch and try to learn it
* with backprop
*/
BOOST_AUTO_TEST_CASE(addition) {
// 1000 examples, 2 inputs, 1 output, 1 modulator level (i.e. no modulation)
ExampleSet e(1000,2,1,1);
// initialise a PRNG
drand48_data rd;
srand48_r(10,&rd);
// create the examples
for(int i=0;i<1000;i++){
// get a pointer to the inputs for this example
double *ins = e.getInputs(i);
// and a pointer to the outputs (only one of them in this case)
double *out = e.getOutputs(i);
// use the PRNG to generate the operands in the range [0,0.5) to ensure
// that the result is <1.
double a,b;
drand48_r(&rd,&a);a*=0.5;
drand48_r(&rd,&b);b*=0.5;
// write the inputs and the output
ins[0] = a;
ins[1] = b;
*out = a+b;
}
// create a plain backprop network
// which conforms to those examples with 2 hidden nodes.
Net *net = NetFactory::makeNet(NetType::PLAIN,e,2);
// set up training parameters:
// eta=1, lots of iterations
Net::SGDParams params(1,10000000);
// cross validation etc.:
// use half of the data as CV examples. This is divided into 10 slices,
// and we do cross-validation 1000 times during the run.
// Don't shuffle the CV examples on epoch. Also, store the best net
// and make sure we end up with that. We also set a PRNG seed to
// ensure reproducibility.
params.crossValidation(e, // example set
0.5, // proportion of set to hold back for CV
1000, // number of CV cycles
10, // number of CV slices
false // don't shuffle the entire CV set on completing an epoch
)
.storeBest() // store the best net inside this param block
.setSeed(0); // initialise PRNG for net, used for initial weights and shuffling.
// do the training and get the MSE of the best net, found by cross-validation.
double mse = net->trainSGD(e,params);
printf("%f\n",mse);
// check the MSE
BOOST_REQUIRE(mse<0.03);
// test the actual performance - loop through lots of pairs
// of numbers <0.5 (the only numbers we can do given the range of the function)
for(double a=0;a<0.5;a+=0.02){
for(double b=0;b<0.5;b+=0.02){
// set up an array holding the inputs
double runIns[2];
runIns[0]=a;
runIns[1]=b;
// run the net and get the output
double out = *(net->run(runIns));
// check the difference (with a line commented out to print it)
double diff = fabs(out-(a+b));
// printf("%f+%f=%f (%f)\n",a,b,out,diff);
BOOST_REQUIRE(diff<0.05);
}
}
delete net;
}
//! [addition]
//! [additionmod]
/**
* \brief Construct an addition/addition+scaling model from scratch and try to learn it
* with UESMANN. The h=0 is \f$y=a+b\f$, the h=1 function is \f$y=(a+b)*0.3\f$.
*/
BOOST_AUTO_TEST_CASE(additionmod) {
// 2000 examples, 2 inputs, 1 output, 2 modulator levels. We need to know
// the number of modulator levels so that example shuffling will shuffle in
// blocks of that count, or will shuffle normally and then fix up to ensure that
// example modulator levels alternate in the net (ExampleSet::STRIDE and
// ExampleNet::ALTERNATE respectively).
ExampleSet e(2000,2,1,2);
// initialise a PRNG
drand48_data rd;
srand48_r(10,&rd);
// create the examples
int idx=0; // example index
for(int i=0;i<1000;i++){
// use the PRNG to generate the operands in the range [0,0.5) to ensure
// that the result is <1.
double a,b;
drand48_r(&rd,&a);a*=0.5;
drand48_r(&rd,&b);b*=0.5;
// get a pointer to the inputs for the h=0 example and write to it
double *ins = e.getInputs(idx);
double *out = e.getOutputs(idx);
ins[0] = a;
ins[1] = b;
*out = a+b;
e.setH(idx,0); // set modulator for this example
idx++; // increment the example index
// Do the same for the h=1 example, but here we're writing 0.3(a+b),
// and the modulator is 1.
ins = e.getInputs(idx);
out = e.getOutputs(idx);
ins[0] = a;
ins[1] = b;
*out = (a+b)*0.3;
e.setH(idx,1);
idx++;
}
// create a UESMANN network which conforms to those examples with 2 hidden nodes.
Net *net = NetFactory::makeNet(NetType::UESMANN,e,2);
// set up training parameters:
// eta=1, lots of iterations
Net::SGDParams params(1,1000000);
// cross validation etc.:
// use half of the data as CV examples. This is divided into 10 slices,
// and we do cross-validation 1000 times during the run.
// DO shuffle the CV examples on epoch. Also, store the best net
// and make sure we end up with that. We also set a PRNG seed to
// ensure reproducibility.
params.crossValidation(e, // example set
0.5, // proportion of set to hold back for CV
1000, // number of CV cycles
10, // number of CV slices
true // shuffle the entire CV set on completing an epoch
)
.storeBest() // store the best net inside this param block
.setSeed(0); // initialise PRNG for net, used for initial weights and shuffling.
// do the training and get the MSE of the best net, found by cross-validation.
double mse = net->trainSGD(e,params);
printf("%f\n",mse);
// check the MSE
BOOST_REQUIRE(mse<0.03);
// test the actual performance - loop through lots of pairs
// of numbers. We limit the range here; performance is known to fall
// off at the ends due to node saturation (probably)
for(double a=0.1;a<0.4;a+=0.02){
for(double b=0.1;b<0.4;b+=0.02){
// set up an array holding the inputs
double runIns[2];
runIns[0]=a;
runIns[1]=b;
// check for H=0
net->setH(0);
double out = *(net->run(runIns));
// check the difference (with a line commented out to print it)
double diff = fabs(out-(a+b));
printf("%f+%f=%f (%f)\n",a,b,out,diff);
BOOST_REQUIRE(diff<0.07);
// check for H=1
net->setH(1);
out = *(net->run(runIns));
diff = fabs(out-(a+b)*0.3);
printf("%f+%f=%f (%f)\n",a,b,out,diff);
BOOST_REQUIRE(diff<0.07);
}
}
delete net;
}
//! [additionmod]
//! [trainmnist]
/**
* \brief Train for MNIST handwriting recognition in a plain backprop network.
* This doesn't do a huge number of iterations.
*/
BOOST_AUTO_TEST_CASE(trainmnist){
// Create an MNIST object, which consists of labelled data in the standard MNIST
// format (http://yann.lecun.com/exdb/mnist/). This is in two files, one containing
// the images and one containing the data.
MNIST m("../testdata/train-labels-idx1-ubyte","../testdata/train-images-idx3-ubyte");
// This ExampleSet constructor builds the examples directly from the MNIST data,
// with a large number of inputs (28x28) and a number of outputs equal to the maximum
// label value + 1. The outputs of the examples are in a one-hot format: for handwritten
// digits, there will be 10 in which the output corresponding to the label will
// be 1 with the others 0.
ExampleSet e(m);
// create a plain MLP network conforming to the examples' input and output counts
// with 16 hidden nodes
Net *n = NetFactory::makeNet(NetType::PLAIN,e,16);
// set up the parameters
Net::SGDParams params(0.1,10000); // eta,iterations
// use half of the data as CV examples, 1000 CV cycles, 10 slices.
// Shuffle the CV examples on epoch. Also, store the best net
// and make sure we end up with that. Set the seed to 10.
// Shuffle mode is stride, the default (not that it matters here)
params.crossValidation(e,0.5,1000,10,true)
.storeBest()
.setSeed(10);
// train and get the MSE, and test it is low.
double mse = n->trainSGD(e,params);
BOOST_REQUIRE(mse<0.03);
// now load the test set of 10000 images and labels and construct
// examples in a similar way.
MNIST mtest("../testdata/t10k-labels-idx1-ubyte","../testdata/t10k-images-idx3-ubyte");
ExampleSet testSet(mtest);
// and test against the test set, recording how many are good.
int correct=0;
for(int i=0;i<testSet.getCount();i++){
// for each test, get the inputs
double *ins = testSet.getInputs(i);
// run them through the network and get the outputs
double *o = n->run(ins);
// find the correct label by getting the highest output in the example
int correctLabel = getHighest(testSet.getOutputs(i),testSet.getOutputCount());
// find the network's result by getting its highest output
int netLabel = getHighest(o,testSet.getOutputCount());
// and increment the count if they agree
if(correctLabel==netLabel)correct++;
}
// get the ratio of correct answers -
// we've not trained for long so this isn't going to be brilliant performance.
double ratio = ((double)correct)/(double)testSet.getCount();
printf("MSE=%f, correct=%d/%d=%f\n",mse,correct,testSet.getCount(),ratio);
// assert that it's at least 85%
BOOST_REQUIRE(ratio>0.85);
delete n;
}
//! [trainmnist]
/**
* @}
*/
BOOST_AUTO_TEST_SUITE_END()