-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNet.cpp
More file actions
367 lines (324 loc) · 9.53 KB
/
NeuralNet.cpp
File metadata and controls
367 lines (324 loc) · 9.53 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
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <cassert>
#include "NeuralNet.h"
using namespace std;
using namespace ML;
#define MAX_RAND 1.0
///////////////////////////////////////////////////////////////////////////////
// Public API
///////////////////////////////////////////////////////////////////////////////
/**
* Constructor - initializes the underlying neural network, preparing it for
* approximation & training.
*
* @param layerDims a vector containing the layer dimensions of the neural
* network, including the number of layers and the number of neurons per layer
* @param stepSize when training, the step size used to update biases & weights
* @param nntype the type of neural network, where SUPERVISED is a standard
* neural network (unimplemented) & REINFORCEMENT_LEARNING is a neural network
* that can be trained online
* @param ntype the neuron model type (see Neuron.h for more details)
* @param cf the type of cost function used to drive backpropagation (see
* CostFunc.h for more details)
*/
NeuralNet::NeuralNet(const vector<size_t>& layerDims,
double stepSize,
NNType nntype,
Neuron::NeuronType ntype,
CostFunc::CostFuncType cf)
: _nntype(nntype), _numLayers(layerDims.size()), _stepSize(stepSize),
_layerDims(layerDims)
{
assert(_nntype == REINFORCEMENT_LEARNING); // TODO
assert(_numLayers > 1);
// Allocate matrices & vectors for NN
_weights = new matrix<double>[_numLayers-1];
_weightGradients = new matrix<double>[_numLayers-1];
_biases = new vector<double>[_numLayers-1];
_sums = new vector<double>[_numLayers-1];
_activations = new vector<double>[_numLayers-1];
_errors = new vector<double>[_numLayers-1];
for(size_t i = 0; i < _numLayers-1; i++)
{
_weights[i].resize(layerDims[i], layerDims[i+1]);
_weightGradients[i].resize(layerDims[i], layerDims[i+1]);
_biases[i].resize(layerDims[i+1]);
_sums[i].resize(layerDims[i+1]);
_activations[i].resize(layerDims[i+1]);
_errors[i].resize(layerDims[i+1]);
}
// Initialize weights & biases
srand(time(NULL));
randomizeWeights(MAX_RAND);
randomizeBiases(MAX_RAND);
// Initialize neuron model for activation & backpropagation
switch(ntype)
{
case Neuron::SIGMOID:
_neuron = new SigmoidNeuron();
break;
/* case TANH: TODO
_neuron = new TanHNeuron();
break;*/
default:
assert(false && "Unknown neuron model!");
break;
}
// Initialize cost function for backpropagation
switch(cf)
{
case CostFunc::MSE:
_cf = new MSE(_neuron); // g++ error if ML namespace isn't declared...?
break;
default:
assert(false && "Unknown cost function!");
break;
}
}
/**
* Destructor - works as advertised
*/
NeuralNet::~NeuralNet()
{
if(_weights)
delete [] _weights;
if(_weightGradients)
delete [] _weightGradients;
if(_biases)
delete [] _biases;
if(_sums)
delete [] _sums;
if(_activations)
delete [] _activations;
if(_errors)
delete [] _errors;
if(_neuron)
delete _neuron;
if(_cf)
delete _cf;
}
/**
* Using an input state vector, approximate a state-action value
*/
void NeuralNet::approximate(const std::vector<double>& inputs,
std::vector<double>& outputs)
{
assert(_nntype == REINFORCEMENT_LEARNING);
feedforward(inputs, outputs);
}
/**
* Train using the reward given by the environment
*/
void NeuralNet::train(unsigned long action, double reward)
{
assert(_nntype == REINFORCEMENT_LEARNING);
assert(action < _activations[_numLayers-2].size());
// Set output delta to (reward - activation) only for action taken, otherwise
// set output delta to zero (no error)
vector<double> outputs(_activations[_numLayers-2].size());
for(size_t i = 0; i < outputs.size(); i++)
{
if(i == action)
outputs[i] = reward;
else
outputs[i] = _activations[_numLayers-2][i];
}
feedback(outputs);
}
/**
* Save the neural network in a Python-parsable format
*
* TODO save other parameters (NN type, num layers, step size, layer
* dimensions, neuron model & cost function)
*
* Output is a tuple that contains the following fields:
* [0] List of weight matrices
* [1] List of bias vectors
*/
ostream& NeuralNet::save(ostream& out) const
{
string data("([");
stringstream ss;
// Weight matrices
for(size_t i = 0; i < _numLayers - 1; i++)
ss << _weights[i] << ",";
data += ss.str();
data.resize(data.length() - 1);
data += "],[";
// Bias vectors
ss.str(string());
for(size_t i = 0; i < _numLayers - 1; i++)
ss << _biases[i] << ",";
data += ss.str();
data.resize(data.length() - 1);
data += "])";
out << data;
// TODO remove
out << endl;
for(size_t i = 0; i < _numLayers - 1; i++)
out << _sums[i] << endl;
return out;
}
istream& NeuralNet::restore(istream& in)
{
//TODO
return in;
}
///////////////////////////////////////////////////////////////////////////////
// Private API
///////////////////////////////////////////////////////////////////////////////
/**
* Feed inputs forward through the network to generate outputs, saves
* appropriate intermediate results for backpropagation.
*
* TODO move matrix multiplications/vector additions to functions
* TODO optimize (SIMD/cache blocking/FMADD)
*/
void NeuralNet::feedforward(const vector<double>& inputs,
vector<double>& outputs)
{
assert(inputs.size() == _weights[0].rows());
assert(outputs.size() == _weights[_numLayers-2].cols());
// Save inputs for backpropagation
_inputs = inputs;
// Initialize output vector
for(size_t i = 0; i < outputs.size(); i++)
outputs[i] = 0.0;
// Feed forward
if(_numLayers == 2)
{
for(size_t i = 0; i < _weights[0].rows(); i++)
for(size_t j = 0; j < _weights[0].cols(); j++)
outputs[j] += inputs[i] * _weights[0](i, j);
for(size_t i = 0; i < outputs.size(); i++)
{
outputs[i] += _biases[0][i];
_sums[0][i] = outputs[i];
outputs[i] = _neuron->activate(outputs[i]);
_activations[0][i] = outputs[i];
}
}
else
{
size_t l = 0;
zeroSums();
// Input layer
for(size_t i = 0; i < _weights[l].rows(); i++)
for(size_t j = 0; j < _weights[l].cols(); j++)
_sums[l][j] += inputs[i] * _weights[l](i, j);
for(size_t i = 0; i < _activations[l].size(); i++)
{
_sums[l][i] += _biases[l][i];
_activations[l][i] = _neuron->activate(_sums[l][i]);
}
// Hidden layers
for(l++; l < _numLayers-2; l++)
{
for(size_t i = 0; i < _weights[l].rows(); i++)
for(size_t j = 0; j < _weights[l].cols(); j++)
_sums[l][j] += _activations[l-1][i] * _weights[l](i, j);
for(size_t i = 0; i < _activations[l].size(); i++)
{
_sums[l][i] += _biases[l][i];
_activations[l][i] = _neuron->activate(_sums[l][i]);
}
}
// Output layer
for(size_t i = 0; i < _weights[l].rows(); i++)
for(size_t j = 0; j < _weights[l].cols(); j++)
outputs[j] += _activations[l-1][i] * _weights[l](i, j);
for(size_t i = 0; i < outputs.size(); i++)
{
outputs[i] += _biases[l][i];
_sums[l][i] = outputs[i];
outputs[i] = _neuron->activate(outputs[i]);
_activations[l][i] = outputs[i];
}
}
}
/**
* Core of backpropagation algorithm - calculate weight & bias gradients
* by propagating errors back through the network. On the way, update weights
* & biases according to the step size.
*
* @param labels a vector containing the expected outputs or lables for the
* previous inputs
*/
void NeuralNet::feedback(const vector<double>& labels)
{
// Sanity checks
assert(_errors[_numLayers-2].size() == labels.size());
// Calculate errors, backpropagate & train
if(_numLayers == 2)
{
_cf->calcOutputError(labels, _activations[0], _sums[0], _errors[0]);
_cf->calcWeightGradients(_errors[0], _inputs, _weightGradients[0]);
}
else
{
size_t l = _numLayers - 2;
// Output layer
_cf->calcOutputError(labels, _activations[l], _sums[l], _errors[l]);
_cf->calcWeightGradients(_errors[l], _activations[l-1], _weightGradients[l]);
// Hidden layers
for(l--; l > 0; l--)
{
_cf->calcHiddenError(_weights[l+1], _errors[l+1], _sums[l], _errors[l]);
_cf->calcWeightGradients(_errors[l], _activations[l-1], _weightGradients[l]);
}
// Input layer
_cf->calcHiddenError(_weights[1], _errors[1], _sums[0], _errors[0]);
_cf->calcWeightGradients(_errors[0], _inputs, _weightGradients[0]);
}
// Update weights & biases after all gradients have been calculated
for(size_t i = 0; i < _numLayers - 1; i++)
{
updateBiases(i);
updateWeights(i);
}
}
void NeuralNet::zeroWeights()
{
for(size_t i = 0; i < _numLayers - 1; i++)
for(size_t j = 0; j < _weights[i].rows(); j++)
for(size_t k = 0; k < _weights[i].cols(); k++)
_weights[i](j, k) = 0.0;
}
void NeuralNet::zeroBiases()
{
for(size_t i = 0; i < _numLayers - 1; i++)
for(size_t j = 0; j < _biases[i].size(); j++)
_biases[i][j] = 0.0;
}
void NeuralNet::zeroSums()
{
for(size_t i = 0; i < _numLayers - 1; i++)
for(size_t j = 0; j < _sums[i].size(); j++)
_sums[i][j] = 0.0;
}
void NeuralNet::randomizeWeights(double max)
{
for(size_t i = 0; i < _numLayers - 1; i++)
for(size_t j = 0; j < _weights[i].rows(); j++)
for(size_t k = 0; k < _weights[i].cols(); k++)
_weights[i](j, k) = ((double)rand() / RAND_MAX) * max;
}
void NeuralNet::randomizeBiases(double max)
{
for(size_t i = 0; i < _numLayers - 1; i++)
for(size_t j = 0; j < _biases[i].size(); j++)
_biases[i][j] = ((double)rand() / RAND_MAX) * max;
}
void NeuralNet::updateBiases(size_t layer)
{
for(size_t i = 0; i < _biases[layer].size(); i++)
_biases[layer][i] -= _stepSize * _errors[layer][i];
}
void NeuralNet::updateWeights(size_t layer)
{
for(size_t i = 0; i < _weights[layer].rows(); i++)
for(size_t j = 0; j < _weights[layer].cols(); j++)
_weights[layer](i, j) -= _stepSize * _weightGradients[layer](i, j);
}