pmlpp/mlpp/ann/ann.cpp

930 lines
32 KiB
C++
Raw Normal View History

//
// ANN.cpp
//
// Created by Marc Melikyan on 11/4/20.
//
2023-01-24 18:12:23 +01:00
#include "ann.h"
2023-02-17 16:55:00 +01:00
2023-01-24 18:12:23 +01:00
#include "../activation/activation.h"
2023-01-24 19:00:54 +01:00
#include "../cost/cost.h"
2023-01-24 18:12:23 +01:00
#include "../regularization/reg.h"
#include "../utilities/utilities.h"
2023-02-17 16:55:00 +01:00
#include "core/log/logger.h"
#include <random>
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> MLPPANN::model_set_test(const Ref<MLPPMatrix> &X) {
2023-02-13 00:56:09 +01:00
if (!_network.empty()) {
2023-02-17 16:55:00 +01:00
Ref<MLPPHiddenLayer> layer = _network[0];
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
layer->set_input(X);
layer->forward_pass();
for (int i = 1; i < _network.size(); i++) {
layer = _network[i];
Ref<MLPPHiddenLayer> prev_layer = _network[i - 1];
layer->set_input(prev_layer->get_a());
layer->forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
_output_layer->set_input(_network.write[_network.size() - 1]->get_a());
2023-01-24 19:00:54 +01:00
} else {
2023-02-17 16:55:00 +01:00
_output_layer->set_input(X);
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
2023-02-17 16:55:00 +01:00
_output_layer->forward_pass();
2023-02-12 15:07:26 +01:00
2023-02-17 16:55:00 +01:00
return _output_layer->get_a();
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
real_t MLPPANN::model_test(const Ref<MLPPVector> &x) {
2023-02-13 00:56:09 +01:00
if (!_network.empty()) {
2023-02-17 16:55:00 +01:00
Ref<MLPPHiddenLayer> layer = _network[0];
layer->test(x);
for (int i = 1; i < _network.size(); i++) {
layer = _network[i];
Ref<MLPPHiddenLayer> prev_layer = _network[i - 1];
layer->test(prev_layer->get_a_test());
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
_output_layer->test(_network.write[_network.size() - 1]->get_a_test());
2023-01-24 19:00:54 +01:00
} else {
2023-02-17 16:55:00 +01:00
_output_layer->test(x);
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
return _output_layer->get_a_test();
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
void MLPPANN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
MLPPCost mlpp_cost;
2023-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-27 13:01:16 +01:00
real_t initial_learning_rate = learning_rate;
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 00:56:09 +01:00
learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate);
2023-02-12 15:07:26 +01:00
2023-02-13 00:56:09 +01:00
cost_prev = cost(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
ComputeGradientsResult grads = compute_gradients(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
grads.cumulative_hidden_layer_w_grad->scalar_multiply(learning_rate / _n);
grads.output_w_grad->scalar_multiply(learning_rate / _n);
2023-02-17 16:55:00 +01:00
update_parameters(grads.cumulative_hidden_layer_w_grad, grads.output_w_grad, learning_rate); // subject to change. may want bias to have this matrix too.
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
if (ui) {
2023-02-13 00:56:09 +01:00
print_ui(epoch, cost_prev, _y_hat, _output_set);
2023-01-24 19:00:54 +01:00
}
epoch++;
2023-02-17 16:55:00 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
}
2023-02-12 15:07:26 +01:00
void MLPPANN::sgd(real_t learning_rate, int max_epoch, bool ui) {
MLPPCost mlpp_cost;
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
2023-01-27 13:01:16 +01:00
real_t initial_learning_rate = learning_rate;
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_int_distribution<int> distribution(0, int(_n - 1));
Ref<MLPPVector> input_set_row_tmp;
input_set_row_tmp.instance();
input_set_row_tmp->resize(_input_set->size().x);
Ref<MLPPVector> y_hat_row_tmp;
y_hat_row_tmp.instance();
y_hat_row_tmp->resize(1);
Ref<MLPPVector> output_set_row_tmp;
output_set_row_tmp.instance();
output_set_row_tmp->resize(1);
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 00:56:09 +01:00
learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate);
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
int output_index = distribution(generator);
2023-04-29 15:07:30 +02:00
_input_set->row_get_into_mlpp_vector(output_index, input_set_row_tmp);
real_t output_element_set = _output_set->element_get(output_index);
output_set_row_tmp->element_set(0, output_element_set);
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
real_t y_hat = model_test(input_set_row_tmp);
y_hat_row_tmp->element_set(0, y_hat);
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
cost_prev = cost(y_hat_row_tmp, output_set_row_tmp);
2023-02-10 23:10:08 +01:00
2023-02-17 16:55:00 +01:00
ComputeGradientsResult grads = compute_gradients(y_hat_row_tmp, output_set_row_tmp);
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
grads.cumulative_hidden_layer_w_grad->scalar_multiply(learning_rate / _n);
grads.output_w_grad->scalar_multiply(learning_rate / _n);
2023-02-17 16:55:00 +01:00
update_parameters(grads.cumulative_hidden_layer_w_grad, grads.output_w_grad, learning_rate); // subject to change. may want bias to have this matrix too.
y_hat = model_test(input_set_row_tmp);
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
if (ui) {
2023-02-17 16:55:00 +01:00
print_ui(epoch, cost_prev, y_hat_row_tmp, output_set_row_tmp);
2023-01-24 19:00:54 +01:00
}
epoch++;
2023-02-17 16:55:00 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
void MLPPANN::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui) {
MLPPCost mlpp_cost;
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
2023-01-27 13:01:16 +01:00
real_t initial_learning_rate = learning_rate;
2023-01-24 19:00:54 +01:00
// Creating the mini-batches
2023-02-13 00:56:09 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-01-24 19:00:54 +01:00
// always evaluate the result
// always do forward pass only ONCE at end.
2023-02-10 23:10:08 +01:00
2023-02-17 16:55:00 +01:00
MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch);
2023-02-10 23:10:08 +01:00
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 00:56:09 +01:00
learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate);
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
for (int i = 0; i < n_mini_batch; i++) {
2023-02-17 16:55:00 +01:00
Ref<MLPPMatrix> current_input_batch = batches.input_sets[i];
Ref<MLPPVector> current_output_batch = batches.output_sets[i];
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> y_hat = model_set_test(current_input_batch);
cost_prev = cost(y_hat, current_output_batch);
2023-02-10 23:10:08 +01:00
2023-02-17 16:55:00 +01:00
ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
grads.cumulative_hidden_layer_w_grad->scalar_multiply(learning_rate / _n);
grads.output_w_grad->scalar_multiply(learning_rate / _n);
2023-02-17 16:55:00 +01:00
update_parameters(grads.cumulative_hidden_layer_w_grad, grads.output_w_grad, learning_rate); // subject to change. may want bias to have this matrix too.
y_hat = model_set_test(current_input_batch);
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
if (ui) {
2023-02-17 16:55:00 +01:00
print_ui(epoch, cost_prev, y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
}
}
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
void MLPPANN::momentum(real_t learning_rate, int max_epoch, int mini_batch_size, real_t gamma, bool nag, bool ui) {
class MLPPCost mlpp_cost;
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
2023-01-27 13:01:16 +01:00
real_t initial_learning_rate = learning_rate;
2023-01-24 19:00:54 +01:00
// Creating the mini-batches
2023-02-13 00:56:09 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-01-24 19:00:54 +01:00
// always evaluate the result
// always do forward pass only ONCE at end.
2023-02-10 23:10:08 +01:00
2023-02-17 16:55:00 +01:00
MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch);
2023-01-24 19:00:54 +01:00
// Initializing necessary components for Adam.
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> v_hidden;
v_hidden.instance();
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> v_output;
v_output.instance();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 00:56:09 +01:00
learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate);
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
for (int i = 0; i < n_mini_batch; i++) {
2023-02-17 16:55:00 +01:00
Ref<MLPPMatrix> current_input_batch = batches.input_sets[i];
Ref<MLPPVector> current_output_batch = batches.output_sets[i];
Ref<MLPPVector> y_hat = model_set_test(current_input_batch);
cost_prev = cost(y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
if (!_network.empty() && v_hidden->size() == Size3i()) { // Initing our tensor
v_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
if (v_output->size() == 0) {
v_output->resize(grads.output_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
if (nag) { // "Aposterori" calculation
update_parameters(v_hidden, v_output, 0); // DON'T update bias.
2023-01-24 19:00:54 +01:00
}
2023-04-30 22:31:35 +02:00
v_hidden = v_hidden->scalar_multiplyn(gamma)->addn(grads.cumulative_hidden_layer_w_grad->scalar_multiplyn(learning_rate / _n));
v_output = v_output->scalar_multiplyn(gamma)->addn(grads.output_w_grad->scalar_multiplyn(learning_rate / _n));
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
update_parameters(v_hidden, v_output, learning_rate); // subject to change. may want bias to have this matrix too.
2023-02-17 16:55:00 +01:00
y_hat = model_set_test(current_input_batch);
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
if (ui) {
2023-02-17 16:55:00 +01:00
print_ui(epoch, cost_prev, y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
}
}
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
void MLPPANN::adagrad(real_t learning_rate, int max_epoch, int mini_batch_size, real_t e, bool ui) {
MLPPCost mlpp_cost;
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
2023-01-27 13:01:16 +01:00
real_t initial_learning_rate = learning_rate;
2023-01-24 19:00:54 +01:00
// Creating the mini-batches
2023-02-13 00:56:09 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-01-24 19:00:54 +01:00
// always evaluate the result
// always do forward pass only ONCE at end.
2023-02-10 23:10:08 +01:00
2023-02-17 16:55:00 +01:00
MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch);
2023-01-24 19:00:54 +01:00
// Initializing necessary components for Adam.
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> v_hidden;
v_hidden.instance();
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> v_output;
v_output.instance();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 00:56:09 +01:00
learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate);
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
for (int i = 0; i < n_mini_batch; i++) {
2023-02-17 16:55:00 +01:00
Ref<MLPPMatrix> current_input_batch = batches.input_sets[i];
Ref<MLPPVector> current_output_batch = batches.output_sets[i];
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> y_hat = model_set_test(current_input_batch);
cost_prev = cost(y_hat, current_output_batch);
ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
if (!_network.empty() && v_hidden->size() == Size3i()) { // Initing our tensor
v_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
if (v_output->size() == 0) {
v_output->resize(grads.output_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-04-30 22:31:35 +02:00
v_hidden->add(grads.cumulative_hidden_layer_w_grad->exponentiaten(2));
v_output->add(grads.output_w_grad->exponentiaten(2));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> hidden_layer_updations = grads.cumulative_hidden_layer_w_grad->division_element_wisen(v_hidden->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
Ref<MLPPVector> output_layer_updation = grads.output_w_grad->division_element_wisen(v_output->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
2023-01-24 19:00:54 +01:00
2023-02-13 00:56:09 +01:00
update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too.
2023-02-17 16:55:00 +01:00
y_hat = model_set_test(current_input_batch);
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
if (ui) {
2023-02-17 16:55:00 +01:00
print_ui(epoch, cost_prev, y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
}
}
2023-02-17 16:55:00 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-17 16:55:00 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
void MLPPANN::adadelta(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t e, bool ui) {
MLPPCost mlpp_cost;
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
2023-01-27 13:01:16 +01:00
real_t initial_learning_rate = learning_rate;
2023-01-24 19:00:54 +01:00
// Creating the mini-batches
2023-02-13 00:56:09 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-01-24 19:00:54 +01:00
// always evaluate the result
// always do forward pass only ONCE at end.
2023-02-10 23:10:08 +01:00
2023-02-17 16:55:00 +01:00
MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch);
2023-01-24 19:00:54 +01:00
// Initializing necessary components for Adam.
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> v_hidden;
v_hidden.instance();
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> v_output;
v_output.instance();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 00:56:09 +01:00
learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate);
2023-01-24 19:00:54 +01:00
for (int i = 0; i < n_mini_batch; i++) {
2023-02-17 16:55:00 +01:00
Ref<MLPPMatrix> current_input_batch = batches.input_sets[i];
Ref<MLPPVector> current_output_batch = batches.output_sets[i];
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> y_hat = model_set_test(current_input_batch);
cost_prev = cost(y_hat, current_output_batch);
ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
if (!_network.empty() && v_hidden->size() == Size3i()) { // Initing our tensor
v_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
if (v_output->size() == 0) {
v_output->resize(grads.output_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-04-30 22:31:35 +02:00
v_hidden = v_hidden->scalar_multiplyn(1 - b1)->addn(grads.cumulative_hidden_layer_w_grad->exponentiaten(2)->scalar_multiplyn(b1));
v_output->add(grads.output_w_grad->exponentiaten(2));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> hidden_layer_updations = grads.cumulative_hidden_layer_w_grad->division_element_wisen(v_hidden->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
Ref<MLPPVector> output_layer_updation = grads.output_w_grad->division_element_wisen(v_output->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
2023-01-24 19:00:54 +01:00
2023-02-13 00:56:09 +01:00
update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too.
2023-02-17 16:55:00 +01:00
y_hat = model_set_test(current_input_batch);
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
if (ui) {
2023-02-17 16:55:00 +01:00
print_ui(epoch, cost_prev, y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
}
}
2023-02-13 00:56:09 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-13 00:56:09 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-13 00:56:09 +01:00
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
void MLPPANN::adam(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool ui) {
MLPPCost mlpp_cost;
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
2023-01-27 13:01:16 +01:00
real_t initial_learning_rate = learning_rate;
2023-01-24 19:00:54 +01:00
// Creating the mini-batches
2023-02-13 00:56:09 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-01-24 19:00:54 +01:00
// always evaluate the result
// always do forward pass only ONCE at end.
2023-02-10 23:10:08 +01:00
2023-02-17 16:55:00 +01:00
MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch);
2023-01-24 19:00:54 +01:00
// Initializing necessary components for Adam.
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> m_hidden;
Ref<MLPPTensor3> v_hidden;
m_hidden.instance();
v_hidden.instance();
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> m_output;
Ref<MLPPVector> v_output;
m_output.instance();
v_output.instance();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 00:56:09 +01:00
learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate);
2023-01-24 19:00:54 +01:00
for (int i = 0; i < n_mini_batch; i++) {
2023-02-17 16:55:00 +01:00
Ref<MLPPMatrix> current_input_batch = batches.input_sets[i];
Ref<MLPPVector> current_output_batch = batches.output_sets[i];
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> y_hat = model_set_test(current_input_batch);
cost_prev = cost(y_hat, current_output_batch);
ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch);
2023-02-10 23:10:08 +01:00
2023-04-30 22:31:35 +02:00
if (!_network.empty() && m_hidden->size() == Size3i() && v_hidden->size() == Size3i()) { // Initing our tensor
m_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
v_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
if (m_output->size() == 0 && v_output->size()) {
m_output->resize(grads.output_w_grad->size());
v_output->resize(grads.output_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-04-30 22:31:35 +02:00
m_hidden = m_hidden->scalar_multiplyn(b1)->addn(grads.cumulative_hidden_layer_w_grad->scalar_multiplyn(1 - b1));
v_hidden = v_hidden->scalar_multiplyn(b2)->addn(grads.cumulative_hidden_layer_w_grad->exponentiaten(2)->scalar_multiplyn(1 - b2));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
m_output = m_output->scalar_multiplyn(b1)->addn(grads.output_w_grad->scalar_multiplyn(1 - b1));
v_output = v_output->scalar_multiplyn(b2)->addn(grads.output_w_grad->exponentiaten(2)->scalar_multiplyn(1 - b2));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> m_hidden_hat = m_hidden->scalar_multiplyn(1 / (1 - Math::pow(b1, epoch)));
Ref<MLPPTensor3> v_hidden_hat = v_hidden->scalar_multiplyn(1 / (1 - Math::pow(b2, epoch)));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPVector> m_output_hat = m_output->scalar_multiplyn(1 / (1 - Math::pow(b1, epoch)));
Ref<MLPPVector> v_output_hat = v_output->scalar_multiplyn(1 / (1 - Math::pow(b2, epoch)));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> hidden_layer_updations = m_hidden_hat->division_element_wisen(v_hidden_hat->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
Ref<MLPPVector> output_layer_updation = m_output_hat->division_element_wisen(v_output_hat->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
2023-01-24 19:00:54 +01:00
2023-02-13 00:56:09 +01:00
update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too.
2023-02-17 16:55:00 +01:00
y_hat = model_set_test(current_input_batch);
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
if (ui) {
2023-02-17 16:55:00 +01:00
print_ui(epoch, cost_prev, y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
}
}
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
void MLPPANN::adamax(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool ui) {
MLPPCost mlpp_cost;
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
2023-01-27 13:01:16 +01:00
real_t initial_learning_rate = learning_rate;
2023-01-24 19:00:54 +01:00
// Creating the mini-batches
2023-02-13 00:56:09 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-01-24 19:00:54 +01:00
// always evaluate the result
// always do forward pass only ONCE at end.
2023-02-10 23:10:08 +01:00
2023-02-17 16:55:00 +01:00
MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch);
2023-01-24 19:00:54 +01:00
// Initializing necessary components for Adam.
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> m_hidden;
Ref<MLPPTensor3> u_hidden;
m_hidden.instance();
u_hidden.instance();
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> m_output;
Ref<MLPPVector> u_output;
2023-04-30 22:31:35 +02:00
m_output.instance();
u_output.instance();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 00:56:09 +01:00
learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate);
2023-02-17 16:55:00 +01:00
2023-01-24 19:00:54 +01:00
for (int i = 0; i < n_mini_batch; i++) {
2023-02-17 16:55:00 +01:00
Ref<MLPPMatrix> current_input_batch = batches.input_sets[i];
Ref<MLPPVector> current_output_batch = batches.output_sets[i];
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> y_hat = model_set_test(current_input_batch);
cost_prev = cost(y_hat, current_output_batch);
ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch);
2023-02-10 23:10:08 +01:00
2023-04-30 22:31:35 +02:00
if (!_network.empty() && m_hidden->size() == Size3i() && u_hidden->size() == Size3i()) { // Initing our tensor
m_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
u_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
if (m_output->size() == 0 && u_output->size() == 0) {
m_output->resize(grads.output_w_grad->size());
u_output->resize(grads.output_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-04-30 22:31:35 +02:00
m_hidden->addb(m_hidden->scalar_multiplyn(b1), grads.cumulative_hidden_layer_w_grad->scalar_multiplyn(1 - b1));
u_hidden->maxb(u_hidden->scalar_multiplyn(b2), grads.cumulative_hidden_layer_w_grad->absn());
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
m_output->addb(m_output->scalar_multiplyn(b1), grads.output_w_grad->scalar_multiplyn(1 - b1));
u_output->maxb(u_output->scalar_multiplyn(b2), grads.output_w_grad->absn());
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> m_hidden_hat = m_hidden->scalar_multiplyn(1 / (1 - Math::pow(b1, epoch)));
Ref<MLPPVector> m_output_hat = m_output->scalar_multiplyn(1 / (1 - Math::pow(b1, epoch)));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> hidden_layer_updations = m_hidden_hat->division_element_wisen(u_hidden->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
Ref<MLPPVector> output_layer_updation = m_output_hat->division_element_wisen(u_output->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
2023-01-24 19:00:54 +01:00
2023-02-13 00:56:09 +01:00
update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too.
2023-02-17 16:55:00 +01:00
y_hat = model_set_test(current_input_batch);
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
if (ui) {
2023-02-17 16:55:00 +01:00
print_ui(epoch, cost_prev, y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
}
}
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
void MLPPANN::nadam(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool ui) {
MLPPCost mlpp_cost;
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
2023-01-27 13:01:16 +01:00
real_t initial_learning_rate = learning_rate;
2023-01-24 19:00:54 +01:00
// Creating the mini-batches
2023-02-13 00:56:09 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-01-24 19:00:54 +01:00
// always evaluate the result
// always do forward pass only ONCE at end.
2023-02-10 23:10:08 +01:00
2023-02-17 16:55:00 +01:00
MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch);
2023-01-24 19:00:54 +01:00
// Initializing necessary components for Adam.
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> m_hidden;
Ref<MLPPTensor3> v_hidden;
m_hidden.instance();
v_hidden.instance();
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> m_output;
Ref<MLPPVector> v_output;
2023-04-30 22:31:35 +02:00
m_output.instance();
v_output.instance();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 00:56:09 +01:00
learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate);
2023-02-17 16:55:00 +01:00
2023-01-24 19:00:54 +01:00
for (int i = 0; i < n_mini_batch; i++) {
2023-02-17 16:55:00 +01:00
Ref<MLPPMatrix> current_input_batch = batches.input_sets[i];
Ref<MLPPVector> current_output_batch = batches.output_sets[i];
Ref<MLPPVector> y_hat = model_set_test(current_input_batch);
cost_prev = cost(y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch);
2023-02-10 23:10:08 +01:00
2023-04-30 22:31:35 +02:00
if (!_network.empty() && m_hidden->size() == Size3i() && v_hidden->size() == Size3i()) { // Initing our tensor
m_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
v_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
if (m_output->size() == 0 && v_output->size() == 0) {
m_output->resize(grads.output_w_grad->size());
v_output->resize(grads.output_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-04-30 22:31:35 +02:00
m_hidden->addb(m_hidden->scalar_multiplyn(b1), grads.cumulative_hidden_layer_w_grad->scalar_multiplyn(1 - b1));
v_hidden->addb(v_hidden->scalar_multiplyn(b2), grads.cumulative_hidden_layer_w_grad->exponentiaten(2)->scalar_multiplyn(1 - b2));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
m_output->addb(m_output->scalar_multiplyn(b1), grads.output_w_grad->scalar_multiplyn(1 - b1));
v_output->addb(v_output->scalar_multiplyn(b2), grads.output_w_grad->exponentiaten(2)->scalar_multiplyn(1 - b2));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> m_hidden_hat = m_hidden->scalar_multiplyn(1 / (1.0 - Math::pow(b1, epoch)));
Ref<MLPPTensor3> v_hidden_hat = v_hidden->scalar_multiplyn(1 / (1.0 - Math::pow(b2, epoch)));
Ref<MLPPTensor3> m_hidden_final = m_hidden_hat->scalar_multiplyn(b1)->addn(grads.cumulative_hidden_layer_w_grad->scalar_multiplyn((1 - b1) / (1 - Math::pow(b1, epoch))));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPVector> m_output_hat = m_output->scalar_multiplyn(1 / (1.0 - Math::pow(b1, epoch)));
Ref<MLPPVector> v_output_hat = v_output->scalar_multiplyn(1 / (1.0 - Math::pow(b2, epoch)));
Ref<MLPPVector> m_output_final = m_output_hat->scalar_multiplyn(b1)->addn(grads.output_w_grad->scalar_multiplyn((1 - b1) / (1.0 - Math::pow(b1, epoch))));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> hidden_layer_updations = m_hidden_final->division_element_wisen(v_hidden_hat->sqrtn()->scalar_multiplyn(e))->scalar_multiplyn(learning_rate / _n);
Ref<MLPPVector> output_layer_updation = m_output_final->division_element_wisen(v_output_hat->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
2023-01-24 19:00:54 +01:00
2023-02-13 00:56:09 +01:00
update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too.
2023-02-17 16:55:00 +01:00
y_hat = model_set_test(current_input_batch);
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
if (ui) {
2023-02-17 16:55:00 +01:00
print_ui(epoch, cost_prev, y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
}
}
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
void MLPPANN::amsgrad(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool ui) {
MLPPCost mlpp_cost;
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
2023-01-27 13:01:16 +01:00
real_t initial_learning_rate = learning_rate;
2023-01-24 19:00:54 +01:00
// Creating the mini-batches
2023-02-13 00:56:09 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-01-24 19:00:54 +01:00
// always evaluate the result
// always do forward pass only ONCE at end.
2023-02-10 23:10:08 +01:00
2023-02-17 16:55:00 +01:00
MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch);
2023-01-24 19:00:54 +01:00
// Initializing necessary components for Adam.
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> m_hidden;
Ref<MLPPTensor3> v_hidden;
m_hidden.instance();
v_hidden.instance();
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> v_hidden_hat;
v_hidden_hat.instance();
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> m_output;
Ref<MLPPVector> v_output;
2023-04-30 22:31:35 +02:00
m_output.instance();
v_output.instance();
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> v_output_hat;
2023-04-30 22:31:35 +02:00
v_output_hat.instance();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 00:56:09 +01:00
learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate);
2023-02-17 16:55:00 +01:00
2023-01-24 19:00:54 +01:00
for (int i = 0; i < n_mini_batch; i++) {
2023-02-17 16:55:00 +01:00
Ref<MLPPMatrix> current_input_batch = batches.input_sets[i];
Ref<MLPPVector> current_output_batch = batches.output_sets[i];
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
Ref<MLPPVector> y_hat = model_set_test(current_input_batch);
cost_prev = cost(y_hat, current_output_batch);
ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch);
2023-02-10 23:10:08 +01:00
2023-04-30 22:31:35 +02:00
if (!_network.empty() && m_hidden->size() == Size3i() && v_hidden->size() == Size3i()) { // Initing our tensor
m_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
v_hidden->resize(grads.cumulative_hidden_layer_w_grad->size());
v_hidden_hat->resize(grads.cumulative_hidden_layer_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
if (m_output->size() == 0 && v_output->size() == 0) {
m_output->resize(grads.output_w_grad->size());
v_output->resize(grads.output_w_grad->size());
v_output_hat->resize(grads.output_w_grad->size());
2023-01-24 19:00:54 +01:00
}
2023-04-30 22:31:35 +02:00
m_hidden->addb(m_hidden->scalar_multiplyn(b1), grads.cumulative_hidden_layer_w_grad->scalar_multiplyn(1 - b1));
v_hidden->addb(v_hidden->scalar_multiplyn(b2), grads.cumulative_hidden_layer_w_grad->exponentiaten(2)->scalar_multiplyn(1 - b2));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
m_output->addb(m_output->scalar_multiplyn(b1), grads.output_w_grad->scalar_multiplyn(1 - b1));
v_output->addb(v_output->scalar_multiplyn(b2), grads.output_w_grad->exponentiaten(2)->scalar_multiplyn(1 - b2));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
v_hidden_hat->max(v_hidden);
v_output_hat->max(v_output);
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPTensor3> hidden_layer_updations = m_hidden->division_element_wisen(v_hidden_hat->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
Ref<MLPPVector> output_layer_updation = m_output->division_element_wisen(v_output_hat->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n);
2023-01-24 19:00:54 +01:00
2023-02-13 00:56:09 +01:00
update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too.
2023-02-17 16:55:00 +01:00
y_hat = model_set_test(current_input_batch);
2023-01-24 19:00:54 +01:00
2023-02-12 15:07:26 +01:00
if (ui) {
2023-02-17 16:55:00 +01:00
print_ui(epoch, cost_prev, y_hat, current_output_batch);
2023-01-24 19:00:54 +01:00
}
}
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-12 15:07:26 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-12 15:07:26 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-01-27 13:01:16 +01:00
real_t MLPPANN::score() {
2023-02-10 23:10:08 +01:00
MLPPUtilities util;
2023-02-17 16:55:00 +01:00
2023-02-12 15:07:26 +01:00
forward_pass();
2023-02-17 16:55:00 +01:00
return util.performance_vec(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
void MLPPANN::save(const String &file_name) {
2023-02-10 23:10:08 +01:00
MLPPUtilities util;
2023-02-17 16:55:00 +01:00
/*
2023-02-13 00:56:09 +01:00
if (!_network.empty()) {
2023-02-17 16:55:00 +01:00
util.saveParameters(file_name, _network[0].weights, _network[0].bias, false, 1);
2023-02-13 00:56:09 +01:00
for (uint32_t i = 1; i < _network.size(); i++) {
2023-02-17 16:55:00 +01:00
util.saveParameters(file_name, _network[i].weights, _network[i].bias, true, i + 1);
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
util.saveParameters(file_name, _output_layer->weights, _output_layer->bias, true, _network.size() + 1);
2023-01-24 19:00:54 +01:00
} else {
2023-02-17 16:55:00 +01:00
util.saveParameters(file_name, _output_layer->weights, _output_layer->bias, false, _network.size() + 1);
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
*/
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
void MLPPANN::set_learning_rate_scheduler(SchedulerType type, real_t decay_constant) {
2023-02-13 00:56:09 +01:00
_lr_scheduler = type;
_decay_constant = decay_constant;
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
void MLPPANN::set_learning_rate_scheduler_drop(SchedulerType type, real_t decay_constant, real_t drop_rate) {
2023-02-13 00:56:09 +01:00
_lr_scheduler = type;
_decay_constant = decay_constant;
_drop_rate = drop_rate;
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
void MLPPANN::add_layer(int n_hidden, MLPPActivation::ActivationFunction activation, MLPPUtilities::WeightDistributionType weight_init, MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) {
2023-02-13 00:56:09 +01:00
if (_network.empty()) {
2023-02-17 16:55:00 +01:00
_network.push_back(Ref<MLPPHiddenLayer>(memnew(MLPPHiddenLayer(n_hidden, activation, _input_set, weight_init, reg, lambda, alpha))));
_network.write[0]->forward_pass();
2023-01-24 19:00:54 +01:00
} else {
2023-02-17 16:55:00 +01:00
_network.push_back(Ref<MLPPHiddenLayer>(memnew(MLPPHiddenLayer(n_hidden, activation, _network.write[_network.size() - 1]->get_a(), weight_init, reg, lambda, alpha))));
_network.write[_network.size() - 1]->forward_pass();
2023-01-24 19:00:54 +01:00
}
}
2023-02-17 16:55:00 +01:00
void MLPPANN::add_output_layer(MLPPActivation::ActivationFunction activation, MLPPCost::CostTypes loss, MLPPUtilities::WeightDistributionType weight_init, MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) {
2023-02-13 00:56:09 +01:00
if (!_network.empty()) {
2023-02-17 16:55:00 +01:00
_output_layer = Ref<MLPPOutputLayer>(memnew(MLPPOutputLayer(_network.write[_network.size() - 1]->get_n_hidden(), activation, loss, _network.write[_network.size() - 1]->get_a(), weight_init, reg, lambda, alpha)));
2023-01-24 19:00:54 +01:00
} else {
2023-02-17 16:55:00 +01:00
_output_layer = Ref<MLPPOutputLayer>(memnew(MLPPOutputLayer(_k, activation, loss, _input_set, weight_init, reg, lambda, alpha)));
2023-01-24 19:00:54 +01:00
}
}
2023-02-17 16:55:00 +01:00
MLPPANN::MLPPANN(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPVector> &p_output_set) {
2023-02-13 00:56:09 +01:00
_input_set = p_input_set;
_output_set = p_output_set;
2023-02-12 15:07:26 +01:00
2023-02-17 16:55:00 +01:00
_n = _input_set->size().y;
_k = _input_set->size().x;
_lr_scheduler = SCHEDULER_TYPE_NONE;
2023-02-13 00:56:09 +01:00
_decay_constant = 0;
_drop_rate = 0;
2023-02-12 15:07:26 +01:00
}
MLPPANN::MLPPANN() {
}
MLPPANN::~MLPPANN() {
}
2023-02-17 16:55:00 +01:00
// https://en.wikipedia.org/wiki/Learning_rate
// Learning Rate Decay (C2W2L09) - Andrew Ng - Deep Learning Specialization
real_t MLPPANN::apply_learning_rate_scheduler(real_t learning_rate, real_t decay_constant, real_t epoch, real_t drop_rate) {
if (_lr_scheduler == SCHEDULER_TYPE_TIME) {
return learning_rate / (1 + decay_constant * epoch);
} else if (_lr_scheduler == SCHEDULER_TYPE_EPOCH) {
return learning_rate * (decay_constant / std::sqrt(epoch));
} else if (_lr_scheduler == SCHEDULER_TYPE_STEP) {
return learning_rate * Math::pow(decay_constant, int((1 + epoch) / drop_rate)); // Utilizing an explicit int conversion implicitly takes the floor.
} else if (_lr_scheduler == SCHEDULER_TYPE_EXPONENTIAL) {
return learning_rate * Math::exp(-decay_constant * epoch);
}
return learning_rate;
}
real_t MLPPANN::cost(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-12 15:07:26 +01:00
MLPPCost mlpp_cost;
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
real_t total_reg_term = 0;
2023-02-12 15:07:26 +01:00
2023-02-13 00:56:09 +01:00
if (!_network.empty()) {
2023-02-17 16:55:00 +01:00
for (int i = 0; i < _network.size() - 1; i++) {
Ref<MLPPHiddenLayer> layer = _network[i];
total_reg_term += regularization.reg_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg());
2023-01-24 19:00:54 +01:00
}
}
2023-02-12 15:07:26 +01:00
2023-02-17 16:55:00 +01:00
return mlpp_cost.run_cost_norm_vector(_output_layer->get_cost(), y_hat, y) + total_reg_term + regularization.reg_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg());
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
void MLPPANN::forward_pass() {
2023-02-13 00:56:09 +01:00
if (!_network.empty()) {
2023-02-17 16:55:00 +01:00
Ref<MLPPHiddenLayer> layer = _network[0];
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
layer->set_input(_input_set);
layer->forward_pass();
for (int i = 1; i < _network.size(); i++) {
layer = _network[i];
Ref<MLPPHiddenLayer> prev_layer = _network[i - 1];
layer->set_input(prev_layer->get_a());
layer->forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
_output_layer->set_input(_network.write[_network.size() - 1]->get_a());
2023-01-24 19:00:54 +01:00
} else {
2023-02-17 16:55:00 +01:00
_output_layer->set_input(_input_set);
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:07:26 +01:00
2023-02-17 16:55:00 +01:00
_output_layer->forward_pass();
_y_hat = _output_layer->get_a();
2023-01-24 19:00:54 +01:00
}
2023-04-30 22:31:35 +02:00
void MLPPANN::update_parameters(const Ref<MLPPTensor3> &hidden_layer_updations, const Ref<MLPPVector> &output_layer_updation, real_t learning_rate) {
_output_layer->set_weights(_output_layer->get_weights()->subn(output_layer_updation));
_output_layer->set_bias(_output_layer->get_bias() - learning_rate * _output_layer->get_delta()->sum_elements() / _n);
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPMatrix> slice;
slice.instance();
2023-01-24 19:00:54 +01:00
2023-02-13 00:56:09 +01:00
if (!_network.empty()) {
2023-02-17 16:55:00 +01:00
Ref<MLPPHiddenLayer> layer = _network[_network.size() - 1];
2023-04-30 22:31:35 +02:00
hidden_layer_updations->z_slice_get_into_mlpp_matrix(0, slice);
layer->set_weights(layer->get_weights()->subn(slice));
layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / _n)));
2023-01-24 19:00:54 +01:00
2023-02-13 00:56:09 +01:00
for (int i = _network.size() - 2; i >= 0; i--) {
2023-02-17 16:55:00 +01:00
layer = _network[i];
2023-04-30 22:31:35 +02:00
hidden_layer_updations->z_slice_get_into_mlpp_matrix((_network.size() - 2) - i + 1, slice);
layer->set_weights(layer->get_weights()->subn(slice));
layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / _n)));
2023-01-24 19:00:54 +01:00
}
}
}
2023-02-17 16:55:00 +01:00
MLPPANN::ComputeGradientsResult MLPPANN::compute_gradients(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &_output_set) {
2023-01-24 19:00:54 +01:00
// std::cout << "BEGIN" << std::endl;
2023-02-12 15:07:26 +01:00
MLPPCost mlpp_cost;
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-04-30 22:31:35 +02:00
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-01-24 19:00:54 +01:00
2023-02-17 16:55:00 +01:00
ComputeGradientsResult res;
2023-04-30 22:31:35 +02:00
_output_layer->set_delta(mlpp_cost.run_cost_deriv_vector(_output_layer->get_cost(), y_hat, _output_set)->hadamard_productn(avn.run_activation_deriv_vector(_output_layer->get_activation(), _output_layer->get_z())));
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
res.output_w_grad = _output_layer->get_input()->transposen()->mult_vec(_output_layer->get_delta());
res.output_w_grad->add(regularization.reg_deriv_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg()));
2023-01-24 19:00:54 +01:00
2023-02-13 00:56:09 +01:00
if (!_network.empty()) {
2023-02-17 16:55:00 +01:00
Ref<MLPPHiddenLayer> layer = _network[_network.size() - 1];
2023-04-30 22:31:35 +02:00
layer->set_delta(_output_layer->get_delta()->outer_product(_output_layer->get_weights())->hadamard_productn(avn.run_activation_deriv_vector(layer->get_activation(), layer->get_z())));
2023-02-17 16:55:00 +01:00
2023-04-30 22:31:35 +02:00
Ref<MLPPMatrix> hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());
2023-01-24 19:00:54 +01:00
2023-04-30 22:31:35 +02:00
res.cumulative_hidden_layer_w_grad->z_slice_add_mlpp_matrix(hidden_layer_w_grad->addn(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well.
2023-01-24 19:00:54 +01:00
2023-02-13 00:56:09 +01:00
for (int i = _network.size() - 2; i >= 0; i--) {
2023-02-17 16:55:00 +01:00
layer = _network[i];
Ref<MLPPHiddenLayer> next_layer = _network[i + 1];
2023-04-30 22:31:35 +02:00
layer->set_delta(next_layer->get_delta()->multn(next_layer->get_weights()->transposen())->hadamard_productn(avn.run_activation_deriv_vector(layer->get_activation(), layer->get_z())));
hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());
res.cumulative_hidden_layer_w_grad->z_slice_add_mlpp_matrix(hidden_layer_w_grad->addn(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well.
2023-01-24 19:00:54 +01:00
}
}
2023-02-17 16:55:00 +01:00
return res;
2023-01-24 19:00:54 +01:00
}
2023-02-17 16:55:00 +01:00
void MLPPANN::print_ui(int epoch, real_t cost_prev, const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &p_output_set) {
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, p_output_set));
PLOG_MSG("Layer " + itos(_network.size() + 1) + ": ");
MLPPUtilities::print_ui_vb(_output_layer->get_weights(), _output_layer->get_bias());
2023-02-13 00:56:09 +01:00
if (!_network.empty()) {
for (int i = _network.size() - 1; i >= 0; i--) {
2023-02-17 16:55:00 +01:00
Ref<MLPPHiddenLayer> layer = _network[i];
PLOG_MSG("Layer " + itos(i + 1) + ": ");
MLPPUtilities::print_ui_mb(layer->get_weights(), layer->get_bias());
2023-01-24 19:00:54 +01:00
}
}
}
2023-02-12 18:35:53 +01:00
void MLPPANN::_bind_methods() {
}