-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpnet.hpp
390 lines (337 loc) · 12.5 KB
/
bpnet.hpp
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* @file bpnet.hpp
* @brief This implements a plain backprop network
*
*/
#ifndef __BPNET_HPP
#define __BPNET_HPP
#include "net.hpp"
/**
* \brief The "basic" back-propagation network using a logistic sigmoid,
* as described by Rumelhart, Hinton and Williams (and many others).
* This class is used by output blending and h-as-input networks.
*/
class BPNet : public Net {
protected:
/**
* \brief Special constructor for subclasses which need to manipulate layer
* count before initialisation (e.g. HInputNet).
*/
BPNet() : Net (NetType::PLAIN) {
}
/**
* \brief Initialiser for use by the main constructor and the ctors of those
* subclasses mentioned in BPNet()
*/
void init(int nlayers,const int *layerCounts){
numLayers = nlayers;
outputs = new double* [numLayers];
errors = new double* [numLayers];
layerSizes = new int [numLayers];
largestLayerSize=0;
for(int i=0;i<numLayers;i++){
int n = layerCounts[i];
outputs[i] = new double[n];
errors[i] = new double[n];
for(int k=0;k<n;k++)
outputs[i][k]=0;
layerSizes[i]=n;
if(n>largestLayerSize)
largestLayerSize=n;
}
weights = new double * [numLayers];
gradAvgsWeights = new double* [numLayers];
biases = new double* [numLayers];
gradAvgsBiases = new double* [numLayers];
for(int i=0;i<numLayers;i++){
int n = layerCounts[i];
weights[i] = new double[largestLayerSize*largestLayerSize];
gradAvgsWeights[i] = new double[largestLayerSize*largestLayerSize];
biases[i] = new double[n];
gradAvgsBiases[i] = new double[n];
}
}
public:
/**
* \brief Constructor - does not initialise the weights to random values so
* that we can reinitialise networks.
* \param nlayers number of layers
* \param layerCounts array of layer counts
*/
BPNet(int nlayers,const int *layerCounts) : Net(NetType::PLAIN) {
init(nlayers,layerCounts);
}
virtual void setH(double h){
// does nothing, because this is an unmodulated net.
}
virtual double getH() const {
return 0;
}
/**
* \brief destructor
*/
virtual ~BPNet(){
for(int i=0;i<numLayers;i++){
delete [] weights[i];
delete [] biases[i];
delete [] gradAvgsWeights[i];
delete [] gradAvgsBiases[i];
delete [] outputs[i];
delete [] errors[i];
}
delete [] weights;
delete [] biases;
delete [] gradAvgsWeights;
delete [] gradAvgsBiases;
delete [] outputs;
delete [] errors;
delete [] layerSizes;
}
virtual void setInputs(double *d) {
for(int i=0;i<layerSizes[0];i++){
outputs[0][i]=d[i];
}
}
/**
* \brief Used to set inputs manually, typically in
* HInputNet.
*/
void setInput(int n, double d){
outputs[0][n] = d;
}
virtual double *getOutputs() const {
return outputs[numLayers-1];
}
virtual int getLayerSize(int n) const {
return layerSizes[n];
}
virtual int getLayerCount() const {
return numLayers;
}
virtual int getDataSize() const {
// number of weights+biases for each layer is
// the number of nodes in that layer (bias count)
// times the number of nodes in the previous layer.
//
// NOTE THAT this uses the true layer size rather than
// the fake version returned in the subclass HInputNet
int pc=0;
int total=0;
for(int i=0;i<numLayers;i++){
int c = layerSizes[i];
total += c*(1+pc);
pc = c;
}
return total;
}
virtual void save(double *buf) const {
double *g=buf;
// data is ordered by layers, with nodes within
// layers, and each node is bias then weights.
//
// NOTE THAT this uses the true layer size rather than
// the fake version returned in the subclass HInputNet
for(int i=0;i<numLayers;i++){
for(int j=0;j<layerSizes[i];j++){
*g++ = biases[i][j];
if(i){
for(int k=0;k<layerSizes[i-1];k++){
*g++ = getw(i,j,k);
}
}
}
}
}
virtual void load(double *buf){
double *g=buf;
// genome is ordered by layers, with nodes within
// layers, and each node is bias then weights.
//
// NOTE THAT this uses the true layer size rather than
// the fake version returned in the subclass HInputNet
for(int i=0;i<numLayers;i++){
for(int j=0;j<layerSizes[i];j++){
biases[i][j]=*g++;
if(i){
for(int k=0;k<layerSizes[i-1];k++){
getw(i,j,k) = *g++;
}
}
}
}
}
protected:
int numLayers; //!< number of layers, including input and output
int *layerSizes; //!< array of layer sizes
int largestLayerSize; //!< number of nodes in largest layer
/// \brief Array of weights as [tolayer][tonode+largestLayerSize*fromnode]
///
/// Weights are stored as a square matrix, even though less than
/// half is used. Less than that, if not all layers are the same
/// size, since the dimension of the matrix must be the size of
/// the largest layer. Each array has its own matrix,
/// so index by [layer][i+largestLayerSize*j], where
/// - layer is the "TO" layer
/// - layer-1 is the FROM layer
/// - i is the TO neuron (i.e. the end of the connection)
/// - j is the FROM neuron (the start)
double **weights;
/// array of biases, stored as a rectangular array of [layer][node]
double **biases;
// data generated during training and running
double **outputs; //!< outputs of each layer: one array of doubles for each
double **errors; //!< the error for each node, calculated by calcError()
double **gradAvgsWeights; //!< average gradient for each weight (built during training)
double **gradAvgsBiases; //!< average gradient for each bias (built during training)
virtual void initWeights(double initr){
for(int i=0;i<numLayers;i++){
double initrange;
if(i){
double ct = layerSizes[i-1];
if(initr>0)
initrange = initr;
else
initrange = 1.0/sqrt(ct); // from Bishop
} else
initrange = 0.1; // on input layer, should mean little.
for(int j=0;j<layerSizes[i];j++)
biases[i][j]=drand(-initrange,initrange);
for(int j=0;j<largestLayerSize*largestLayerSize;j++){
weights[i][j]=drand(-initrange,initrange);
}
}
// zero the input layer weights, which should be unused.
for(int j=0;j<layerSizes[0];j++)
biases[0][j]=0;
for(int j=0;j<largestLayerSize*largestLayerSize;j++)
weights[0][j]=0;
}
/**
* \brief get the value of a weight.
* \param tolayer the layer of the destination node (from is assumed to be previous layer)
* \param toneuron the index of the destination node in that layer
* \param fromneuron the index of the source node
*/
inline double& getw(int tolayer,int toneuron,int fromneuron) const {
return weights[tolayer][toneuron+largestLayerSize*fromneuron];
}
/**
* \brief get the value of a bias
* \param layer index of layer
* \param neuron index of neuron within layer
*/
inline double& getb(int layer,int neuron) const {
return biases[layer][neuron];
}
/**
* \brief get the value of the gradient for a given weight
* \pre gradients must have been calculated as part of training step
* \param tolayer the layer of the destination node (from is assumed to be previous layer)
* \param toneuron the index of the destination node in that layer
* \param fromneuron the index of the source node
*/
inline double& getavggradw(int tolayer,int toneuron,int fromneuron) const {
return gradAvgsWeights[tolayer][toneuron+largestLayerSize*fromneuron];
}
/**
* \brief get the value of a bias gradient
* \pre gradients must have been calculated as part of training step
* \param l index of layer
* \param n index of neuron within layer
*/
inline double getavggradb(int l,int n) const {
return gradAvgsBiases[l][n];
}
/**
* \brief run a single example and calculate the errors; used in training.
* \param in inputs
* \param out required outputs
* \post the errors will be in the errors variable
*/
void calcError(double *in,double *out){
// first run the network forwards
setInputs(in);
update();
// first, calculate the error in the output layer
int ol = numLayers-1;
for(int i=0;i<layerSizes[ol];i++){
double o = outputs[ol][i];
errors[ol][i] = o*(1-o)*(o-out[i]);
}
// then work out the errors in all the other layers
for(int l=1;l<numLayers-1;l++){
for(int j=0;j<layerSizes[l];j++){
double e = 0;
for(int i=0;i<layerSizes[l+1];i++)
e += errors[l+1][i]*getw(l+1,i,j);
// produce the \delta^l_i term where l is the layer and i
// the index of the node
errors[l][j] = e * outputs[l][j] * (1-outputs[l][j]);
}
}
}
virtual void update(){
for(int i=1;i<numLayers;i++){
for(int j=0;j<layerSizes[i];j++){
double v = biases[i][j];
for(int k=0;k<layerSizes[i-1];k++){
v += getw(i,j,k) * outputs[i-1][k];
}
outputs[i][j]=sigmoid(v);
}
}
}
virtual double trainBatch(ExampleSet& ex,int start,int num,double eta){
// zero average gradients
for(int j=0;j<numLayers;j++){
for(int k=0;k<layerSizes[j];k++)
gradAvgsBiases[j][k]=0;
for(int i=0;i<largestLayerSize*largestLayerSize;i++)
gradAvgsWeights[j][i]=0;
}
// reset total error
double totalError=0;
// iterate over examples
for(int nn=0;nn<num;nn++){
int exampleIndex = nn+start;
// set modulator
setH(ex.getH(exampleIndex));
// get outputs for this example
double *outs = ex.getOutputs(exampleIndex);
// build errors for each example
calcError(ex.getInputs(exampleIndex),outs);
// accumulate errors
for(int l=1;l<numLayers;l++){
for(int i=0;i<layerSizes[l];i++){
for(int j=0;j<layerSizes[l-1];j++)
getavggradw(l,i,j) += errors[l][i]*outputs[l-1][j];
gradAvgsBiases[l][i] += errors[l][i];
}
}
// count up the total error
int ol = numLayers-1;
for(int i=0;i<layerSizes[ol];i++){
double o = outputs[ol][i];
double e = (o-outs[i]);
totalError += e*e;
}
}
// for calculating average error - 1/number of examples trained
double factor = 1.0/(double)num;
// we now have a full set of running averages. Time to apply them.
for(int l=1;l<numLayers;l++){
for(int i=0;i<layerSizes[l];i++){
for(int j=0;j<layerSizes[l-1];j++){
double wdelta = eta*getavggradw(l,i,j)*factor;
// printf("WCORR: %f factor %f\n",wdelta,getavggradw(l,i,j));
getw(l,i,j) -= wdelta;
}
double bdelta = eta*gradAvgsBiases[l][i]*factor;
biases[l][i] -= bdelta;
}
}
// and return total error - this is the SUM of the MSE of each output
return totalError*factor;
}
};
#endif /* __BPNET_HPP */