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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694 | /*************************************************************************
*
* Project
* _____ _____ __ __ _____
* / ____| __ \| \/ | __ \
* ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
* / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
*| (_) | |_) | __/ | | | |__| | | | | | | |
* \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
* | |
* |_|
*
* Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
*
* This software is licensed as described in the file LICENSE, which
* you should have received as part of this distribution. The terms
* among other details are referenced in the official documentation
* seen here : https://akielaries.github.io/openGPMP/ along with
* important files seen in this project.
*
* You may opt to use, copy, modify, merge, publish, distribute
* and/or sell copies of the Software, and permit persons to whom
* the Software is furnished to do so, under the terms of the
* LICENSE file. As this is an Open Source effort, all implementations
* must be of the same methodology.
*
*
*
* This software is distributed on an AS IS basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied.
*
************************************************************************/
#include <algorithm>
#include <ctime>
#include <fstream>
#include <iostream>
#include <openGPMP/ml/encoder.hpp>
#include <random>
#include <vector>
void gpmp::ml::AutoEncoder::save(const std::string &filename) const {
std::ofstream file(filename, std::ios::out | std::ios::binary);
if (file.is_open()) {
file.write(reinterpret_cast<const char *>(&weights_input_hidden[0][0]),
weights_input_hidden.size() *
weights_input_hidden[0].size() * sizeof(double));
file.write(reinterpret_cast<const char *>(&weights_hidden_output[0][0]),
weights_hidden_output.size() *
weights_hidden_output[0].size() * sizeof(double));
file.close();
std::cout << "Model saved successfully." << std::endl;
} else {
std::cerr << "Unable to open the file for saving." << std::endl;
}
}
void gpmp::ml::AutoEncoder::load(const std::string &filename) {
std::ifstream file(filename, std::ios::in | std::ios::binary);
if (file.is_open()) {
file.read(reinterpret_cast<char *>(&weights_input_hidden[0][0]),
weights_input_hidden.size() * weights_input_hidden[0].size() *
sizeof(double));
file.read(reinterpret_cast<char *>(&weights_hidden_output[0][0]),
weights_hidden_output.size() *
weights_hidden_output[0].size() * sizeof(double));
file.close();
std::cout << "Model loaded successfully." << std::endl;
} else {
std::cerr << "Unable to open the file for loading." << std::endl;
}
}
void gpmp::ml::AutoEncoder::lrate_set(double initial_rate) {
learning_rate = initial_rate;
}
void gpmp::ml::AutoEncoder::lrate_update(int epoch) {
// reduce the learning rate by half every N epochs
const int decay_interval = 10;
if (epoch % decay_interval == 0) {
learning_rate /= 2.0;
std::cout << "Learning rate updated to: " << learning_rate
<< " at epoch " << epoch << std::endl;
}
// TODO?
}
gpmp::ml::AutoEncoder::AutoEncoder(int in_size,
int h_size,
int out_size,
double l_rate)
: input_size(in_size), hidden_size(h_size), output_size(out_size),
learning_rate(l_rate) {
// initialize weights randomly
weights_input_hidden.resize(input_size, std::vector<double>(hidden_size));
weights_hidden_output.resize(hidden_size, std::vector<double>(output_size));
for (int i = 0; i < input_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
// random values between 0 and 1
weights_input_hidden[i][j] = (rand() % 1000) / 1000.0;
}
}
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < output_size; ++j) {
weights_hidden_output[i][j] = (rand() % 1000) / 1000.0;
}
}
}
std::vector<double>
gpmp::ml::AutoEncoder::sigmoid(const std::vector<double> &x) {
std::vector<double> result;
for (double val : x) {
result.push_back(1.0 / (1.0 + exp(-val)));
}
return result;
}
std::vector<double>
gpmp::ml::AutoEncoder::forward(const std::vector<double> &input) {
// forward passes
std::vector<double> hidden(hidden_size);
std::vector<double> output(output_size);
// calculate hidden layer values
for (int i = 0; i < hidden_size; ++i) {
hidden[i] = 0;
for (int j = 0; j < input_size; ++j) {
hidden[i] += input[j] * weights_input_hidden[j][i];
}
hidden[i] = sigmoid({hidden[i]})[0];
}
// calculate output layer values
for (int i = 0; i < output_size; ++i) {
output[i] = 0;
for (int j = 0; j < hidden_size; ++j) {
output[i] += hidden[j] * weights_hidden_output[j][i];
}
output[i] = sigmoid({output[i]})[0];
}
return output;
}
void gpmp::ml::AutoEncoder::train(
const std::vector<std::vector<double>> &training_data,
int epochs) {
for (int epoch = 0; epoch < epochs; ++epoch) {
for (const auto &input : training_data) {
// forward pass
std::vector<double> hidden(hidden_size);
std::vector<double> output(output_size);
// calculate hidden layer values
for (int i = 0; i < hidden_size; ++i) {
hidden[i] = 0;
for (int j = 0; j < input_size; ++j) {
hidden[i] += input[j] * weights_input_hidden[j][i];
}
hidden[i] = sigmoid({hidden[i]})[0];
}
// calculate output layer values
for (int i = 0; i < output_size; ++i) {
output[i] = 0;
for (int j = 0; j < hidden_size; ++j) {
output[i] += hidden[j] * weights_hidden_output[j][i];
}
output[i] = sigmoid({output[i]})[0];
}
// backward pass (gradient descent)
for (int i = 0; i < output_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
weights_hidden_output[j][i] -=
learning_rate * (output[i] - input[i]) * hidden[j];
}
}
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < input_size; ++j) {
double error = 0;
for (int k = 0; k < output_size; ++k) {
error += (output[k] - input[k]) *
weights_hidden_output[i][k];
}
weights_input_hidden[j][i] -= learning_rate * error *
input[j] * (1 - hidden[i]) *
hidden[i];
}
}
}
}
}
void gpmp::ml::AutoEncoder::display() {
std::cout << "Input to Hidden Weights:\n";
for (int i = 0; i < input_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
std::cout << weights_input_hidden[i][j] << " ";
}
std::cout << "\n";
}
std::cout << "\nHidden to Output Weights:\n";
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < output_size; ++j) {
std::cout << weights_hidden_output[i][j] << " ";
}
std::cout << "\n";
}
}
gpmp::ml::SparseAutoEncoder::SparseAutoEncoder(int in_size,
int h_size,
int out_size,
double l_rate,
double s_weight,
double s_target)
: AutoEncoder(in_size, h_size, out_size, l_rate), sparsity_weight(s_weight),
sparsity_target(s_target) {
}
void gpmp::ml::SparseAutoEncoder::train(
const std::vector<std::vector<double>> &training_data,
int epochs) {
const double SPARSITY_TARGET_DECAY = 0.1;
for (int epoch = 0; epoch < epochs; ++epoch) {
for (const auto ¤t_input : training_data) {
// forward pass
std::vector<double> hidden = forward(current_input);
// backward pass (gradient descent)
for (int i = 0; i < output_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
weights_hidden_output[j][i] -=
learning_rate * (hidden[i] - current_input[i]) *
hidden[j];
}
}
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < input_size; ++j) {
double error = 0;
for (int k = 0; k < output_size; ++k) {
error += (hidden[k] - current_input[k]) *
weights_hidden_output[i][k];
}
double sparsity_term =
sparsity_weight * (sparsity_target - hidden[i]);
weights_input_hidden[j][i] -=
learning_rate * (error + sparsity_term) *
current_input[j] * (1 - hidden[i]) * hidden[i];
}
}
for (int i = 0; i < hidden_size; ++i) {
double average_activation = 0.0;
for (const auto &input : training_data) {
std::vector<double> current_hidden = forward(input);
average_activation += current_hidden[i];
}
average_activation /= training_data.size();
sparsity_target =
(1.0 - SPARSITY_TARGET_DECAY) * sparsity_target +
SPARSITY_TARGET_DECAY * average_activation;
}
}
}
}
gpmp::ml::DenoisingAutoEncoder::DenoisingAutoEncoder(int in_size,
int h_size,
int out_size,
double l_rate,
double c_level)
: AutoEncoder(in_size, h_size, out_size, l_rate),
corruption_level(c_level) {
}
void gpmp::ml::DenoisingAutoEncoder::train(
const std::vector<std::vector<double>> &training_data,
int epochs) {
std::srand(std::time(0));
for (int epoch = 0; epoch < epochs; ++epoch) {
for (const auto &input : training_data) {
// add noise to the input data
std::vector<double> noisy_input = input;
for (double &val : noisy_input) {
if ((std::rand() / (RAND_MAX + 1.0)) < corruption_level) {
// set to zero with probability corruption_level
val = 0.0;
}
}
// forward pass
std::vector<double> hidden = forward(noisy_input);
// backward pass (gradient descent)
for (int i = 0; i < output_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
weights_hidden_output[j][i] -=
learning_rate * (hidden[i] - input[i]) * hidden[j];
}
}
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < input_size; ++j) {
double error = 0;
for (int k = 0; k < output_size; ++k) {
error += (hidden[k] - input[k]) *
weights_hidden_output[i][k];
}
weights_input_hidden[j][i] -= learning_rate * error *
noisy_input[j] *
(1 - hidden[i]) * hidden[i];
}
}
}
}
}
gpmp::ml::ContractiveAutoEncoder::ContractiveAutoEncoder(int in_size,
int h_size,
int out_size,
double l_rate,
double c_weight)
: AutoEncoder(in_size, h_size, out_size, l_rate),
contractive_weight(c_weight) {
}
void gpmp::ml::ContractiveAutoEncoder::train(
const std::vector<std::vector<double>> &training_data,
int epochs) {
for (int epoch = 0; epoch < epochs; ++epoch) {
for (const auto &input : training_data) {
// forward pass
std::vector<double> hidden = forward(input);
// backward pass (gradient descent)
for (int i = 0; i < output_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
weights_hidden_output[j][i] -=
learning_rate * (hidden[i] - input[i]) * hidden[j];
}
}
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < input_size; ++j) {
double error = 0;
for (int k = 0; k < output_size; ++k) {
error += (hidden[k] - input[k]) *
weights_hidden_output[i][k];
}
double contractive_term =
contractive_weight * hidden[i] * (1 - hidden[i]);
weights_input_hidden[j][i] -=
learning_rate * (error + contractive_term) * input[j] *
(1 - hidden[i]) * hidden[i];
}
}
}
}
}
gpmp::ml::MDLAutoEncoder::MDLAutoEncoder(int in_size,
int h_size,
int out_size,
double l_rate,
double m_wt)
: AutoEncoder(in_size, h_size, out_size, l_rate), mdl_weight(m_wt) {
}
void gpmp::ml::MDLAutoEncoder::train(
const std::vector<std::vector<double>> &training_data,
int epochs) {
for (int epoch = 0; epoch < epochs; ++epoch) {
for (const auto &input : training_data) {
// forward pass
std::vector<double> hidden = forward(input);
// backward pass (gradient descent)
for (int i = 0; i < output_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
weights_hidden_output[j][i] -=
learning_rate * (hidden[i] - input[i]) * hidden[j];
}
}
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < input_size; ++j) {
double error = 0;
for (int k = 0; k < output_size; ++k) {
error += (hidden[k] - input[k]) *
weights_hidden_output[i][k];
}
double mdl_term = mdl_weight * log(1.0 + fabs(error));<--- Expression 'log(1 + x)' can be replaced by 'log1p(x)' to avoid loss of precision.
weights_input_hidden[j][i] -=
learning_rate * (error + mdl_term) * input[j] *
(1 - hidden[i]) * hidden[i];
}
}
}
}
}
gpmp::ml::ConcreteAutoEncoder::ConcreteAutoEncoder(int in_size,
int h_size,
int out_size,
double l_rate,
double temp)
: AutoEncoder(in_size, h_size, out_size, l_rate), temperature(temp) {
}
void gpmp::ml::ConcreteAutoEncoder::train(
const std::vector<std::vector<double>> &training_data,
int epochs) {
std::default_random_engine generator;
std::uniform_real_distribution<double> uniform_distribution(0.0, 1.0);
for (int epoch = 0; epoch < epochs; ++epoch) {
for (const auto &input : training_data) {
// forward pass with Concrete distribution
std::vector<double> hidden;
for (int i = 0; i < hidden_size; ++i) {
double u = uniform_distribution(generator);
// Gumbel noise
double g = -log(-log(u));
double s = (input[i] + g) / temperature;
double p = 1.0 / (1.0 + exp(-s));
hidden.push_back(p);
}
// backward pass (gradient descent)
for (int i = 0; i < output_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
weights_hidden_output[j][i] -=
learning_rate * (hidden[i] - input[i]) * hidden[j];
}
}
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < input_size; ++j) {
double error = 0;
for (int k = 0; k < output_size; ++k) {
error += (hidden[k] - input[k]) *
weights_hidden_output[i][k];
}
weights_input_hidden[j][i] -= learning_rate * error *
input[j] * (1 - hidden[i]) *
hidden[i];
}
}
}
}
}
gpmp::ml::VariationalAutoEncoder::VariationalAutoEncoder(int in_size,
int h_size,
int out_size,
double l_rate)
: AutoEncoder(in_size, h_size, out_size, l_rate) {
}
double gpmp::ml::VariationalAutoEncoder::sample_dist() {
static std::default_random_engine generator;
static std::normal_distribution<double> distribution(0.0, 1.0);
return distribution(generator);
}
double gpmp::ml::VariationalAutoEncoder::reparameterize(double mean,
double log_variance) {
double standard_normal_sample = sample_dist();
return mean + exp(0.5 * log_variance) * standard_normal_sample;
}
std::vector<double>
gpmp::ml::VariationalAutoEncoder::encoder(const std::vector<double> &input) {
std::vector<double> hidden(hidden_size);
for (int i = 0; i < hidden_size; ++i) {
hidden[i] = 0;
for (int j = 0; j < input_size; ++j) {
hidden[i] += input[j] * weights_input_hidden[j][i];
}
hidden[i] = sigmoid({hidden[i]})[0];
}
return hidden;
}
std::vector<double> gpmp::ml::VariationalAutoEncoder::decoder(
const std::vector<double> &hidden_sampled) {
std::vector<double> output(output_size);
for (int i = 0; i < output_size; ++i) {
output[i] = 0;
for (int j = 0; j < hidden_size; ++j) {
output[i] += hidden_sampled[j] * weights_hidden_output[j][i];
}
output[i] = sigmoid({output[i]})[0];
}
return output;
}
void gpmp::ml::VariationalAutoEncoder::gradient_descent(
const std::vector<double> &input,
const std::vector<double> &output,
const std::vector<double> &hidden_sampled) {
for (int i = 0; i < output_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
weights_hidden_output[j][i] -=
learning_rate * (output[i] - input[i]) * hidden_sampled[j];
}
}
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < input_size; ++j) {
double error = 0;
for (int k = 0; k < output_size; ++k) {
error += (output[k] - input[k]) * weights_hidden_output[i][k];
}
double hidden_gradient =
hidden_sampled[i] * (1 - hidden_sampled[i]);
// derivative of softplus
double log_variance_gradient =
1.0 / (1.0 + exp(-hidden_log_variance[i]));
weights_input_hidden[j][i] -=
learning_rate *
(error * hidden_gradient +
(hidden_sampled[i] - hidden_mean[i]) * hidden_gradient +
(hidden_log_variance[i] - log_variance_gradient) *
hidden_gradient) *
input[j];
}
}
}
void gpmp::ml::VariationalAutoEncoder::train(
const std::vector<std::vector<double>> &training_data,
int epochs) {
std::default_random_engine generator;<--- Unused variable: generator
std::normal_distribution<double> normal_distribution(0.0, 1.0);
std::vector<double> hidden_sampled;
for (int epoch = 0; epoch < epochs; ++epoch) {
for (const auto &input : training_data) {
// forward pass (encoder)
hidden_mean = encoder(input);
hidden_log_variance = encoder(input);
for (int i = 0; i < hidden_size; ++i) {
hidden_sampled.push_back(
reparameterize(hidden_mean[i], hidden_log_variance[i]));
}
// forward pass (decoder)
std::vector<double> output = decoder(hidden_sampled);
// backward pass (gradient descent)
gradient_descent(input, output, hidden_sampled);
}
}
}
gpmp::ml::RecurrentAutoEncoder::RecurrentAutoEncoder(int in_size,
int h_size,
int out_size,
double l_rate)
: AutoEncoder(in_size, h_size, out_size, l_rate),
weights_recurrent(h_size, std::vector<double>(h_size, 0.0)) {
}
void gpmp::ml::RecurrentAutoEncoder::train(
const std::vector<std::vector<double>> &training_data,
int epochs) {
for (int epoch = 0; epoch < epochs; ++epoch) {
std::vector<double> previous_hidden(hidden_size, 0.0);
for (const auto &input : training_data) {
// forward pass
std::vector<double> hidden = recurr_fwd(input, previous_hidden);
std::vector<double> output = forward(hidden);
// backward pass (gradient descent)
for (int i = 0; i < output_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
weights_hidden_output[j][i] -=
learning_rate * (output[i] - input[i]) * hidden[j];
}
}
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < input_size; ++j) {
double error = 0;
for (int k = 0; k < output_size; ++k) {
error += (output[k] - input[k]) *
weights_hidden_output[i][k];
}
weights_input_hidden[j][i] -= learning_rate * error *
input[j] * (1 - hidden[i]) *
hidden[i];
}
}
// recurrent weights update
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
weights_recurrent[j][i] -=
learning_rate * (hidden[i] - previous_hidden[i]) *
hidden[j];
}
}
previous_hidden = hidden;
}
}
}
std::vector<double> gpmp::ml::RecurrentAutoEncoder::recurr_fwd(
const std::vector<double> &input,
const std::vector<double> &previous_hidden) {
std::vector<double> recurrent_input(hidden_size, 0.0);
// sum the weighted contributions from the current input and the previous
// hidden state
for (int i = 0; i < hidden_size; ++i) {
recurrent_input[i] = 0.0;
for (int j = 0; j < input_size; ++j) {
recurrent_input[i] += weights_input_hidden[j][i] * input[j];
}
for (int j = 0; j < hidden_size; ++j) {
recurrent_input[i] += weights_recurrent[j][i] * previous_hidden[j];
}
// activation function
// recurrent_input[i] = 1.0 / (1.0 + std::exp(-recurrent_input[i]));
recurrent_input[i] = sigmoid({recurrent_input[i]})[0];
}
return recurrent_input;
}
gpmp::ml::FullAutoEncoder::FullAutoEncoder(int in_size,
int h_size,
int out_size,
double l_rate)
: AutoEncoder(in_size, h_size, out_size, l_rate) {
}
void gpmp::ml::FullAutoEncoder::train(
const std::vector<std::vector<double>> &training_data,
int epochs) {
for (int epoch = 0; epoch < epochs; ++epoch) {
for (const auto &input : training_data) {
// forward pass
std::vector<double> hidden = forward(input);
std::vector<double> output = forward(hidden);
// backward pass (gradient descent)
for (int i = 0; i < output_size; ++i) {
for (int j = 0; j < hidden_size; ++j) {
weights_hidden_output[j][i] -=
learning_rate * (output[i] - input[i]) * hidden[j];
}
}
for (int i = 0; i < hidden_size; ++i) {
for (int j = 0; j < input_size; ++j) {
double error = 0;
for (int k = 0; k < output_size; ++k) {
error += (output[k] - input[k]) *
weights_hidden_output[i][k];
}
weights_input_hidden[j][i] -= learning_rate * error *
input[j] * (1 - hidden[i]) *
hidden[i];
}
}
}
}
}
|