// // ANN.cpp // // Created by Marc Melikyan on 11/4/20. // #include "ann.h" #include "../activation/activation.h" #include "../cost/cost.h" #include "../regularization/reg.h" #include "../utilities/utilities.h" #include "core/log/logger.h" #include Ref MLPPANN::model_set_test(const Ref &X) { if (!_network.empty()) { Ref layer = _network[0]; layer->set_input(X); layer->forward_pass(); for (int i = 1; i < _network.size(); i++) { layer = _network[i]; Ref prev_layer = _network[i - 1]; layer->set_input(prev_layer->get_a()); layer->forward_pass(); } _output_layer->set_input(_network.write[_network.size() - 1]->get_a()); } else { _output_layer->set_input(X); } _output_layer->forward_pass(); return _output_layer->get_a(); } real_t MLPPANN::model_test(const Ref &x) { if (!_network.empty()) { Ref layer = _network[0]; layer->test(x); for (int i = 1; i < _network.size(); i++) { layer = _network[i]; Ref prev_layer = _network[i - 1]; layer->test(prev_layer->get_a_test()); } _output_layer->test(_network.write[_network.size() - 1]->get_a_test()); } else { _output_layer->test(x); } return _output_layer->get_a_test(); } void MLPPANN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { MLPPCost mlpp_cost; real_t cost_prev = 0; int epoch = 1; forward_pass(); real_t initial_learning_rate = learning_rate; while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); cost_prev = cost(_y_hat, _output_set); ComputeGradientsResult grads = compute_gradients(_y_hat, _output_set); grads.cumulative_hidden_layer_w_grad->scalar_multiply(learning_rate / _n); grads.output_w_grad->scalar_multiply(learning_rate / _n); 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. forward_pass(); if (ui) { print_ui(epoch, cost_prev, _y_hat, _output_set); } epoch++; if (epoch > max_epoch) { break; } } } void MLPPANN::sgd(real_t learning_rate, int max_epoch, bool ui) { MLPPCost mlpp_cost; real_t cost_prev = 0; int epoch = 1; real_t initial_learning_rate = learning_rate; std::random_device rd; std::default_random_engine generator(rd()); std::uniform_int_distribution distribution(0, int(_n - 1)); Ref input_set_row_tmp; input_set_row_tmp.instance(); input_set_row_tmp->resize(_input_set->size().x); Ref y_hat_row_tmp; y_hat_row_tmp.instance(); y_hat_row_tmp->resize(1); Ref output_set_row_tmp; output_set_row_tmp.instance(); output_set_row_tmp->resize(1); while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); int output_index = distribution(generator); _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); real_t y_hat = model_test(input_set_row_tmp); y_hat_row_tmp->element_set(0, y_hat); cost_prev = cost(y_hat_row_tmp, output_set_row_tmp); ComputeGradientsResult grads = compute_gradients(y_hat_row_tmp, output_set_row_tmp); grads.cumulative_hidden_layer_w_grad->scalar_multiply(learning_rate / _n); grads.output_w_grad->scalar_multiply(learning_rate / _n); 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); if (ui) { print_ui(epoch, cost_prev, y_hat_row_tmp, output_set_row_tmp); } epoch++; if (epoch > max_epoch) { break; } } forward_pass(); } void MLPPANN::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui) { MLPPCost mlpp_cost; real_t cost_prev = 0; int epoch = 1; real_t initial_learning_rate = learning_rate; // Creating the mini-batches int n_mini_batch = _n / mini_batch_size; // always evaluate the result // always do forward pass only ONCE at end. MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); for (int i = 0; i < n_mini_batch; i++) { Ref current_input_batch = batches.input_sets[i]; Ref current_output_batch = batches.output_sets[i]; Ref 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); grads.cumulative_hidden_layer_w_grad->scalar_multiply(learning_rate / _n); grads.output_w_grad->scalar_multiply(learning_rate / _n); 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); if (ui) { print_ui(epoch, cost_prev, y_hat, current_output_batch); } } epoch++; if (epoch > max_epoch) { break; } } forward_pass(); } 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; real_t cost_prev = 0; int epoch = 1; real_t initial_learning_rate = learning_rate; // Creating the mini-batches int n_mini_batch = _n / mini_batch_size; // always evaluate the result // always do forward pass only ONCE at end. MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. Ref v_hidden; v_hidden.instance(); Ref v_output; v_output.instance(); while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); for (int i = 0; i < n_mini_batch; i++) { Ref current_input_batch = batches.input_sets[i]; Ref current_output_batch = batches.output_sets[i]; Ref 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); if (!_network.empty() && v_hidden->size() == Size3i()) { // Initing our tensor v_hidden->resize(grads.cumulative_hidden_layer_w_grad->size()); } if (v_output->size() == 0) { v_output->resize(grads.output_w_grad->size()); } if (nag) { // "Aposterori" calculation update_parameters(v_hidden, v_output, 0); // DON'T update bias. } 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)); update_parameters(v_hidden, v_output, learning_rate); // subject to change. may want bias to have this matrix too. y_hat = model_set_test(current_input_batch); if (ui) { print_ui(epoch, cost_prev, y_hat, current_output_batch); } } epoch++; if (epoch > max_epoch) { break; } } forward_pass(); } void MLPPANN::adagrad(real_t learning_rate, int max_epoch, int mini_batch_size, real_t e, bool ui) { MLPPCost mlpp_cost; real_t cost_prev = 0; int epoch = 1; real_t initial_learning_rate = learning_rate; // Creating the mini-batches int n_mini_batch = _n / mini_batch_size; // always evaluate the result // always do forward pass only ONCE at end. MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. Ref v_hidden; v_hidden.instance(); Ref v_output; v_output.instance(); while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); for (int i = 0; i < n_mini_batch; i++) { Ref current_input_batch = batches.input_sets[i]; Ref current_output_batch = batches.output_sets[i]; Ref 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); if (!_network.empty() && v_hidden->size() == Size3i()) { // Initing our tensor v_hidden->resize(grads.cumulative_hidden_layer_w_grad->size()); } if (v_output->size() == 0) { v_output->resize(grads.output_w_grad->size()); } v_hidden->add(grads.cumulative_hidden_layer_w_grad->exponentiaten(2)); v_output->add(grads.output_w_grad->exponentiaten(2)); Ref hidden_layer_updations = grads.cumulative_hidden_layer_w_grad->division_element_wisen(v_hidden->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); Ref output_layer_updation = grads.output_w_grad->division_element_wisen(v_output->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. y_hat = model_set_test(current_input_batch); if (ui) { print_ui(epoch, cost_prev, y_hat, current_output_batch); } } epoch++; if (epoch > max_epoch) { break; } } forward_pass(); } 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; real_t cost_prev = 0; int epoch = 1; real_t initial_learning_rate = learning_rate; // Creating the mini-batches int n_mini_batch = _n / mini_batch_size; // always evaluate the result // always do forward pass only ONCE at end. MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. Ref v_hidden; v_hidden.instance(); Ref v_output; v_output.instance(); while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); for (int i = 0; i < n_mini_batch; i++) { Ref current_input_batch = batches.input_sets[i]; Ref current_output_batch = batches.output_sets[i]; Ref 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); if (!_network.empty() && v_hidden->size() == Size3i()) { // Initing our tensor v_hidden->resize(grads.cumulative_hidden_layer_w_grad->size()); } if (v_output->size() == 0) { v_output->resize(grads.output_w_grad->size()); } 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)); Ref hidden_layer_updations = grads.cumulative_hidden_layer_w_grad->division_element_wisen(v_hidden->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); Ref output_layer_updation = grads.output_w_grad->division_element_wisen(v_output->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. y_hat = model_set_test(current_input_batch); if (ui) { print_ui(epoch, cost_prev, y_hat, current_output_batch); } } epoch++; if (epoch > max_epoch) { break; } } forward_pass(); } 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; real_t cost_prev = 0; int epoch = 1; real_t initial_learning_rate = learning_rate; // Creating the mini-batches int n_mini_batch = _n / mini_batch_size; // always evaluate the result // always do forward pass only ONCE at end. MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. Ref m_hidden; Ref v_hidden; m_hidden.instance(); v_hidden.instance(); Ref m_output; Ref v_output; m_output.instance(); v_output.instance(); while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); for (int i = 0; i < n_mini_batch; i++) { Ref current_input_batch = batches.input_sets[i]; Ref current_output_batch = batches.output_sets[i]; Ref 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); 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()); } if (m_output->size() == 0 && v_output->size()) { m_output->resize(grads.output_w_grad->size()); v_output->resize(grads.output_w_grad->size()); } 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)); 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)); Ref m_hidden_hat = m_hidden->scalar_multiplyn(1 / (1 - Math::pow(b1, epoch))); Ref v_hidden_hat = v_hidden->scalar_multiplyn(1 / (1 - Math::pow(b2, epoch))); Ref m_output_hat = m_output->scalar_multiplyn(1 / (1 - Math::pow(b1, epoch))); Ref v_output_hat = v_output->scalar_multiplyn(1 / (1 - Math::pow(b2, epoch))); Ref hidden_layer_updations = m_hidden_hat->division_element_wisen(v_hidden_hat->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); Ref output_layer_updation = m_output_hat->division_element_wisen(v_output_hat->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. y_hat = model_set_test(current_input_batch); if (ui) { print_ui(epoch, cost_prev, y_hat, current_output_batch); } } epoch++; if (epoch > max_epoch) { break; } } forward_pass(); } 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; real_t cost_prev = 0; int epoch = 1; real_t initial_learning_rate = learning_rate; // Creating the mini-batches int n_mini_batch = _n / mini_batch_size; // always evaluate the result // always do forward pass only ONCE at end. MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. Ref m_hidden; Ref u_hidden; m_hidden.instance(); u_hidden.instance(); Ref m_output; Ref u_output; m_output.instance(); u_output.instance(); while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); for (int i = 0; i < n_mini_batch; i++) { Ref current_input_batch = batches.input_sets[i]; Ref current_output_batch = batches.output_sets[i]; Ref 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); 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()); } 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()); } 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()); 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()); Ref m_hidden_hat = m_hidden->scalar_multiplyn(1 / (1 - Math::pow(b1, epoch))); Ref m_output_hat = m_output->scalar_multiplyn(1 / (1 - Math::pow(b1, epoch))); Ref hidden_layer_updations = m_hidden_hat->division_element_wisen(u_hidden->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); Ref output_layer_updation = m_output_hat->division_element_wisen(u_output->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. y_hat = model_set_test(current_input_batch); if (ui) { print_ui(epoch, cost_prev, y_hat, current_output_batch); } } epoch++; if (epoch > max_epoch) { break; } } forward_pass(); } 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; real_t cost_prev = 0; int epoch = 1; real_t initial_learning_rate = learning_rate; // Creating the mini-batches int n_mini_batch = _n / mini_batch_size; // always evaluate the result // always do forward pass only ONCE at end. MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. Ref m_hidden; Ref v_hidden; m_hidden.instance(); v_hidden.instance(); Ref m_output; Ref v_output; m_output.instance(); v_output.instance(); while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); for (int i = 0; i < n_mini_batch; i++) { Ref current_input_batch = batches.input_sets[i]; Ref current_output_batch = batches.output_sets[i]; Ref 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); 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()); } 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()); } 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)); 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)); Ref m_hidden_hat = m_hidden->scalar_multiplyn(1 / (1.0 - Math::pow(b1, epoch))); Ref v_hidden_hat = v_hidden->scalar_multiplyn(1 / (1.0 - Math::pow(b2, epoch))); Ref 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)))); Ref m_output_hat = m_output->scalar_multiplyn(1 / (1.0 - Math::pow(b1, epoch))); Ref v_output_hat = v_output->scalar_multiplyn(1 / (1.0 - Math::pow(b2, epoch))); Ref m_output_final = m_output_hat->scalar_multiplyn(b1)->addn(grads.output_w_grad->scalar_multiplyn((1 - b1) / (1.0 - Math::pow(b1, epoch)))); Ref hidden_layer_updations = m_hidden_final->division_element_wisen(v_hidden_hat->sqrtn()->scalar_multiplyn(e))->scalar_multiplyn(learning_rate / _n); Ref output_layer_updation = m_output_final->division_element_wisen(v_output_hat->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. y_hat = model_set_test(current_input_batch); if (ui) { print_ui(epoch, cost_prev, y_hat, current_output_batch); } } epoch++; if (epoch > max_epoch) { break; } } forward_pass(); } 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; real_t cost_prev = 0; int epoch = 1; real_t initial_learning_rate = learning_rate; // Creating the mini-batches int n_mini_batch = _n / mini_batch_size; // always evaluate the result // always do forward pass only ONCE at end. MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. Ref m_hidden; Ref v_hidden; m_hidden.instance(); v_hidden.instance(); Ref v_hidden_hat; v_hidden_hat.instance(); Ref m_output; Ref v_output; m_output.instance(); v_output.instance(); Ref v_output_hat; v_output_hat.instance(); while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); for (int i = 0; i < n_mini_batch; i++) { Ref current_input_batch = batches.input_sets[i]; Ref current_output_batch = batches.output_sets[i]; Ref 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); 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()); } 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()); } 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)); 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)); v_hidden_hat->max(v_hidden); v_output_hat->max(v_output); Ref hidden_layer_updations = m_hidden->division_element_wisen(v_hidden_hat->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); Ref output_layer_updation = m_output->division_element_wisen(v_output_hat->sqrtn()->scalar_addn(e))->scalar_multiplyn(learning_rate / _n); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. y_hat = model_set_test(current_input_batch); if (ui) { print_ui(epoch, cost_prev, y_hat, current_output_batch); } } epoch++; if (epoch > max_epoch) { break; } } forward_pass(); } real_t MLPPANN::score() { MLPPUtilities util; forward_pass(); return util.performance_vec(_y_hat, _output_set); } void MLPPANN::save(const String &file_name) { MLPPUtilities util; /* if (!_network.empty()) { util.saveParameters(file_name, _network[0].weights, _network[0].bias, false, 1); for (uint32_t i = 1; i < _network.size(); i++) { util.saveParameters(file_name, _network[i].weights, _network[i].bias, true, i + 1); } util.saveParameters(file_name, _output_layer->weights, _output_layer->bias, true, _network.size() + 1); } else { util.saveParameters(file_name, _output_layer->weights, _output_layer->bias, false, _network.size() + 1); } */ } void MLPPANN::set_learning_rate_scheduler(SchedulerType type, real_t decay_constant) { _lr_scheduler = type; _decay_constant = decay_constant; } void MLPPANN::set_learning_rate_scheduler_drop(SchedulerType type, real_t decay_constant, real_t drop_rate) { _lr_scheduler = type; _decay_constant = decay_constant; _drop_rate = drop_rate; } void MLPPANN::add_layer(int n_hidden, MLPPActivation::ActivationFunction activation, MLPPUtilities::WeightDistributionType weight_init, MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) { if (_network.empty()) { _network.push_back(Ref(memnew(MLPPHiddenLayer(n_hidden, activation, _input_set, weight_init, reg, lambda, alpha)))); _network.write[0]->forward_pass(); } else { _network.push_back(Ref(memnew(MLPPHiddenLayer(n_hidden, activation, _network.write[_network.size() - 1]->get_a(), weight_init, reg, lambda, alpha)))); _network.write[_network.size() - 1]->forward_pass(); } } void MLPPANN::add_output_layer(MLPPActivation::ActivationFunction activation, MLPPCost::CostTypes loss, MLPPUtilities::WeightDistributionType weight_init, MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) { if (!_network.empty()) { _output_layer = Ref(memnew(MLPPOutputLayer(_network.write[_network.size() - 1]->get_n_hidden(), activation, loss, _network.write[_network.size() - 1]->get_a(), weight_init, reg, lambda, alpha))); } else { _output_layer = Ref(memnew(MLPPOutputLayer(_k, activation, loss, _input_set, weight_init, reg, lambda, alpha))); } } MLPPANN::MLPPANN(const Ref &p_input_set, const Ref &p_output_set) { _input_set = p_input_set; _output_set = p_output_set; _n = _input_set->size().y; _k = _input_set->size().x; _lr_scheduler = SCHEDULER_TYPE_NONE; _decay_constant = 0; _drop_rate = 0; } MLPPANN::MLPPANN() { } MLPPANN::~MLPPANN() { } // 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 &y_hat, const Ref &y) { MLPPReg regularization; MLPPCost mlpp_cost; real_t total_reg_term = 0; if (!_network.empty()) { for (int i = 0; i < _network.size() - 1; i++) { Ref layer = _network[i]; total_reg_term += regularization.reg_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()); } } 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()); } void MLPPANN::forward_pass() { if (!_network.empty()) { Ref layer = _network[0]; layer->set_input(_input_set); layer->forward_pass(); for (int i = 1; i < _network.size(); i++) { layer = _network[i]; Ref prev_layer = _network[i - 1]; layer->set_input(prev_layer->get_a()); layer->forward_pass(); } _output_layer->set_input(_network.write[_network.size() - 1]->get_a()); } else { _output_layer->set_input(_input_set); } _output_layer->forward_pass(); _y_hat = _output_layer->get_a(); } void MLPPANN::update_parameters(const Ref &hidden_layer_updations, const Ref &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); Ref slice; slice.instance(); if (!_network.empty()) { Ref layer = _network[_network.size() - 1]; 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))); for (int i = _network.size() - 2; i >= 0; i--) { layer = _network[i]; 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))); } } } MLPPANN::ComputeGradientsResult MLPPANN::compute_gradients(const Ref &y_hat, const Ref &_output_set) { // std::cout << "BEGIN" << std::endl; MLPPCost mlpp_cost; MLPPActivation avn; MLPPReg regularization; ComputeGradientsResult res; _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()))); 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())); if (!_network.empty()) { Ref layer = _network[_network.size() - 1]; 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()))); Ref 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. for (int i = _network.size() - 2; i >= 0; i--) { layer = _network[i]; Ref next_layer = _network[i + 1]; 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. } } return res; } void MLPPANN::print_ui(int epoch, real_t cost_prev, const Ref &y_hat, const Ref &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()); if (!_network.empty()) { for (int i = _network.size() - 1; i >= 0; i--) { Ref layer = _network[i]; PLOG_MSG("Layer " + itos(i + 1) + ": "); MLPPUtilities::print_ui_mb(layer->get_weights(), layer->get_bias()); } } } void MLPPANN::_bind_methods() { }