-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.hpp
455 lines (394 loc) · 12.2 KB
/
NeuralNetwork.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright (C) <[email protected]>
//
// This file is part of TOOFUS (TOols OFten USued)
//
// It can not be copied and/or distributed without the express
// permission of the authors.
// It is coded for academic purposes.
//
// Note
// Without a license, the code is copyrighted by default.
// People can read the code, but they have no legal right to use it.
// To use the code, you must contact the author directly and ask permission.
//
// This is a stand-alone header file that implement a Neural Network
// with a single hidden layer (Non-deep Neural Network)
#ifndef NN_HPP
#define NN_HPP
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
typedef std::vector<double> cvector;
typedef std::vector<cvector> cmatrix;
void cvector_add_inplace(cvector &m, cvector &b) {
if (m.size() != b.size()) {
std::cerr << "@cvector_add_inplace, m and b must have the same size\n";
}
for (size_t i = 0; i < m.size(); i++) {
m[i] += b[i];
}
}
cvector cvector_substract(cvector &a, cvector &b) {
if (a.size() != b.size()) {
std::cerr << "@cvector_substract, a and b must have the same size\n";
}
cvector result(a.size());
for (size_t i = 0; i < a.size(); i++) {
result[i] = a[i] - b[i];
}
return result;
}
cmatrix cmatrix_create(size_t rows, size_t cols) {
cmatrix M;
M.resize(rows);
for (size_t i = 0; i < rows; i++) {
M[i].resize(cols);
for (size_t j = 0; j < cols; j++) {
M[i][j] = 0.0;
}
}
return M;
}
void cmatrix_getSizes(cmatrix &m, size_t &rows, size_t &cols) {
rows = m.size();
cols = m[0].size();
}
void cmatrix_randomize_gaussian_inplace(cmatrix &m, double stddev = 1.0) {
size_t rows, cols;
cmatrix_getSizes(m, rows, cols);
std::default_random_engine generator;
std::normal_distribution<double> distribution(0.0, stddev);
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
m[i][j] = distribution(generator);
}
}
}
void cmatrix_randomize_inplace(cmatrix &m, double min = -1.0, double max = 1.0) {
size_t rows, cols;
cmatrix_getSizes(m, rows, cols);
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
m[i][j] = min + (rand() / (double)RAND_MAX) * (max - min);
}
}
}
void cvector_randomize_gaussian_inplace(cvector &v, double stddev = 1.0) {
std::default_random_engine generator;
std::normal_distribution<double> distribution(0.0, stddev);
for (size_t i = 0; i < v.size(); i++) {
v[i] = distribution(generator);
}
}
void cvector_randomize_inplace(cvector &v, double min = -1.0, double max = 1.0) {
for (size_t i = 0; i < v.size(); i++) {
v[i] = min + (rand() / (double)RAND_MAX) * (max - min);
}
}
// example with lambda: cvector_map_inplace(m, [](double x) -> double { return 2.0 * x; });
void cvector_map_inplace(cvector &m, std::function<double(double)> func) {
for (size_t i = 0; i < m.size(); i++) {
m[i] = func(m[i]);
}
}
cvector cmatrix_mult(cmatrix &a, cvector &b) {
size_t a_rows, a_cols;
cmatrix_getSizes(a, a_rows, a_cols);
size_t b_rows = b.size();
if (a_cols != b_rows) {
std::cerr << "@cmatrix_mult(a, b): number of columns of a must equals number of rows of b\n";
}
cvector result(a_rows);
for (size_t i = 0; i < a_rows; i++) {
double sum = 0.0;
for (size_t k = 0; k < a_cols; k++) {
sum += a[i][k] * b[k];
}
result[i] = sum;
}
return result;
}
void cmatrix_add_inplace(cmatrix &a, cmatrix &b) {
size_t a_rows, a_cols;
cmatrix_getSizes(a, a_rows, a_cols);
size_t b_rows, b_cols;
cmatrix_getSizes(b, b_rows, b_cols);
if (a_rows != b_rows || a_cols != b_cols) {
std::cerr << "@cmatrix_add_inplace(a, b): a and b must have the same sizes\n";
}
for (size_t i = 0; i < a_rows; i++) {
for (size_t j = 0; j < a_cols; j++) {
a[i][j] += b[i][j];
}
}
}
void print(cmatrix &m) {
size_t rows, cols;
cmatrix_getSizes(m, rows, cols);
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
std::cout << m[i][j] << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
}
void print(cvector &v) {
for (size_t i = 0; i < v.size(); i++) {
std::cout << v[i] << '\n';
}
std::cout << '\n';
}
// ==================== ACTIVATORS ============================
// SIGMOID
int SIGMOID_ACTIVATOR_ID = 0;
double sigmoid(double x) { return (1.0 / (1.0 + exp(-x))); }
double dsigmoid(double s) { return (s * (1.0 - s)); }
double invSigmoid(double y) { return (log(y / (1.0 - y))); }
// TANH
int TANH_ACTIVATOR_ID = 1;
double Tanh(double x) { return tanh(x); }
double dTanh(double s) {
double th = tanh(s);
return (1.0 - th * th);
}
double invTanh(double y) { return 0.5 * log((1.0 + y) / (1.0 - y)); }
// BINARY STEP
int BINARY_STEP_ACTIVATOR_ID = 2;
double binary(double x) {
if (fabs(x) < 1.0)
return 0.5 * (x + 1.0);
if (x < 0.0)
return 0.0;
return 1.0;
}
double dBinary(double x) {
if (fabs(x) < 1.0)
return 0.5;
return 0.0;
}
double invBinary(double x) { return 0.0; } // TODO
// IDENTITY
int IDENTITY_ACTIVATOR_ID = 3;
double identity(double x) { return x; }
double dIdentity(double s) { return 1.0; }
double invIdentity(double y) { return y; }
// BINARY STEP
int RELU_ACTIVATOR_ID = 4;
double reLU(double x) {
if (x < 0.0)
return 0.0;
return x;
}
double dReLU(double x) {
if (x < 0.0)
return 0.0;
return 1.0;
}
double invReLU(double y) {
if (y < 0.0)
return -1.0;
return y;
}
// ===================================================================
// Stand alone Neural Network
// This version is with a single hidden layer (Non-deep Neural Network)
class NeuralNetwork {
std::function<double(double)> Activatorfunc;
std::function<double(double)> dActivatorfunc;
std::function<double(double)> invActivatorfunc;
public:
int activatorId;
size_t nbInputs;
size_t nbHiddenNodes;
size_t nbOutputs;
double learnRate;
cmatrix W_ih;
cvector bias_h;
cmatrix W_ho;
cvector bias_o;
NeuralNetwork(size_t nbInputs_, size_t nbHiddenNodes_, size_t nbOutputs_, double learnRate_,
int activatorId_ = SIGMOID_ACTIVATOR_ID) {
nbInputs = nbInputs_;
nbHiddenNodes = nbHiddenNodes_;
nbOutputs = nbOutputs_;
learnRate = learnRate_;
setActivator(activatorId_);
// initiation (TODO: add possibility to use different strategies)
W_ih = cmatrix_create(nbHiddenNodes, nbInputs);
cmatrix_randomize_gaussian_inplace(W_ih, pow((double)nbHiddenNodes, -0.5));
W_ho = cmatrix_create(nbOutputs, nbHiddenNodes);
cmatrix_randomize_gaussian_inplace(W_ho, pow((double)nbOutputs, -0.5));
bias_h.resize(nbHiddenNodes);
cvector_randomize_gaussian_inplace(bias_h, pow((double)nbHiddenNodes, -0.5));
bias_o.resize(nbOutputs);
cvector_randomize_gaussian_inplace(bias_o, pow((double)nbOutputs, -0.5));
}
// construct the Neural Network by reading a text file
NeuralNetwork(const char *filename) {
std::ifstream file(filename);
int actId = 0;
file >> actId;
setActivator(actId);
file >> nbInputs >> nbHiddenNodes >> nbOutputs >> learnRate;
W_ih = cmatrix_create(nbHiddenNodes, nbInputs);
for (size_t i = 0; i < nbHiddenNodes; i++) {
for (size_t j = 0; j < nbInputs; j++) {
file >> W_ih[i][j];
}
}
bias_h.resize(nbHiddenNodes);
for (size_t i = 0; i < bias_h.size(); i++) {
file >> bias_h[i];
}
W_ho = cmatrix_create(nbOutputs, nbHiddenNodes);
for (size_t i = 0; i < nbOutputs; i++) {
for (size_t j = 0; j < nbHiddenNodes; j++) {
file >> W_ho[i][j];
}
}
bias_o.resize(nbOutputs);
for (size_t i = 0; i < bias_o.size(); i++) {
file >> bias_o[i];
}
}
void setActivator(int activatorId_ = 0) {
activatorId = activatorId_;
if (activatorId == SIGMOID_ACTIVATOR_ID) {
Activatorfunc = sigmoid;
dActivatorfunc = dsigmoid;
invActivatorfunc = invSigmoid;
} else if (activatorId == BINARY_STEP_ACTIVATOR_ID) {
Activatorfunc = binary;
dActivatorfunc = dBinary;
invActivatorfunc = invBinary;
} else if (activatorId == RELU_ACTIVATOR_ID) {
Activatorfunc = reLU;
dActivatorfunc = dReLU;
invActivatorfunc = invReLU;
} else if (activatorId == TANH_ACTIVATOR_ID) {
Activatorfunc = Tanh;
dActivatorfunc = dTanh;
invActivatorfunc = invTanh;
} else if (activatorId == IDENTITY_ACTIVATOR_ID) {
Activatorfunc = identity;
dActivatorfunc = dIdentity;
invActivatorfunc = invIdentity;
} else { // default activator is sigmoid
Activatorfunc = sigmoid;
dActivatorfunc = dsigmoid;
invActivatorfunc = invSigmoid;
}
}
void query(cvector &inputs, cvector &outputs) {
if (inputs.size() != nbInputs) {
std::cerr << "@NeuralNetwork::predict, size of input vector doesn't match\n";
}
if (outputs.size() != nbOutputs) {
std::cerr << "@NeuralNetwork::predict, size of output vector doesn't match\n";
}
cvector H = cmatrix_mult(W_ih, inputs);
cvector_add_inplace(H, bias_h);
cvector_map_inplace(H, Activatorfunc);
outputs = cmatrix_mult(W_ho, H);
cvector_add_inplace(outputs, bias_o);
cvector_map_inplace(outputs, Activatorfunc);
}
void train(cvector &inputs, cvector &targets) {
if (inputs.size() != nbInputs) {
std::cerr << "@NeuralNetwork::train, size of input vector doesn't match\n";
}
if (targets.size() != nbOutputs) {
std::cerr << "@NeuralNetwork::train, size of target vector doesn't match\n";
}
// feedforward
cvector H = cmatrix_mult(W_ih, inputs);
cvector_add_inplace(H, bias_h);
cvector_map_inplace(H, Activatorfunc);
cvector outputs = cmatrix_mult(W_ho, H);
cvector_add_inplace(outputs, bias_o);
cvector_map_inplace(outputs, Activatorfunc);
// errors in the output layer
cvector output_errors = cvector_substract(targets, outputs);
// gradient_ho
cvector grad_ho(nbOutputs);
for (size_t i = 0; i < nbOutputs; i++) {
grad_ho[i] = learnRate * output_errors[i] * dActivatorfunc(outputs[i]);
}
// delta_ho
cmatrix delta_ho = cmatrix_create(nbOutputs, nbHiddenNodes);
for (size_t i = 0; i < nbOutputs; i++) {
for (size_t j = 0; j < nbHiddenNodes; j++) {
delta_ho[i][j] = grad_ho[i] * H[j];
}
}
// corrections
cmatrix_add_inplace(W_ho, delta_ho);
for (size_t i = 0; i < bias_o.size(); i++) {
bias_o[i] += grad_ho[i];
}
// errors in the hidden layer: W_ho^T * output_errors
cvector hidden_errors(nbHiddenNodes);
for (size_t i = 0; i < nbHiddenNodes; i++) {
double sum = 0.0;
for (size_t j = 0; j < nbOutputs; j++) {
sum += W_ho[j][i] * output_errors[j];
}
hidden_errors[i] = sum;
}
// gradient_ih
cvector grad_ih(nbHiddenNodes);
for (size_t i = 0; i < nbHiddenNodes; i++) {
grad_ih[i] = learnRate * hidden_errors[i] * dActivatorfunc(H[i]);
}
// delta_ih
cmatrix delta_ih = cmatrix_create(nbHiddenNodes, nbInputs);
for (size_t i = 0; i < nbHiddenNodes; i++) {
for (size_t j = 0; j < nbInputs; j++) {
delta_ih[i][j] = grad_ih[i] * inputs[j];
}
}
// corrections
cmatrix_add_inplace(W_ih, delta_ih);
for (size_t i = 0; i < bias_h.size(); i++) {
bias_h[i] += grad_ih[i];
}
}
void backQuery(cvector &ouputs, cvector &inputs) {
// todo
}
// save the current Neural Network (may be already trained) into a text file
void save(const char *filename) {
std::ofstream file(filename);
file << activatorId << '\n';
file << nbInputs << ' ' << nbHiddenNodes << ' ' << nbOutputs << ' ' << learnRate << '\n';
for (size_t i = 0; i < nbHiddenNodes; i++) {
for (size_t j = 0; j < nbInputs; j++) {
file << W_ih[i][j] << ' ';
}
file << '\n';
}
file << '\n';
for (size_t i = 0; i < bias_h.size(); i++) {
file << bias_h[i] << '\n';
}
file << '\n';
for (size_t i = 0; i < nbOutputs; i++) {
for (size_t j = 0; j < nbHiddenNodes; j++) {
file << W_ho[i][j] << ' ';
}
file << '\n';
}
file << '\n';
for (size_t i = 0; i < bias_o.size(); i++) {
file << bias_o[i] << '\n';
}
file << '\n';
}
};
#endif /* end of include guard: NN_HPP */