-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorMinimizationProcedure.h
464 lines (391 loc) · 15.3 KB
/
ErrorMinimizationProcedure.h
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
456
457
458
459
460
461
462
463
464
/*
* Recursive Neural Networks: neural networks for data structures
*
* Copyright (C) 2018 Alessandro Vullo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
Templatized Strategy Pattern implementation
of different Error Minimization Procedures.
*/
#ifndef _ERROR_MINIMIZATION_PROCEDURE_H
#define _ERROR_MINIMIZATION_PROCEDURE_H
#include <vector>
#include <iostream>
using std::cout;
using std::endl;
/* Simple Gradient Descent */
class GradientDescent {
// Simply update networks weights in the opposite direction
// of the current gradient stored in the net and with a
// distance defined by learning rate.
bool _ss_tr, _ios_tr;
int _norient, _n, _v, _m, _r, _s;
std::vector<int> _lnunits;
public:
template<typename T1, typename T2, typename T3, template<typename, typename, typename> class RNN>
void setInternals(RNN<T1, T2, T3>* const rnn);
template<typename T1, typename T2, typename T3, template<typename, typename, typename> class RNN>
void updateWeights(RNN<T1, T2, T3>* const rnn, double, double = 0.0);
};
template<typename T1, typename T2, typename T3, template<typename, typename, typename> class RNN>
void GradientDescent::setInternals(RNN<T1, T2, T3>* const rnn) {
// Simply synchronize private member with those of recursive network
_norient = rnn->_norient;
_ss_tr = rnn->_ss_tr;
_ios_tr = rnn->_ios_tr;
_n = rnn->_n, _v = rnn->_v, _m = rnn->_m, _r = rnn->_r, _s = rnn->_s;
_lnunits = rnn->_lnunits;
}
template<typename T1, typename T2, typename T3, template<typename, typename, typename> class RNN>
void GradientDescent::updateWeights(RNN<T1, T2, T3>* const rnn, double _learning_rate, double) {
// rnn (the recursive network) store gradient components, so to obtain
// weight update rule change the sign of these components and multiply
// by the learning rate
if(_ss_tr) {
// begin with layers of the MLP output function
for(int i=0; i<_norient*_m; i++) {
for(int j=0; j<_lnunits[_r]; j++) {
// Save parameters in case we make a bad move.
rnn->_prev_g_layers_w[0][i][j] = rnn->_g_layers_w[0][i][j];
rnn->_g_layers_w[0][i][j] += -_learning_rate * rnn->_g_layers_gradient_w[0][i][j];
}
}
// this could go inside the previous loop
for(int j=0; j<_lnunits[_r]; j++) {
rnn->_prev_g_layers_w[0][_norient*_m][j] = rnn->_g_layers_w[0][_norient*_m][j];
rnn->_g_layers_w[0][_norient*_m][j] += -_learning_rate * rnn->_g_layers_gradient_w[0][_norient*_m][j];
}
for(int k=1; k<_s; k++) {
for(int i=0; i<_lnunits[_r+k-1]+1; i++) {
for(int j=0; j<_lnunits[_r+k]; j++) {
rnn->_prev_g_layers_w[k][i][j] = rnn->_g_layers_w[k][i][j];
rnn->_g_layers_w[k][i][j] += -_learning_rate * rnn->_g_layers_gradient_w[k][i][j];
}
}
}
}
if(_ios_tr) {
// proceed with layers of the h map
for(int i=0; i<_norient*_m+_n; i++) {
for(int j=0; j<_lnunits[_r]; j++) {
rnn->_prev_h_layers_w[0][i][j] = rnn->_h_layers_w[0][i][j];
rnn->_h_layers_w[0][i][j] += -_learning_rate * rnn->_h_layers_gradient_w[0][i][j];
}
}
// this could inside the previous loop
for(int j=0; j<_lnunits[_r]; j++) {
rnn->_prev_h_layers_w[0][_norient*_m + _n][j] = rnn->_h_layers_w[0][_norient*_m + _n][j];
rnn->_h_layers_w[0][_norient*_m + _n][j] += -_learning_rate * rnn->_h_layers_gradient_w[0][_norient*_m + _n][j];
}
for(int k=1; k<_s; k++) {
for(int i=0; i<_lnunits[_r+k-1]+1; i++) {
for(int j=0; j<_lnunits[_r+k]; j++) {
rnn->_prev_h_layers_w[k][i][j] = rnn->_h_layers_w[k][i][j];
rnn->_h_layers_w[k][i][j] += -_learning_rate * rnn->_h_layers_gradient_w[k][i][j];
}
}
}
}
// proceed with the folding layers
for(int o=0; o<_norient; ++o) {
for(int i=0; i<(_n+_v*_m) + 1; i++) {
for(int j=0; j<_lnunits[0]; j++) {
rnn->_prev_layers_w[o][0][i][j] = rnn->_layers_w[o][0][i][j];
rnn->_layers_w[o][0][i][j] += -_learning_rate * rnn->_layers_gradient_w[o][0][i][j];
}
}
for(int k=1; k<_r; k++) {
for(int i=0; i<_lnunits[k-1]+1; i++) {
for(int j=0; j<_lnunits[k]; j++) {
rnn->_prev_layers_w[o][k][i][j] = rnn->_layers_w[o][k][i][j];
rnn->_layers_w[o][k][i][j] += -_learning_rate * rnn->_layers_gradient_w[o][k][i][j];
}
}
}
}
}
/* Gradient Descent with momentum */
class MGradientDescent {
// Store previuos step weights delta values and update
// weights with net current gradient and these values.
double**** _layers_old_deltas_w;
double*** _g_layers_old_deltas_w;
double*** _h_layers_old_deltas_w;
bool _ss_tr, _ios_tr;
int _norient, _n, _v, _m, _r, _s;
std::vector<int> _lnunits;
void allocFoldingPart(double**** layers_w) {
(*layers_w) = new double**[_r];
// Allocate weights and gradient components matrixes
// for f folding part. Connections from input layer
// have special dimensions.
(*layers_w)[0] = new double*[(_n + _v * _m) + 1];
for(int i=0; i<(_n+_v*_m) + 1; i++) {
(*layers_w)[0][i] = new double[_lnunits[0]];
// Reset gradient for corresponding weights
memset((*layers_w)[0][i], 0, _lnunits[0] * sizeof(double));
}
// Allocate f weights&gradient matrixes for f folding part.
for(int k=1; k<_r; k++) {
// Allocate space for weight&delta matrix between layer i-1 and i.
// Automatically include space for threshold unit in layer i-1
(*layers_w)[k] = new double*[_lnunits[k-1] + 1];
for(int i=0; i<_lnunits[k-1]+1; i++) {
(*layers_w)[k][i] = new double[_lnunits[k]];
// Reset f gradient components for corresponding weights
memset((*layers_w)[k][i], 0, (_lnunits[k])*sizeof(double));
}
}
}
void allocSSPart() {
_g_layers_old_deltas_w = new double**[_s];
// Allocate weights and gradient components matrixes
// for g transforming part. Connections from input layers
// have special dimensions.
_g_layers_old_deltas_w[0] = new double*[_norient*_m + 1];
for(int i=0; i<_norient*_m + 1; i++) {
_g_layers_old_deltas_w[0][i] = new double[_lnunits[_r]];
// Reset gradient for corresponding weights
memset(_g_layers_old_deltas_w[0][i], 0, _lnunits[_r] * sizeof(double));
}
// Allocate weights&gradient matrixes for g transforming part
for(int k=1; k<_s; k++) {
// Allocate space for weight&gradient matrixes between layer i-1 and i.
// Automatically include space for threshold unit in layer i-1
_g_layers_old_deltas_w[k] = new double*[_lnunits[_r+k-1] + 1];
for(int i=0; i<_lnunits[_r+k-1]+1; i++) {
_g_layers_old_deltas_w[k][i] = new double[_lnunits[_r+k]];
// Reset g gradient components for corresponding weights
memset(_g_layers_old_deltas_w[k][i], 0, (_lnunits[_r+k])*sizeof(double));
}
}
}
void allocIOSPart() {
_h_layers_old_deltas_w = new double**[_s];
// Allocate weights and gradient components matrixes
// for h map. Connections from input layers have special dimensions.
_h_layers_old_deltas_w[0] = new double*[_norient*_m + _n + 1];
for(int i=0; i<_norient*_m + _n + 1; i++) {
_h_layers_old_deltas_w[0][i] = new double[_lnunits[_r]];
// Reset gradient for corresponding weights
memset(_h_layers_old_deltas_w[0][i], 0, _lnunits[_r] * sizeof(double));
}
// Allocate weights&gradient matrixes for h map
for(int k=1; k<_s; k++) {
// Allocate space for weight&gradient matrixes between layer i-1 and i.
// Automatically include space for threshold unit in layer i-1
_h_layers_old_deltas_w[k] = new double*[_lnunits[_r+k-1] + 1];
for(int i=0; i<_lnunits[_r+k-1]+1; i++) {
_h_layers_old_deltas_w[k][i] = new double[_lnunits[_r+k]];
// Reset h gradient components for corresponding weights
memset(_h_layers_old_deltas_w[k][i], 0, (_lnunits[_r+k])*sizeof(double));
}
}
}
void deallocFoldingPart(double**** layers_w) {
if((*layers_w)[0]) {
for(int i=0; i<(_n+_v*_m) + 1; i++) {
if((*layers_w)[0][i])
delete[] (*layers_w)[0][i];
(*layers_w)[0][i] = 0;
}
delete[] (*layers_w)[0];
(*layers_w)[0] = 0;
}
for(int k=1; k<_r; k++) {
if((*layers_w)[k]) {
for(int i=0; i<_lnunits[k-1]+1; i++) {
if((*layers_w)[k][i])
delete[] (*layers_w)[k][i];
(*layers_w)[k][i] = 0;
}
delete[] (*layers_w)[k];
(*layers_w)[k] = 0;
}
}
delete[] (*layers_w);
(*layers_w) = 0;
}
void deallocSSPart() {
if(_g_layers_old_deltas_w[0]) {
for(int i=0; i<_norient*_m + 1; i++) {
if(_g_layers_old_deltas_w[0][i])
delete[] _g_layers_old_deltas_w[0][i];
_g_layers_old_deltas_w[0][i] = 0;
}
delete[] _g_layers_old_deltas_w[0];
_g_layers_old_deltas_w[0] = 0;
}
for(int k=1; k<_s; k++) {
if(_g_layers_old_deltas_w[k]) {
for(int i=0; i<_lnunits[_r+k-1]+1; i++) {
if(_g_layers_old_deltas_w[k][i])
delete[] _g_layers_old_deltas_w[k][i];
_g_layers_old_deltas_w[k][i] = 0;
}
delete[] _g_layers_old_deltas_w[k];
_g_layers_old_deltas_w[k] = 0;
}
}
delete[] _g_layers_old_deltas_w;
_g_layers_old_deltas_w = 0;
}
void deallocIOSPart() {
if(_h_layers_old_deltas_w[0]) {
for(int i=0; i<_norient*_m + _n + 1; i++) {
if(_h_layers_old_deltas_w[0][i])
delete[] _h_layers_old_deltas_w[0][i];
_h_layers_old_deltas_w[0][i] = 0;
}
delete[] _h_layers_old_deltas_w[0];
_h_layers_old_deltas_w[0] = 0;
}
for(int k=1; k<_s; k++) {
if(_h_layers_old_deltas_w[k]) {
for(int i=0; i<_lnunits[_r+k-1]+1; i++) {
if(_h_layers_old_deltas_w[k][i])
delete[] _h_layers_old_deltas_w[k][i];
_h_layers_old_deltas_w[k][i] = 0;
}
delete[] _h_layers_old_deltas_w[k];
_h_layers_old_deltas_w[k] = 0;
}
}
delete[] _h_layers_old_deltas_w;
_h_layers_old_deltas_w = 0;
}
public:
~MGradientDescent();
template<typename T1, typename T2, typename T3, template<typename, typename, typename> class RNN>
void setInternals(RNN<T1, T2, T3>* const rnn);
template<typename T1, typename T2, typename T3, template<typename, typename, typename> class RNN>
void updateWeights(RNN<T1, T2, T3>* const rnn, float = .0, float = .0, float = .0);
};
MGradientDescent::~MGradientDescent() {
for(int i=0; i<_norient; ++i)
deallocFoldingPart(&(_layers_old_deltas_w[i]));
if(_ss_tr) deallocSSPart();
if(_ios_tr) deallocIOSPart();
}
template<typename T1, typename T2, typename T3, template<typename, typename, typename> class RNN>
void MGradientDescent::setInternals(RNN<T1, T2, T3>* const rnn) {
// Assume rnn constructor has initialized
// required dimension quantities.
_norient = rnn->_norient;
_ss_tr = rnn->_ss_tr; _ios_tr = rnn->_ios_tr;
_n = rnn->_n, _v = rnn->_v, _m = rnn->_m, _r = rnn->_r, _s = rnn->_s;
_lnunits = rnn->_lnunits;
_layers_old_deltas_w = new double***[_norient];
for(int i=0; i<_norient; ++i)
allocFoldingPart(&(_layers_old_deltas_w[i]));
// Finally allocate space for g and or h gradient structures
if(_ss_tr) allocSSPart();
if(_ios_tr) allocIOSPart();
}
template<typename T1, typename T2, typename T3, template<typename, typename, typename> class RNN>
void MGradientDescent::updateWeights(RNN<T1, T2, T3>* const rnn, float _learning_rate, float momentum_term, float ni) {
// rnn (the recursive network) store gradient components, so to obtain
// weight update rule change the sign of this components, multiply
// by the learning rate and add multiplication of momentum_term
// with old weights deltas.
float new_delta_w = .0;
if(_ss_tr) { // begin with layers of the MLP output function
for(int i=0; i<_norient*_m + 1; i++) {
for(int j=0; j<_lnunits[_r]; j++) {
rnn->_prev_g_layers_w[0][i][j] = rnn->_g_layers_w[0][i][j];
new_delta_w =
-_learning_rate * rnn->_g_layers_gradient_w[0][i][j] +
(momentum_term * _g_layers_old_deltas_w[0][i][j]) -
(ni * rnn->_g_layers_w[0][i][j]);
rnn->_g_layers_w[0][i][j] += new_delta_w;
_g_layers_old_deltas_w[0][i][j] = new_delta_w;
}
}
for(int k=1; k<_s; k++) {
for(int i=0; i<_lnunits[_r+k-1]+1; i++) {
for(int j=0; j<_lnunits[_r+k]; j++) {
rnn->_prev_g_layers_w[k][i][j] = rnn->_g_layers_w[k][i][j];
new_delta_w =
-_learning_rate * rnn->_g_layers_gradient_w[k][i][j] +
(momentum_term * _g_layers_old_deltas_w[k][i][j]) -
(ni * rnn->_g_layers_w[k][i][j]);;
rnn->_g_layers_w[k][i][j] += new_delta_w;
_g_layers_old_deltas_w[k][i][j] = new_delta_w;
}
}
}
}
if(_ios_tr) { // proceed with layers of the MLP h map
new_delta_w = 0.0;
for(int i=0; i<_norient*_m + _n + 1; i++) {
for(int j=0; j<_lnunits[_r]; j++) {
rnn->_prev_h_layers_w[0][i][j] = rnn->_h_layers_w[0][i][j];
new_delta_w =
-_learning_rate * rnn->_h_layers_gradient_w[0][i][j] +
(momentum_term * _h_layers_old_deltas_w[0][i][j]) -
(ni * rnn->_h_layers_w[0][i][j]);
rnn->_h_layers_w[0][i][j] += new_delta_w;
_h_layers_old_deltas_w[0][i][j] = new_delta_w;
}
}
for(int k=1; k<_s; k++) {
for(int i=0; i<_lnunits[_r+k-1]+1; i++) {
for(int j=0; j<_lnunits[_r+k]; j++) {
rnn->_prev_h_layers_w[k][i][j] = rnn->_h_layers_w[k][i][j];
new_delta_w =
-_learning_rate * rnn->_h_layers_gradient_w[k][i][j] +
(momentum_term * _h_layers_old_deltas_w[k][i][j]) -
(ni * rnn->_h_layers_w[k][i][j]);
rnn->_h_layers_w[k][i][j] += new_delta_w;
_h_layers_old_deltas_w[k][i][j] = new_delta_w;
}
}
}
}
// proceed with the folding layers
for(int o=0; o<_norient; ++o) {
for(int i=0; i<(_n+_v*_m) + 1; i++) {
for(int j=0; j<_lnunits[0]; j++) {
rnn->_prev_layers_w[o][0][i][j] = rnn->_layers_w[o][0][i][j];
new_delta_w =
-_learning_rate * rnn->_layers_gradient_w[o][0][i][j] +
(momentum_term * _layers_old_deltas_w[o][0][i][j]) -
(ni * rnn->_layers_w[o][0][i][j]);
rnn->_layers_w[o][0][i][j] += new_delta_w;
_layers_old_deltas_w[o][0][i][j] = new_delta_w;
}
}
for(int k=1; k<_r; k++) {
for(int i=0; i<_lnunits[k-1]+1; i++) {
for(int j=0; j<_lnunits[k]; j++) {
rnn->_prev_layers_w[o][k][i][j] = rnn->_layers_w[o][k][i][j];
new_delta_w =
-_learning_rate * rnn->_layers_gradient_w[o][k][i][j] +
(momentum_term * _layers_old_deltas_w[o][k][i][j]) -
(ni * rnn->_layers_w[o][k][i][j]);
rnn->_layers_w[o][k][i][j] += new_delta_w;
_layers_old_deltas_w[o][k][i][j] = new_delta_w;
}
}
}
}
}
#endif // _ERROR_MINIMIZATION_PROCEDURE_H