diff --git a/mlpp/ann/ann.cpp b/mlpp/ann/ann.cpp index 9364810..c667e63 100644 --- a/mlpp/ann/ann.cpp +++ b/mlpp/ann/ann.cpp @@ -5,46 +5,60 @@ // #include "ann.h" + #include "../activation/activation.h" #include "../cost/cost.h" #include "../lin_alg/lin_alg.h" #include "../regularization/reg.h" #include "../utilities/utilities.h" +#include "core/log/logger.h" -#include -#include #include -std::vector MLPPANN::model_set_test(std::vector> X) { +Ref MLPPANN::model_set_test(const Ref &X) { if (!_network.empty()) { - _network[0].input = X; - _network[0].forwardPass(); + Ref layer = _network[0]; - for (uint32_t i = 1; i < _network.size(); i++) { - _network[i].input = _network[i - 1].a; - _network[i].forwardPass(); + 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->input = _network[_network.size() - 1].a; + + _output_layer->set_input(_network.write[_network.size() - 1]->get_a()); } else { - _output_layer->input = X; + _output_layer->set_input(X); } - _output_layer->forwardPass(); + _output_layer->forward_pass(); - return _output_layer->a; + return _output_layer->get_a(); } -real_t MLPPANN::model_test(std::vector x) { +real_t MLPPANN::model_test(const Ref &x) { if (!_network.empty()) { - _network[0].Test(x); - for (uint32_t i = 1; i < _network.size(); i++) { - _network[i].Test(_network[i - 1].a_test); + 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[_network.size() - 1].a_test); + + _output_layer->test(_network.write[_network.size() - 1]->get_a_test()); } else { - _output_layer->Test(x); + _output_layer->test(x); } - return _output_layer->a_test; + + return _output_layer->get_a_test(); } void MLPPANN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { @@ -57,21 +71,16 @@ void MLPPANN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { real_t initial_learning_rate = learning_rate; - alg.printMatrix(_network[_network.size() - 1].weights); while (true) { learning_rate = apply_learning_rate_scheduler(initial_learning_rate, _decay_constant, epoch, _drop_rate); cost_prev = cost(_y_hat, _output_set); - auto grads = compute_gradients(_y_hat, _output_set); - auto cumulative_hidden_layer_w_grad = std::get<0>(grads); - auto output_w_grad = std::get<1>(grads); + ComputeGradientsResult grads = compute_gradients(_y_hat, _output_set); - cumulative_hidden_layer_w_grad = alg.scalarMultiply(learning_rate / _n, cumulative_hidden_layer_w_grad); - output_w_grad = alg.scalarMultiply(learning_rate / _n, output_w_grad); - update_parameters(cumulative_hidden_layer_w_grad, output_w_grad, learning_rate); // subject to change. may want bias to have this matrix too. - - std::cout << learning_rate << std::endl; + grads.cumulative_hidden_layer_w_grad = alg.scalar_multiply_vm(learning_rate / _n, grads.cumulative_hidden_layer_w_grad); + grads.output_w_grad = alg.scalar_multiplynv(learning_rate / _n, grads.output_w_grad); + 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(); @@ -80,6 +89,7 @@ void MLPPANN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { } epoch++; + if (epoch > max_epoch) { break; } @@ -94,32 +104,50 @@ void MLPPANN::sgd(real_t learning_rate, int max_epoch, bool ui) { 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); - std::random_device rd; - std::default_random_engine generator(rd()); - std::uniform_int_distribution distribution(0, int(_n - 1)); - int outputIndex = distribution(generator); + int output_index = distribution(generator); - std::vector y_hat = model_set_test({ _input_set[outputIndex] }); - cost_prev = cost({ y_hat }, { _output_set[outputIndex] }); + _input_set->get_row_into_mlpp_vector(output_index, input_set_row_tmp); + real_t output_set_element = _output_set->get_element(output_index); + output_set_row_tmp->set_element(0, output_set_element); - auto grads = compute_gradients(y_hat, { _output_set[outputIndex] }); - auto cumulative_hidden_layer_w_grad = std::get<0>(grads); - auto output_w_grad = std::get<1>(grads); + real_t y_hat = model_test(input_set_row_tmp); + y_hat_row_tmp->set_element(0, y_hat); - cumulative_hidden_layer_w_grad = alg.scalarMultiply(learning_rate / _n, cumulative_hidden_layer_w_grad); - output_w_grad = alg.scalarMultiply(learning_rate / _n, output_w_grad); + cost_prev = cost(y_hat_row_tmp, output_set_row_tmp); - update_parameters(cumulative_hidden_layer_w_grad, output_w_grad, learning_rate); // subject to change. may want bias to have this matrix too. - y_hat = model_set_test({ _input_set[outputIndex] }); + ComputeGradientsResult grads = compute_gradients(y_hat_row_tmp, output_set_row_tmp); + + grads.cumulative_hidden_layer_w_grad = alg.scalar_multiply_vm(learning_rate / _n, grads.cumulative_hidden_layer_w_grad); + grads.output_w_grad = alg.scalar_multiplynv(learning_rate / _n, grads.output_w_grad); + + 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, { _output_set[outputIndex] }); + print_ui(epoch, cost_prev, y_hat_row_tmp, output_set_row_tmp); } epoch++; + if (epoch > max_epoch) { break; } @@ -141,29 +169,28 @@ void MLPPANN::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, boo // always evaluate the result // always do forward pass only ONCE at end. - auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch); - auto input_mini_batches = std::get<0>(batches); - auto output_mini_batches = std::get<1>(batches); + 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++) { - std::vector y_hat = model_set_test(input_mini_batches[i]); - cost_prev = cost(y_hat, output_mini_batches[i]); + Ref current_input_batch = batches.input_sets[i]; + Ref current_output_batch = batches.output_sets[i]; - auto grads = compute_gradients(y_hat, output_mini_batches[i]); - auto cumulative_hidden_layer_w_grad = std::get<0>(grads); - auto output_w_grad = std::get<1>(grads); + Ref y_hat = model_set_test(current_input_batch); + cost_prev = cost(y_hat, current_output_batch); - cumulative_hidden_layer_w_grad = alg.scalarMultiply(learning_rate / _n, cumulative_hidden_layer_w_grad); - output_w_grad = alg.scalarMultiply(learning_rate / _n, output_w_grad); + ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch); - update_parameters(cumulative_hidden_layer_w_grad, output_w_grad, learning_rate); // subject to change. may want bias to have this matrix too. - y_hat = model_set_test(input_mini_batches[i]); + grads.cumulative_hidden_layer_w_grad = alg.scalar_multiply_vm(learning_rate / _n, grads.cumulative_hidden_layer_w_grad); + grads.output_w_grad = alg.scalar_multiplynv(learning_rate / _n, grads.output_w_grad); + + 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, output_mini_batches[i]); + print_ui(epoch, cost_prev, y_hat, current_output_batch); } } @@ -190,46 +217,46 @@ void MLPPANN::momentum(real_t learning_rate, int max_epoch, int mini_batch_size, // always evaluate the result // always do forward pass only ONCE at end. - auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch); - auto input_mini_batches = std::get<0>(batches); - auto output_mini_batches = std::get<1>(batches); + MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. - std::vector>> v_hidden; + Vector> v_hidden; + + Ref v_output; + v_output.instance(); - std::vector v_output; 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++) { - std::vector y_hat = model_set_test(input_mini_batches[i]); - cost_prev = cost(y_hat, output_mini_batches[i]); + Ref current_input_batch = batches.input_sets[i]; + Ref current_output_batch = batches.output_sets[i]; - auto grads = compute_gradients(y_hat, output_mini_batches[i]); - auto cumulative_hidden_layer_w_grad = std::get<0>(grads); - auto output_w_grad = std::get<1>(grads); + 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.empty()) { // Initing our tensor - v_hidden = alg.resize(v_hidden, cumulative_hidden_layer_w_grad); + v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); } - if (v_output.empty()) { - v_output.resize(output_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 = alg.addition(alg.scalarMultiply(gamma, v_hidden), alg.scalarMultiply(learning_rate / _n, cumulative_hidden_layer_w_grad)); - - v_output = alg.addition(alg.scalarMultiply(gamma, v_output), alg.scalarMultiply(learning_rate / _n, output_w_grad)); + v_hidden = alg.addition_vt(alg.scalar_multiply_vm(gamma, v_hidden), alg.scalar_multiply_vm(learning_rate / _n, grads.cumulative_hidden_layer_w_grad)); + v_output = alg.additionnv(alg.scalar_multiplynv(gamma, v_output), alg.scalar_multiplynv(learning_rate / _n, grads.output_w_grad)); update_parameters(v_hidden, v_output, learning_rate); // subject to change. may want bias to have this matrix too. - y_hat = model_set_test(input_mini_batches[i]); + y_hat = model_set_test(current_input_batch); if (ui) { - print_ui(epoch, cost_prev, y_hat, output_mini_batches[i]); + print_ui(epoch, cost_prev, y_hat, current_output_batch); } } @@ -256,48 +283,50 @@ void MLPPANN::adagrad(real_t learning_rate, int max_epoch, int mini_batch_size, // always evaluate the result // always do forward pass only ONCE at end. - auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch); - auto input_mini_batches = std::get<0>(batches); - auto output_mini_batches = std::get<1>(batches); + MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. - std::vector>> v_hidden; + Vector> v_hidden; + + Ref v_output; + v_output.instance(); - std::vector v_output; 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++) { - std::vector y_hat = model_set_test(input_mini_batches[i]); - cost_prev = cost(y_hat, output_mini_batches[i]); + Ref current_input_batch = batches.input_sets[i]; + Ref current_output_batch = batches.output_sets[i]; - auto grads = compute_gradients(y_hat, output_mini_batches[i]); - auto cumulative_hidden_layer_w_grad = std::get<0>(grads); - auto output_w_grad = std::get<1>(grads); + 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.empty()) { // Initing our tensor - v_hidden = alg.resize(v_hidden, cumulative_hidden_layer_w_grad); + v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); } - if (v_output.empty()) { - v_output.resize(output_w_grad.size()); + if (v_output->size() == 0) { + v_output->resize(grads.output_w_grad->size()); } - v_hidden = alg.addition(v_hidden, alg.exponentiate(cumulative_hidden_layer_w_grad, 2)); + v_hidden = alg.addition_vt(v_hidden, alg.exponentiate_vt(grads.cumulative_hidden_layer_w_grad, 2)); + v_output = alg.additionnv(v_output, alg.exponentiatev(grads.output_w_grad, 2)); - v_output = alg.addition(v_output, alg.exponentiate(output_w_grad, 2)); - - std::vector>> hidden_layer_updations = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(cumulative_hidden_layer_w_grad, alg.scalarAdd(e, alg.sqrt(v_hidden)))); - std::vector output_layer_updation = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(output_w_grad, alg.scalarAdd(e, alg.sqrt(v_output)))); + Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_division_vt(grads.cumulative_hidden_layer_w_grad, alg.scalar_add_vm(e, alg.sqrt_vt(v_hidden)))); + Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_division(grads.output_w_grad, alg.scalar_addnv(e, alg.sqrtv(v_output)))); 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(input_mini_batches[i]); + y_hat = model_set_test(current_input_batch); if (ui) { - print_ui(epoch, cost_prev, y_hat, output_mini_batches[i]); + print_ui(epoch, cost_prev, y_hat, current_output_batch); } } + epoch++; + if (epoch > max_epoch) { break; } @@ -319,44 +348,44 @@ void MLPPANN::adadelta(real_t learning_rate, int max_epoch, int mini_batch_size, // always evaluate the result // always do forward pass only ONCE at end. - auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch); - auto input_mini_batches = std::get<0>(batches); - auto output_mini_batches = std::get<1>(batches); + MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. - std::vector>> v_hidden; + Vector> v_hidden; + + Ref v_output; + v_output.instance(); - std::vector v_output; 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++) { - std::vector y_hat = model_set_test(input_mini_batches[i]); - cost_prev = cost(y_hat, output_mini_batches[i]); + Ref current_input_batch = batches.input_sets[i]; + Ref current_output_batch = batches.output_sets[i]; - auto grads = compute_gradients(y_hat, output_mini_batches[i]); - auto cumulative_hidden_layer_w_grad = std::get<0>(grads); - auto output_w_grad = std::get<1>(grads); + 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.empty()) { // Initing our tensor - v_hidden = alg.resize(v_hidden, cumulative_hidden_layer_w_grad); + v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); } - if (v_output.empty()) { - v_output.resize(output_w_grad.size()); + if (v_output->size() == 0) { + v_output->resize(grads.output_w_grad->size()); } - v_hidden = alg.addition(alg.scalarMultiply(1 - b1, v_hidden), alg.scalarMultiply(b1, alg.exponentiate(cumulative_hidden_layer_w_grad, 2))); + v_hidden = alg.addition_vt(alg.scalar_multiply_vm(1 - b1, v_hidden), alg.scalar_multiply_vm(b1, alg.exponentiate_vt(grads.cumulative_hidden_layer_w_grad, 2))); + v_output = alg.additionnv(v_output, alg.exponentiatev(grads.output_w_grad, 2)); - v_output = alg.addition(v_output, alg.exponentiate(output_w_grad, 2)); - - std::vector>> hidden_layer_updations = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(cumulative_hidden_layer_w_grad, alg.scalarAdd(e, alg.sqrt(v_hidden)))); - std::vector output_layer_updation = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(output_w_grad, alg.scalarAdd(e, alg.sqrt(v_output)))); + Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_division_vt(grads.cumulative_hidden_layer_w_grad, alg.scalar_add_vm(e, alg.sqrt_vt(v_hidden)))); + Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_division(grads.output_w_grad, alg.scalar_addnv(e, alg.sqrtv(v_output)))); 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(input_mini_batches[i]); + y_hat = model_set_test(current_input_batch); if (ui) { - print_ui(epoch, cost_prev, y_hat, output_mini_batches[i]); + print_ui(epoch, cost_prev, y_hat, current_output_batch); } } @@ -383,56 +412,58 @@ void MLPPANN::adam(real_t learning_rate, int max_epoch, int mini_batch_size, rea // always evaluate the result // always do forward pass only ONCE at end. - auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch); - auto input_mini_batches = std::get<0>(batches); - auto output_mini_batches = std::get<1>(batches); + MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. - std::vector>> m_hidden; - std::vector>> v_hidden; + Vector> m_hidden; + Vector> v_hidden; + + Ref m_output; + Ref v_output; + m_output.instance(); + v_output.instance(); - std::vector m_output; - std::vector v_output; 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++) { - std::vector y_hat = model_set_test(input_mini_batches[i]); - cost_prev = cost(y_hat, output_mini_batches[i]); + Ref current_input_batch = batches.input_sets[i]; + Ref current_output_batch = batches.output_sets[i]; - auto grads = compute_gradients(y_hat, output_mini_batches[i]); - auto cumulative_hidden_layer_w_grad = std::get<0>(grads); - auto output_w_grad = std::get<1>(grads); + 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.empty() && v_hidden.empty()) { // Initing our tensor - m_hidden = alg.resize(m_hidden, cumulative_hidden_layer_w_grad); - v_hidden = alg.resize(v_hidden, cumulative_hidden_layer_w_grad); + m_hidden = alg.resize_vt(m_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); } - if (m_output.empty() && v_output.empty()) { - m_output.resize(output_w_grad.size()); - v_output.resize(output_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 = alg.addition(alg.scalarMultiply(b1, m_hidden), alg.scalarMultiply(1 - b1, cumulative_hidden_layer_w_grad)); - v_hidden = alg.addition(alg.scalarMultiply(b2, v_hidden), alg.scalarMultiply(1 - b2, alg.exponentiate(cumulative_hidden_layer_w_grad, 2))); + m_hidden = alg.addition_vt(alg.scalar_multiply_vm(b1, m_hidden), alg.scalar_multiply_vm(1 - b1, grads.cumulative_hidden_layer_w_grad)); + v_hidden = alg.addition_vt(alg.scalar_multiply_vm(b2, v_hidden), alg.scalar_multiply_vm(1 - b2, alg.exponentiate_vt(grads.cumulative_hidden_layer_w_grad, 2))); - m_output = alg.addition(alg.scalarMultiply(b1, m_output), alg.scalarMultiply(1 - b1, output_w_grad)); - v_output = alg.addition(alg.scalarMultiply(b2, v_output), alg.scalarMultiply(1 - b2, alg.exponentiate(output_w_grad, 2))); + m_output = alg.additionnv(alg.scalar_multiplynv(b1, m_output), alg.scalar_multiplynv(1 - b1, grads.output_w_grad)); + v_output = alg.additionnv(alg.scalar_multiplynv(b2, v_output), alg.scalar_multiplynv(1 - b2, alg.exponentiatev(grads.output_w_grad, 2))); - std::vector>> m_hidden_hat = alg.scalarMultiply(1 / (1 - std::pow(b1, epoch)), m_hidden); - std::vector>> v_hidden_hat = alg.scalarMultiply(1 / (1 - std::pow(b2, epoch)), v_hidden); + Vector> m_hidden_hat = alg.scalar_multiply_vm(1 / (1 - Math::pow(b1, epoch)), m_hidden); + Vector> v_hidden_hat = alg.scalar_multiply_vm(1 / (1 - Math::pow(b2, epoch)), v_hidden); - std::vector m_output_hat = alg.scalarMultiply(1 / (1 - std::pow(b1, epoch)), m_output); - std::vector v_output_hat = alg.scalarMultiply(1 / (1 - std::pow(b2, epoch)), v_output); + Ref m_output_hat = alg.scalar_multiplynv(1 / (1 - Math::pow(b1, epoch)), m_output); + Ref v_output_hat = alg.scalar_multiplynv(1 / (1 - Math::pow(b2, epoch)), v_output); - std::vector>> hidden_layer_updations = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(m_hidden_hat, alg.scalarAdd(e, alg.sqrt(v_hidden_hat)))); - std::vector output_layer_updation = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(m_output_hat, alg.scalarAdd(e, alg.sqrt(v_output_hat)))); + Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_division_vt(m_hidden_hat, alg.scalar_add_vm(e, alg.sqrt_vt(v_hidden_hat)))); + Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_division(m_output_hat, alg.scalar_addnv(e, alg.sqrtv(v_output_hat)))); 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(input_mini_batches[i]); + y_hat = model_set_test(current_input_batch); if (ui) { - print_ui(epoch, cost_prev, y_hat, output_mini_batches[i]); + print_ui(epoch, cost_prev, y_hat, current_output_batch); } } @@ -458,54 +489,54 @@ void MLPPANN::adamax(real_t learning_rate, int max_epoch, int mini_batch_size, r // always evaluate the result // always do forward pass only ONCE at end. - auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch); - auto input_mini_batches = std::get<0>(batches); - auto output_mini_batches = std::get<1>(batches); + MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. - std::vector>> m_hidden; - std::vector>> u_hidden; + Vector> m_hidden; + Vector> u_hidden; - std::vector m_output; - std::vector u_output; + Ref m_output; + Ref u_output; 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++) { - std::vector y_hat = model_set_test(input_mini_batches[i]); - cost_prev = cost(y_hat, output_mini_batches[i]); - auto grads = compute_gradients(y_hat, output_mini_batches[i]); - auto cumulative_hidden_layer_w_grad = std::get<0>(grads); - auto output_w_grad = std::get<1>(grads); + 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.empty() && u_hidden.empty()) { // Initing our tensor - m_hidden = alg.resize(m_hidden, cumulative_hidden_layer_w_grad); - u_hidden = alg.resize(u_hidden, cumulative_hidden_layer_w_grad); + m_hidden = alg.resize_vt(m_hidden, grads.cumulative_hidden_layer_w_grad); + u_hidden = alg.resize_vt(u_hidden, grads.cumulative_hidden_layer_w_grad); } - if (m_output.empty() && u_output.empty()) { - m_output.resize(output_w_grad.size()); - u_output.resize(output_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 = alg.addition(alg.scalarMultiply(b1, m_hidden), alg.scalarMultiply(1 - b1, cumulative_hidden_layer_w_grad)); - u_hidden = alg.max(alg.scalarMultiply(b2, u_hidden), alg.abs(cumulative_hidden_layer_w_grad)); + m_hidden = alg.addition_vt(alg.scalar_multiply_vm(b1, m_hidden), alg.scalar_multiply_vm(1 - b1, grads.cumulative_hidden_layer_w_grad)); + u_hidden = alg.max_vt(alg.scalar_multiply_vm(b2, u_hidden), alg.abs_vt(grads.cumulative_hidden_layer_w_grad)); - m_output = alg.addition(alg.scalarMultiply(b1, m_output), alg.scalarMultiply(1 - b1, output_w_grad)); - u_output = alg.max(alg.scalarMultiply(b2, u_output), alg.abs(output_w_grad)); + m_output = alg.additionnv(alg.scalar_multiplynv(b1, m_output), alg.scalar_multiplynv(1 - b1, grads.output_w_grad)); + u_output = alg.maxnvv(alg.scalar_multiplynv(b2, u_output), alg.absv(grads.output_w_grad)); - std::vector>> m_hidden_hat = alg.scalarMultiply(1 / (1 - std::pow(b1, epoch)), m_hidden); + Vector> m_hidden_hat = alg.scalar_multiply_vm(1 / (1 - Math::pow(b1, epoch)), m_hidden); - std::vector m_output_hat = alg.scalarMultiply(1 / (1 - std::pow(b1, epoch)), m_output); + Ref m_output_hat = alg.scalar_multiplynv(1 / (1 - Math::pow(b1, epoch)), m_output); - std::vector>> hidden_layer_updations = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(m_hidden_hat, alg.scalarAdd(e, u_hidden))); - std::vector output_layer_updation = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(m_output_hat, alg.scalarAdd(e, u_output))); + Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_division_vt(m_hidden_hat, alg.scalar_add_vm(e, u_hidden))); + Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_division(m_output_hat, alg.scalar_addnv(e, u_output))); 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(input_mini_batches[i]); + y_hat = model_set_test(current_input_batch); if (ui) { - print_ui(epoch, cost_prev, y_hat, output_mini_batches[i]); + print_ui(epoch, cost_prev, y_hat, current_output_batch); } } @@ -531,58 +562,59 @@ void MLPPANN::nadam(real_t learning_rate, int max_epoch, int mini_batch_size, re // always evaluate the result // always do forward pass only ONCE at end. - auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch); - auto input_mini_batches = std::get<0>(batches); - auto output_mini_batches = std::get<1>(batches); + MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. - std::vector>> m_hidden; - std::vector>> v_hidden; + Vector> m_hidden; + Vector> v_hidden; - std::vector m_output; - std::vector v_output; + Ref m_output; + Ref v_output; 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++) { - std::vector y_hat = model_set_test(input_mini_batches[i]); - cost_prev = cost(y_hat, output_mini_batches[i]); - auto grads = compute_gradients(y_hat, output_mini_batches[i]); - auto cumulative_hidden_layer_w_grad = std::get<0>(grads); - auto output_w_grad = std::get<1>(grads); + 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.empty() && v_hidden.empty()) { // Initing our tensor - m_hidden = alg.resize(m_hidden, cumulative_hidden_layer_w_grad); - v_hidden = alg.resize(v_hidden, cumulative_hidden_layer_w_grad); + m_hidden = alg.resize_vt(m_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); } - if (m_output.empty() && v_output.empty()) { - m_output.resize(output_w_grad.size()); - v_output.resize(output_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 = alg.addition(alg.scalarMultiply(b1, m_hidden), alg.scalarMultiply(1 - b1, cumulative_hidden_layer_w_grad)); - v_hidden = alg.addition(alg.scalarMultiply(b2, v_hidden), alg.scalarMultiply(1 - b2, alg.exponentiate(cumulative_hidden_layer_w_grad, 2))); + m_hidden = alg.addition_vt(alg.scalar_multiply_vm(b1, m_hidden), alg.scalar_multiply_vm(1 - b1, grads.cumulative_hidden_layer_w_grad)); + v_hidden = alg.addition_vt(alg.scalar_multiply_vm(b2, v_hidden), alg.scalar_multiply_vm(1 - b2, alg.exponentiate_vt(grads.cumulative_hidden_layer_w_grad, 2))); - m_output = alg.addition(alg.scalarMultiply(b1, m_output), alg.scalarMultiply(1 - b1, output_w_grad)); - v_output = alg.addition(alg.scalarMultiply(b2, v_output), alg.scalarMultiply(1 - b2, alg.exponentiate(output_w_grad, 2))); + m_output = alg.additionnv(alg.scalar_multiplynv(b1, m_output), alg.scalar_multiplynv(1 - b1, grads.output_w_grad)); + v_output = alg.additionnv(alg.scalar_multiplynv(b2, v_output), alg.scalar_multiplynv(1 - b2, alg.exponentiatev(grads.output_w_grad, 2))); - std::vector>> m_hidden_hat = alg.scalarMultiply(1 / (1 - std::pow(b1, epoch)), m_hidden); - std::vector>> v_hidden_hat = alg.scalarMultiply(1 / (1 - std::pow(b2, epoch)), v_hidden); - std::vector>> m_hidden_final = alg.addition(alg.scalarMultiply(b1, m_hidden_hat), alg.scalarMultiply((1 - b1) / (1 - std::pow(b1, epoch)), cumulative_hidden_layer_w_grad)); + Vector> m_hidden_hat = alg.scalar_multiply_vm(1 / (1.0 - Math::pow(b1, epoch)), m_hidden); + Vector> v_hidden_hat = alg.scalar_multiply_vm(1 / (1.0 - Math::pow(b2, epoch)), v_hidden); + Vector> m_hidden_final = alg.addition_vt(alg.scalar_multiply_vm(b1, m_hidden_hat), alg.scalar_multiply_vm((1 - b1) / (1 - Math::pow(b1, epoch)), grads.cumulative_hidden_layer_w_grad)); - std::vector m_output_hat = alg.scalarMultiply(1 / (1 - std::pow(b1, epoch)), m_output); - std::vector v_output_hat = alg.scalarMultiply(1 / (1 - std::pow(b2, epoch)), v_output); - std::vector m_output_final = alg.addition(alg.scalarMultiply(b1, m_output_hat), alg.scalarMultiply((1 - b1) / (1 - std::pow(b1, epoch)), output_w_grad)); + Ref m_output_hat = alg.scalar_multiplynv(1 / (1.0 - Math::pow(b1, epoch)), m_output); + Ref v_output_hat = alg.scalar_multiplynv(1 / (1.0 - Math::pow(b2, epoch)), v_output); + Ref m_output_final = alg.additionnv(alg.scalar_multiplynv(b1, m_output_hat), alg.scalar_multiplynv((1 - b1) / (1.0 - Math::pow(b1, epoch)), grads.output_w_grad)); - std::vector>> hidden_layer_updations = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(m_hidden_final, alg.scalarAdd(e, alg.sqrt(v_hidden_hat)))); - std::vector output_layer_updation = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(m_output_final, alg.scalarAdd(e, alg.sqrt(v_output_hat)))); + Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_division_vt(m_hidden_final, alg.scalar_add_vm(e, alg.sqrt_vt(v_hidden_hat)))); + Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_divisionm(m_output_final, alg.scalar_addnv(e, alg.sqrtv(v_output_hat)))); 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(input_mini_batches[i]); + + y_hat = model_set_test(current_input_batch); if (ui) { - print_ui(epoch, cost_prev, y_hat, output_mini_batches[i]); + print_ui(epoch, cost_prev, y_hat, current_output_batch); } } @@ -609,60 +641,59 @@ void MLPPANN::amsgrad(real_t learning_rate, int max_epoch, int mini_batch_size, // always evaluate the result // always do forward pass only ONCE at end. - auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch); - auto input_mini_batches = std::get<0>(batches); - auto output_mini_batches = std::get<1>(batches); + MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); // Initializing necessary components for Adam. - std::vector>> m_hidden; - std::vector>> v_hidden; + Vector> m_hidden; + Vector> v_hidden; - std::vector>> v_hidden_hat; + Vector> v_hidden_hat; - std::vector m_output; - std::vector v_output; + Ref m_output; + Ref v_output; - std::vector v_output_hat; + Ref v_output_hat; 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++) { - std::vector y_hat = model_set_test(input_mini_batches[i]); - cost_prev = cost(y_hat, output_mini_batches[i]); - auto grads = compute_gradients(y_hat, output_mini_batches[i]); - auto cumulative_hidden_layer_w_grad = std::get<0>(grads); - auto output_w_grad = std::get<1>(grads); + 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.empty() && v_hidden.empty()) { // Initing our tensor - m_hidden = alg.resize(m_hidden, cumulative_hidden_layer_w_grad); - v_hidden = alg.resize(v_hidden, cumulative_hidden_layer_w_grad); - v_hidden_hat = alg.resize(v_hidden_hat, cumulative_hidden_layer_w_grad); + m_hidden = alg.resize_vt(m_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden_hat = alg.resize_vt(v_hidden_hat, grads.cumulative_hidden_layer_w_grad); } - if (m_output.empty() && v_output.empty()) { - m_output.resize(output_w_grad.size()); - v_output.resize(output_w_grad.size()); - v_output_hat.resize(output_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 = alg.addition(alg.scalarMultiply(b1, m_hidden), alg.scalarMultiply(1 - b1, cumulative_hidden_layer_w_grad)); - v_hidden = alg.addition(alg.scalarMultiply(b2, v_hidden), alg.scalarMultiply(1 - b2, alg.exponentiate(cumulative_hidden_layer_w_grad, 2))); + m_hidden = alg.addition_vt(alg.scalar_multiply_vm(b1, m_hidden), alg.scalar_multiply_vm(1 - b1, grads.cumulative_hidden_layer_w_grad)); + v_hidden = alg.addition_vt(alg.scalar_multiply_vm(b2, v_hidden), alg.scalar_multiply_vm(1 - b2, alg.exponentiate_vt(grads.cumulative_hidden_layer_w_grad, 2))); - m_output = alg.addition(alg.scalarMultiply(b1, m_output), alg.scalarMultiply(1 - b1, output_w_grad)); - v_output = alg.addition(alg.scalarMultiply(b2, v_output), alg.scalarMultiply(1 - b2, alg.exponentiate(output_w_grad, 2))); + m_output = alg.additionnv(alg.scalar_multiplynv(b1, m_output), alg.scalar_multiplynv(1 - b1, grads.output_w_grad)); + v_output = alg.additionnv(alg.scalar_multiplynv(b2, v_output), alg.scalar_multiplynv(1 - b2, alg.exponentiatev(grads.output_w_grad, 2))); - v_hidden_hat = alg.max(v_hidden_hat, v_hidden); + v_hidden_hat = alg.max_vt(v_hidden_hat, v_hidden); + v_output_hat = alg.maxnvv(v_output_hat, v_output); - v_output_hat = alg.max(v_output_hat, v_output); - - std::vector>> hidden_layer_updations = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(m_hidden, alg.scalarAdd(e, alg.sqrt(v_hidden_hat)))); - std::vector output_layer_updation = alg.scalarMultiply(learning_rate / _n, alg.elementWiseDivision(m_output, alg.scalarAdd(e, alg.sqrt(v_output_hat)))); + Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_division_vt(m_hidden, alg.scalar_add_vm(e, alg.sqrt_vt(v_hidden_hat)))); + Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_division(m_output, alg.scalar_addnv(e, alg.sqrtv(v_output_hat)))); 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(input_mini_batches[i]); + y_hat = model_set_test(current_input_batch); if (ui) { - print_ui(epoch, cost_prev, y_hat, output_mini_batches[i]); + print_ui(epoch, cost_prev, y_hat, current_output_batch); } } @@ -678,74 +709,64 @@ void MLPPANN::amsgrad(real_t learning_rate, int max_epoch, int mini_batch_size, real_t MLPPANN::score() { MLPPUtilities util; + forward_pass(); - return util.performance(_y_hat, _output_set); + + return util.performance_vec(_y_hat, _output_set); } -void MLPPANN::save(std::string fileName) { +void MLPPANN::save(const String &file_name) { MLPPUtilities util; + + /* if (!_network.empty()) { - util.saveParameters(fileName, _network[0].weights, _network[0].bias, false, 1); + util.saveParameters(file_name, _network[0].weights, _network[0].bias, false, 1); for (uint32_t i = 1; i < _network.size(); i++) { - util.saveParameters(fileName, _network[i].weights, _network[i].bias, true, i + 1); + util.saveParameters(file_name, _network[i].weights, _network[i].bias, true, i + 1); } - util.saveParameters(fileName, _output_layer->weights, _output_layer->bias, true, _network.size() + 1); + util.saveParameters(file_name, _output_layer->weights, _output_layer->bias, true, _network.size() + 1); } else { - util.saveParameters(fileName, _output_layer->weights, _output_layer->bias, false, _network.size() + 1); + util.saveParameters(file_name, _output_layer->weights, _output_layer->bias, false, _network.size() + 1); } + */ } -void MLPPANN::set_learning_rate_scheduler(std::string type, real_t decay_constant) { +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(std::string type, real_t decay_constant, real_t drop_rate) { +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; } -// 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 == "Time") { - return learning_rate / (1 + decay_constant * epoch); - } else if (_lr_scheduler == "Epoch") { - return learning_rate * (decay_constant / std::sqrt(epoch)); - } else if (_lr_scheduler == "Step") { - return learning_rate * std::pow(decay_constant, int((1 + epoch) / drop_rate)); // Utilizing an explicit int conversion implicitly takes the floor. - } else if (_lr_scheduler == "Exponential") { - return learning_rate * std::exp(-decay_constant * epoch); - } - return learning_rate; -} - -void MLPPANN::add_layer(int n_hidden, std::string activation, std::string weightInit, std::string reg, real_t lambda, real_t alpha) { +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(MLPPOldHiddenLayer(n_hidden, activation, _input_set, weightInit, reg, lambda, alpha)); - _network[0].forwardPass(); + _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(MLPPOldHiddenLayer(n_hidden, activation, _network[_network.size() - 1].a, weightInit, reg, lambda, alpha)); - _network[_network.size() - 1].forwardPass(); + _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(std::string activation, std::string loss, std::string weightInit, std::string reg, real_t lambda, real_t alpha) { +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 = new MLPPOldOutputLayer(_network[_network.size() - 1].n_hidden, activation, loss, _network[_network.size() - 1].a, weightInit, reg, lambda, alpha); + _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 = new MLPPOldOutputLayer(_k, activation, loss, _input_set, weightInit, reg, lambda, alpha); + _output_layer = Ref(memnew(MLPPOutputLayer(_k, activation, loss, _input_set, weight_init, reg, lambda, alpha))); } } -MLPPANN::MLPPANN(std::vector> p_input_set, std::vector p_output_set) { +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(); - _k = _input_set[0].size(); - _lr_scheduler = "None"; + _n = _input_set->size().y; + _k = _input_set->size().x; + _lr_scheduler = SCHEDULER_TYPE_NONE; _decay_constant = 0; _drop_rate = 0; } @@ -754,100 +775,135 @@ MLPPANN::MLPPANN() { } MLPPANN::~MLPPANN() { - delete _output_layer; } -real_t MLPPANN::cost(std::vector y_hat, std::vector y) { +// 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 totalRegTerm = 0; - auto cost_function = _output_layer->cost_map[_output_layer->cost]; + real_t total_reg_term = 0; if (!_network.empty()) { - for (uint32_t i = 0; i < _network.size() - 1; i++) { - totalRegTerm += regularization.regTerm(_network[i].weights, _network[i].lambda, _network[i].alpha, _network[i].reg); + 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.*cost_function)(y_hat, y) + totalRegTerm + regularization.regTerm(_output_layer->weights, _output_layer->lambda, _output_layer->alpha, _output_layer->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()) { - _network[0].input = _input_set; - _network[0].forwardPass(); + Ref layer = _network[0]; - for (uint32_t i = 1; i < _network.size(); i++) { - _network[i].input = _network[i - 1].a; - _network[i].forwardPass(); + 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->input = _network[_network.size() - 1].a; + + _output_layer->set_input(_network.write[_network.size() - 1]->get_a()); } else { - _output_layer->input = _input_set; + _output_layer->set_input(_input_set); } - _output_layer->forwardPass(); - _y_hat = _output_layer->a; + _output_layer->forward_pass(); + + _y_hat = _output_layer->get_a(); } -void MLPPANN::update_parameters(std::vector>> hidden_layer_updations, std::vector output_layer_updation, real_t learning_rate) { +void MLPPANN::update_parameters(const Vector> &hidden_layer_updations, const Ref &output_layer_updation, real_t learning_rate) { MLPPLinAlg alg; - _output_layer->weights = alg.subtraction(_output_layer->weights, output_layer_updation); - _output_layer->bias -= learning_rate * alg.sum_elements(_output_layer->delta) / _n; + _output_layer->set_weights(alg.subtractionnv(_output_layer->get_weights(), output_layer_updation)); + _output_layer->set_bias(_output_layer->get_bias() - learning_rate * alg.sum_elementsv(_output_layer->get_delta()) / _n); if (!_network.empty()) { - _network[_network.size() - 1].weights = alg.subtraction(_network[_network.size() - 1].weights, hidden_layer_updations[0]); - _network[_network.size() - 1].bias = alg.subtractMatrixRows(_network[_network.size() - 1].bias, alg.scalarMultiply(learning_rate / _n, _network[_network.size() - 1].delta)); + Ref layer = _network[_network.size() - 1]; + + layer->set_weights(alg.subtractionm(layer->get_weights(), hidden_layer_updations[0])); + layer->set_bias(alg.subtract_matrix_rows(layer->get_bias(), alg.scalar_multiplym(learning_rate / _n, layer->get_delta()))); for (int i = _network.size() - 2; i >= 0; i--) { - _network[i].weights = alg.subtraction(_network[i].weights, hidden_layer_updations[(_network.size() - 2) - i + 1]); - _network[i].bias = alg.subtractMatrixRows(_network[i].bias, alg.scalarMultiply(learning_rate / _n, _network[i].delta)); + layer = _network[i]; + + layer->set_weights(alg.subtractionm(layer->get_weights(), hidden_layer_updations[(_network.size() - 2) - i + 1])); + layer->set_bias(alg.subtract_matrix_rows(layer->get_bias(), alg.scalar_multiplym(learning_rate / _n, layer->get_delta()))); } } } -std::tuple>>, std::vector> MLPPANN::compute_gradients(std::vector y_hat, std::vector _output_set) { +MLPPANN::ComputeGradientsResult MLPPANN::compute_gradients(const Ref &y_hat, const Ref &_output_set) { // std::cout << "BEGIN" << std::endl; MLPPCost mlpp_cost; MLPPActivation avn; MLPPLinAlg alg; MLPPReg regularization; - std::vector>> cumulative_hidden_layer_w_grad; // Tensor containing ALL hidden grads. + ComputeGradientsResult res; - auto costDeriv = _output_layer->costDeriv_map[_output_layer->cost]; - auto outputAvn = _output_layer->activation_map[_output_layer->activation]; - _output_layer->delta = alg.hadamard_product((mlpp_cost.*costDeriv)(y_hat, _output_set), (avn.*outputAvn)(_output_layer->z, 1)); - std::vector output_w_grad = alg.mat_vec_mult(alg.transpose(_output_layer->input), _output_layer->delta); - output_w_grad = alg.addition(output_w_grad, regularization.regDerivTerm(_output_layer->weights, _output_layer->lambda, _output_layer->alpha, _output_layer->reg)); + _output_layer->set_delta(alg.hadamard_productnv(mlpp_cost.run_cost_deriv_vector(_output_layer->get_cost(), y_hat, _output_set), avn.run_activation_deriv_vector(_output_layer->get_activation(), _output_layer->get_z()))); + + res.output_w_grad = alg.mat_vec_multv(alg.transposem(_output_layer->get_input()), _output_layer->get_delta()); + res.output_w_grad = alg.additionnv(res.output_w_grad, regularization.reg_deriv_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg())); if (!_network.empty()) { - auto hiddenLayerAvn = _network[_network.size() - 1].activation_map[_network[_network.size() - 1].activation]; - _network[_network.size() - 1].delta = alg.hadamard_product(alg.outerProduct(_output_layer->delta, _output_layer->weights), (avn.*hiddenLayerAvn)(_network[_network.size() - 1].z, 1)); - std::vector> hiddenLayerWGrad = alg.matmult(alg.transpose(_network[_network.size() - 1].input), _network[_network.size() - 1].delta); + Ref layer = _network[_network.size() - 1]; - cumulative_hidden_layer_w_grad.push_back(alg.addition(hiddenLayerWGrad, regularization.regDerivTerm(_network[_network.size() - 1].weights, _network[_network.size() - 1].lambda, _network[_network.size() - 1].alpha, _network[_network.size() - 1].reg))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well. + layer->set_delta(alg.hadamard_productm(alg.outer_product(_output_layer->get_delta(), _output_layer->get_weights()), avn.run_activation_deriv_vector(layer->get_activation(), layer->get_z()))); + + Ref hidden_layer_w_grad = alg.matmultm(alg.transposem(layer->get_input()), layer->get_delta()); + + res.cumulative_hidden_layer_w_grad.push_back(alg.additionm(hidden_layer_w_grad, 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--) { - hiddenLayerAvn = _network[i].activation_map[_network[i].activation]; - _network[i].delta = alg.hadamard_product(alg.matmult(_network[i + 1].delta, alg.transpose(_network[i + 1].weights)), (avn.*hiddenLayerAvn)(_network[i].z, 1)); - hiddenLayerWGrad = alg.matmult(alg.transpose(_network[i].input), _network[i].delta); - cumulative_hidden_layer_w_grad.push_back(alg.addition(hiddenLayerWGrad, regularization.regDerivTerm(_network[i].weights, _network[i].lambda, _network[i].alpha, _network[i].reg))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well. + layer = _network[i]; + Ref next_layer = _network[i + 1]; + + layer->set_delta(alg.hadamard_productm(alg.matmultm(next_layer->get_delta(), alg.transposem(next_layer->get_weights())), avn.run_activation_deriv_vector(layer->get_activation(), layer->get_z()))); + hidden_layer_w_grad = alg.matmultm(alg.transposem(layer->get_input()), layer->get_delta()); + res.cumulative_hidden_layer_w_grad.push_back(alg.additionm(hidden_layer_w_grad, 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 { cumulative_hidden_layer_w_grad, output_w_grad }; + + return res; } -void MLPPANN::print_ui(int epoch, real_t cost_prev, std::vector y_hat, std::vector p_output_set) { - MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, p_output_set)); - std::cout << "Layer " << _network.size() + 1 << ": " << std::endl; - MLPPUtilities::UI(_output_layer->weights, _output_layer->bias); +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--) { - std::cout << "Layer " << i + 1 << ": " << std::endl; - MLPPUtilities::UI(_network[i].weights, _network[i].bias); + Ref layer = _network[i]; + + PLOG_MSG("Layer " + itos(i + 1) + ": "); + MLPPUtilities::print_ui_mb(layer->get_weights(), layer->get_bias()); } } } diff --git a/mlpp/ann/ann.h b/mlpp/ann/ann.h index 6e7a998..00775f5 100644 --- a/mlpp/ann/ann.h +++ b/mlpp/ann/ann.h @@ -11,22 +11,32 @@ #include "core/object/reference.h" +#include "../lin_alg/mlpp_matrix.h" +#include "../lin_alg/mlpp_vector.h" + #include "../hidden_layer/hidden_layer.h" #include "../output_layer/output_layer.h" -#include "../hidden_layer/hidden_layer_old.h" -#include "../output_layer/output_layer_old.h" - -#include -#include -#include +#include "../activation/activation.h" +#include "../cost/cost.h" +#include "../regularization/reg.h" +#include "../utilities/utilities.h" class MLPPANN : public Reference { GDCLASS(MLPPANN, Reference); public: - std::vector model_set_test(std::vector> X); - real_t model_test(std::vector x); + enum SchedulerType { + SCHEDULER_TYPE_NONE = 0, + SCHEDULER_TYPE_TIME, + SCHEDULER_TYPE_EPOCH, + SCHEDULER_TYPE_STEP, + SCHEDULER_TYPE_EXPONENTIAL, + }; + +public: + Ref model_set_test(const Ref &X); + real_t model_test(const Ref &x); void gradient_descent(real_t learning_rate, int max_epoch, bool ui = false); void sgd(real_t learning_rate, int max_epoch, bool ui = false); @@ -40,15 +50,15 @@ public: void amsgrad(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool ui = false); real_t score(); - void save(std::string file_name); + void save(const String &file_name); - void set_learning_rate_scheduler(std::string type, real_t decay_constant); - void set_learning_rate_scheduler_drop(std::string type, real_t decay_constant, real_t drop_rate); + void set_learning_rate_scheduler(SchedulerType type, real_t decay_constant); + void set_learning_rate_scheduler_drop(SchedulerType type, real_t decay_constant, real_t drop_rate); - void add_layer(int n_hidden, std::string activation, std::string weight_init = "Default", std::string reg = "None", real_t lambda = 0.5, real_t alpha = 0.5); - void add_output_layer(std::string activation, std::string loss, std::string weight_init = "Default", std::string reg = "None", real_t lambda = 0.5, real_t alpha = 0.5); + void add_layer(int n_hidden, MLPPActivation::ActivationFunction activation, MLPPUtilities::WeightDistributionType weight_init = MLPPUtilities::WEIGHT_DISTRIBUTION_TYPE_DEFAULT, MLPPReg::RegularizationType reg = MLPPReg::REGULARIZATION_TYPE_NONE, real_t lambda = 0.5, real_t alpha = 0.5); + void add_output_layer(MLPPActivation::ActivationFunction activation, MLPPCost::CostTypes loss, MLPPUtilities::WeightDistributionType weight_init = MLPPUtilities::WEIGHT_DISTRIBUTION_TYPE_DEFAULT, MLPPReg::RegularizationType reg = MLPPReg::REGULARIZATION_TYPE_NONE, real_t lambda = 0.5, real_t alpha = 0.5); - MLPPANN(std::vector> p_input_set, std::vector p_output_set); + MLPPANN(const Ref &p_input_set, const Ref &p_output_set); MLPPANN(); ~MLPPANN(); @@ -56,29 +66,37 @@ public: protected: real_t apply_learning_rate_scheduler(real_t learning_rate, real_t decay_constant, real_t epoch, real_t drop_rate); - real_t cost(std::vector y_hat, std::vector y); + real_t cost(const Ref &y_hat, const Ref &y); void forward_pass(); - void update_parameters(std::vector>> hidden_layer_updations, std::vector output_layer_updation, real_t learning_rate); - std::tuple>>, std::vector> compute_gradients(std::vector y_hat, std::vector _output_set); + void update_parameters(const Vector> &hidden_layer_updations, const Ref &output_layer_updation, real_t learning_rate); - void print_ui(int epoch, real_t cost_prev, std::vector y_hat, std::vector p_output_set); + struct ComputeGradientsResult { + Vector> cumulative_hidden_layer_w_grad; + Ref output_w_grad; + }; + + ComputeGradientsResult compute_gradients(const Ref &y_hat, const Ref &_output_set); + + void print_ui(int epoch, real_t cost_prev, const Ref &y_hat, const Ref &p_output_set); static void _bind_methods(); - std::vector> _input_set; - std::vector _output_set; - std::vector _y_hat; + Ref _input_set; + Ref _output_set; + Ref _y_hat; - std::vector _network; - MLPPOldOutputLayer *_output_layer; + Vector> _network; + Ref _output_layer; int _n; int _k; - std::string _lr_scheduler; + SchedulerType _lr_scheduler; real_t _decay_constant; real_t _drop_rate; }; +VARIANT_ENUM_CAST(MLPPANN::SchedulerType); + #endif /* ANN_hpp */ \ No newline at end of file diff --git a/mlpp/gan/gan.cpp b/mlpp/gan/gan.cpp index 62815ce..0af5a3b 100644 --- a/mlpp/gan/gan.cpp +++ b/mlpp/gan/gan.cpp @@ -142,9 +142,9 @@ void MLPPGAN::add_output_layer(MLPPUtilities::WeightDistributionType weight_init MLPPLinAlg alg; if (!_network.empty()) { - _output_layer = Ref(memnew(MLPPOutputLayer(_network.write[_network.size() - 1]->get_n_hidden(), MLPPActivation::ACTIVATION_FUNCTION_SIGMOID, _network.write[_network.size() - 1]->get_a(), weight_init, reg, lambda, alpha))); + _output_layer = Ref(memnew(MLPPOutputLayer(_network.write[_network.size() - 1]->get_n_hidden(), MLPPActivation::ACTIVATION_FUNCTION_SIGMOID, MLPPCost::COST_TYPE_LOGISTIC_LOSS, _network.write[_network.size() - 1]->get_a(), weight_init, reg, lambda, alpha))); } else { - _output_layer = Ref(memnew(MLPPOutputLayer(_k, MLPPActivation::ACTIVATION_FUNCTION_SIGMOID, alg.gaussian_noise(_n, _k), weight_init, reg, lambda, alpha))); + _output_layer = Ref(memnew(MLPPOutputLayer(_k, MLPPActivation::ACTIVATION_FUNCTION_SIGMOID, MLPPCost::COST_TYPE_LOGISTIC_LOSS, alg.gaussian_noise(_n, _k), weight_init, reg, lambda, alpha))); } } diff --git a/mlpp/lin_alg/lin_alg.cpp b/mlpp/lin_alg/lin_alg.cpp index 48e12e0..cd16d59 100644 --- a/mlpp/lin_alg/lin_alg.cpp +++ b/mlpp/lin_alg/lin_alg.cpp @@ -2499,6 +2499,24 @@ std::vector> MLPPLinAlg::max(std::vector return C; } +Ref MLPPLinAlg::max_nm(const Ref &A, const Ref &B) { + Ref C; + C.instance(); + C->resize(A->size()); + + const real_t *a_ptr = A->ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = C->ptrw(); + + int size = A->data_size(); + + for (int i = 0; i < size; i++) { + c_ptr[i] = MAX(a_ptr[i], b_ptr[i]); + } + + return C; +} + real_t MLPPLinAlg::max(std::vector a) { int max = a[0]; for (uint32_t i = 0; i < a.size(); i++) { @@ -2749,6 +2767,17 @@ std::vector>> MLPPLinAlg::addition(std::vector> MLPPLinAlg::addition_vt(const Vector> &A, const Vector> &B) { + Vector> res; + res.resize(A.size()); + + for (int i = 0; i < res.size(); i++) { + res.write[i] = additionm(A[i], B[i]); + } + + return res; +} + std::vector>> MLPPLinAlg::elementWiseDivision(std::vector>> A, std::vector>> B) { for (uint32_t i = 0; i < A.size(); i++) { A[i] = elementWiseDivision(A[i], B[i]); @@ -2756,6 +2785,17 @@ std::vector>> MLPPLinAlg::elementWiseDivision(st return A; } +Vector> MLPPLinAlg::element_wise_division_vt(const Vector> &A, const Vector> &B) { + Vector> res; + res.resize(A.size()); + + for (int i = 0; i < A.size(); i++) { + res.write[i] = element_wise_divisionm(A[i], B[i]); + } + + return res; +} + std::vector>> MLPPLinAlg::sqrt(std::vector>> A) { for (uint32_t i = 0; i < A.size(); i++) { A[i] = sqrt(A[i]); @@ -2763,6 +2803,17 @@ std::vector>> MLPPLinAlg::sqrt(std::vector> MLPPLinAlg::sqrt_vt(const Vector> &A) { + Vector> res; + res.resize(A.size()); + + for (int i = 0; i < A.size(); i++) { + res.write[i] = sqrtm(A[i]); + } + + return res; +} + std::vector>> MLPPLinAlg::exponentiate(std::vector>> A, real_t p) { for (uint32_t i = 0; i < A.size(); i++) { A[i] = exponentiate(A[i], p); @@ -2770,6 +2821,17 @@ std::vector>> MLPPLinAlg::exponentiate(std::vect return A; } +Vector> MLPPLinAlg::exponentiate_vt(const Vector> &A, real_t p) { + Vector> res; + res.resize(A.size()); + + for (int i = 0; i < A.size(); i++) { + res.write[i] = exponentiatem(A[i], p); + } + + return res; +} + std::vector> MLPPLinAlg::tensor_vec_mult(std::vector>> A, std::vector b) { std::vector> C; C.resize(A.size()); @@ -2840,6 +2902,21 @@ std::vector>> MLPPLinAlg::resize(std::vector> MLPPLinAlg::resize_vt(const Vector> &A, const Vector> &B) { + Vector> res; + res.resize(B.size()); + + for (int i = 0; i < res.size(); i++) { + Ref m; + m.instance(); + m->resize(B[i]->size()); + + res.write[i] = m; + } + + return res; +} + std::vector>> MLPPLinAlg::max(std::vector>> A, std::vector>> B) { for (uint32_t i = 0; i < A.size(); i++) { A[i] = max(A[i], B[i]); @@ -2847,6 +2924,17 @@ std::vector>> MLPPLinAlg::max(std::vector> MLPPLinAlg::max_vt(const Vector> &A, const Vector> &B) { + Vector> res; + res.resize(A.size()); + + for (int i = 0; i < A.size(); i++) { + res.write[i] = max_nm(A[i], B[i]); + } + + return res; +} + std::vector>> MLPPLinAlg::abs(std::vector>> A) { for (uint32_t i = 0; i < A.size(); i++) { A[i] = abs(A[i]); @@ -2854,6 +2942,17 @@ std::vector>> MLPPLinAlg::abs(std::vector> MLPPLinAlg::abs_vt(const Vector> &A) { + Vector> res; + res.resize(A.size()); + + for (int i = 0; i < A.size(); i++) { + res.write[i] = absm(A[i]); + } + + return A; +} + real_t MLPPLinAlg::norm_2(std::vector>> A) { real_t sum = 0; for (uint32_t i = 0; i < A.size(); i++) { diff --git a/mlpp/lin_alg/lin_alg.h b/mlpp/lin_alg/lin_alg.h index 50da6d3..84ba6f8 100644 --- a/mlpp/lin_alg/lin_alg.h +++ b/mlpp/lin_alg/lin_alg.h @@ -111,6 +111,8 @@ public: std::vector> rotate(std::vector> A, real_t theta, int axis = -1); std::vector> max(std::vector> A, std::vector> B); + Ref max_nm(const Ref &A, const Ref &B); + real_t max(std::vector> A); real_t min(std::vector> A); @@ -305,11 +307,16 @@ public: // TENSOR FUNCTIONS std::vector>> addition(std::vector>> A, std::vector>> B); + Vector> addition_vt(const Vector> &A, const Vector> &B); + std::vector>> elementWiseDivision(std::vector>> A, std::vector>> B); + Vector> element_wise_division_vt(const Vector> &A, const Vector> &B); std::vector>> sqrt(std::vector>> A); + Vector> sqrt_vt(const Vector> &A); std::vector>> exponentiate(std::vector>> A, real_t p); + Vector> exponentiate_vt(const Vector> &A, real_t p); std::vector> tensor_vec_mult(std::vector>> A, std::vector b); @@ -325,11 +332,15 @@ public: std::vector>> resize(std::vector>> A, std::vector>> B); + Vector> resize_vt(const Vector> &A, const Vector> &B); + std::vector>> hadamard_product(std::vector>> A, std::vector>> B); std::vector>> max(std::vector>> A, std::vector>> B); + Vector> max_vt(const Vector> &A, const Vector> &B); std::vector>> abs(std::vector>> A); + Vector> abs_vt(const Vector> &A); real_t norm_2(std::vector>> A); diff --git a/mlpp/output_layer/output_layer.cpp b/mlpp/output_layer/output_layer.cpp index 067870b..0182cde 100644 --- a/mlpp/output_layer/output_layer.cpp +++ b/mlpp/output_layer/output_layer.cpp @@ -72,18 +72,18 @@ void MLPPOutputLayer::set_a(const Ref &val) { _initialized = false; } -Ref MLPPOutputLayer::get_z_test() { +real_t MLPPOutputLayer::get_z_test() { return _z_test; } -void MLPPOutputLayer::set_z_test(const Ref &val) { +void MLPPOutputLayer::set_z_test(const real_t val) { _z_test = val; _initialized = false; } -Ref MLPPOutputLayer::get_a_test() { +real_t MLPPOutputLayer::get_a_test() { return _a_test; } -void MLPPOutputLayer::set_a_test(const Ref &val) { +void MLPPOutputLayer::set_a_test(const real_t val) { _a_test = val; _initialized = false; } @@ -166,12 +166,13 @@ void MLPPOutputLayer::test(const Ref &x) { MLPPActivation avn; _z_test = alg.dotv(_weights, x) + _bias; - _a_test = avn.run_activation_norm_vector(_activation, _z_test); + _a_test = avn.run_activation_norm_real(_activation, _z_test); } -MLPPOutputLayer::MLPPOutputLayer(int p_n_hidden, MLPPActivation::ActivationFunction p_activation, Ref p_input, MLPPUtilities::WeightDistributionType p_weight_init, MLPPReg::RegularizationType p_reg, real_t p_lambda, real_t p_alpha) { +MLPPOutputLayer::MLPPOutputLayer(int p_n_hidden, MLPPActivation::ActivationFunction p_activation, MLPPCost::CostTypes p_cost, Ref p_input, MLPPUtilities::WeightDistributionType p_weight_init, MLPPReg::RegularizationType p_reg, real_t p_lambda, real_t p_alpha) { _n_hidden = p_n_hidden; _activation = p_activation; + _cost = p_cost; _input = p_input; @@ -185,8 +186,8 @@ MLPPOutputLayer::MLPPOutputLayer(int p_n_hidden, MLPPActivation::ActivationFunct _z.instance(); _a.instance(); - _z_test.instance(); - _a_test.instance(); + _z_test = 0; + _a_test = 0; _delta.instance(); @@ -217,8 +218,8 @@ MLPPOutputLayer::MLPPOutputLayer() { _z.instance(); _a.instance(); - _z_test.instance(); - _a_test.instance(); + _z_test = 0; + _a_test = 0; _delta.instance(); @@ -265,11 +266,11 @@ void MLPPOutputLayer::_bind_methods() { ClassDB::bind_method(D_METHOD("get_z_test"), &MLPPOutputLayer::get_z_test); ClassDB::bind_method(D_METHOD("set_z_test", "val"), &MLPPOutputLayer::set_z_test); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "z_test", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "set_z_test", "get_z_test"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "z_test"), "set_z_test", "get_z_test"); ClassDB::bind_method(D_METHOD("get_a_test"), &MLPPOutputLayer::get_a_test); ClassDB::bind_method(D_METHOD("set_a_test", "val"), &MLPPOutputLayer::set_a_test); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "a_test", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "set_a_test", "get_a_test"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "a_test"), "set_a_test", "get_a_test"); ClassDB::bind_method(D_METHOD("get_delta"), &MLPPOutputLayer::get_delta); ClassDB::bind_method(D_METHOD("set_delta", "val"), &MLPPOutputLayer::set_delta); diff --git a/mlpp/output_layer/output_layer.h b/mlpp/output_layer/output_layer.h index 249a885..516f27f 100644 --- a/mlpp/output_layer/output_layer.h +++ b/mlpp/output_layer/output_layer.h @@ -49,11 +49,11 @@ public: Ref get_a(); void set_a(const Ref &val); - Ref get_z_test(); - void set_z_test(const Ref &val); + real_t get_z_test(); + void set_z_test(const real_t val); - Ref get_a_test(); - void set_a_test(const Ref &val); + real_t get_a_test(); + void set_a_test(const real_t val); Ref get_delta(); void set_delta(const Ref &val); @@ -76,7 +76,7 @@ public: void forward_pass(); void test(const Ref &x); - MLPPOutputLayer(int p_n_hidden, MLPPActivation::ActivationFunction p_activation, Ref p_input, MLPPUtilities::WeightDistributionType p_weight_init, MLPPReg::RegularizationType p_reg, real_t p_lambda, real_t p_alpha); + MLPPOutputLayer(int p_n_hidden, MLPPActivation::ActivationFunction p_activation, MLPPCost::CostTypes p_cost, Ref p_input, MLPPUtilities::WeightDistributionType p_weight_init, MLPPReg::RegularizationType p_reg, real_t p_lambda, real_t p_alpha); MLPPOutputLayer(); ~MLPPOutputLayer(); @@ -96,8 +96,8 @@ protected: Ref _z; Ref _a; - Ref _z_test; - Ref _a_test; + real_t _z_test; + real_t _a_test; Ref _delta; diff --git a/test/mlpp_tests.cpp b/test/mlpp_tests.cpp index 49557d0..f96a89e 100644 --- a/test/mlpp_tests.cpp +++ b/test/mlpp_tests.cpp @@ -626,18 +626,26 @@ void MLPPTests::test_dynamically_sized_ann(bool ui) { alg.printVector(ann_old.modelSetTest(alg.transpose(inputSet))); std::cout << "ACCURACY: " << 100 * ann_old.score() << "%" << std::endl; - MLPPANN ann(alg.transpose(inputSet), outputSet); - ann.add_layer(2, "Cosh"); - ann.add_output_layer("Sigmoid", "LogLoss"); + Ref input_set; + input_set.instance(); + input_set->set_from_std_vectors(inputSet); + + Ref output_set; + output_set.instance(); + output_set->set_from_std_vector(outputSet); + + MLPPANN ann(alg.transposem(input_set), output_set); + ann.add_layer(2, MLPPActivation::ACTIVATION_FUNCTION_COSH); + ann.add_output_layer(MLPPActivation::ACTIVATION_FUNCTION_SIGMOID, MLPPCost::COST_TYPE_LOGISTIC_LOSS); ann.amsgrad(0.1, 10000, 1, 0.9, 0.999, 0.000001, ui); ann.adadelta(1, 1000, 2, 0.9, 0.000001, ui); ann.momentum(0.1, 8000, 2, 0.9, true, ui); - ann.set_learning_rate_scheduler_drop("Step", 0.5, 1000); + ann.set_learning_rate_scheduler_drop(MLPPANN::SCHEDULER_TYPE_STEP, 0.5, 1000); ann.gradient_descent(0.01, 30000); - alg.printVector(ann.model_set_test(alg.transpose(inputSet))); - std::cout << "ACCURACY: " << 100 * ann.score() << "%" << std::endl; + PLOG_MSG(ann.model_set_test(alg.transposem(input_set))->to_string()); + PLOG_MSG("ACCURACY: " + String::num(100 * ann.score()) + "%"); } void MLPPTests::test_wgan_old(bool ui) { //MLPPStat stat; @@ -705,15 +713,23 @@ void MLPPTests::test_ann(bool ui) { alg.printVector(predictions_old); // Testing out the model's preds for train set. std::cout << "ACCURACY: " << 100 * ann_old.score() << "%" << std::endl; // Accuracy. - MLPPANN ann(inputSet, outputSet); - ann.add_layer(5, "Sigmoid"); - ann.add_layer(8, "Sigmoid"); // Add more layers as needed. - ann.add_output_layer("Sigmoid", "LogLoss"); + Ref input_set; + input_set.instance(); + input_set->set_from_std_vectors(inputSet); + + Ref output_set; + output_set.instance(); + output_set->set_from_std_vector(outputSet); + + MLPPANN ann(input_set, output_set); + ann.add_layer(5, MLPPActivation::ACTIVATION_FUNCTION_SIGMOID); + ann.add_layer(8, MLPPActivation::ACTIVATION_FUNCTION_SIGMOID); // Add more layers as needed. + ann.add_output_layer(MLPPActivation::ACTIVATION_FUNCTION_SIGMOID, MLPPCost::COST_TYPE_LOGISTIC_LOSS); ann.gradient_descent(1, 20000, ui); - std::vector predictions = ann.model_set_test(inputSet); - alg.printVector(predictions); // Testing out the model's preds for train set. - std::cout << "ACCURACY: " << 100 * ann.score() << "%" << std::endl; // Accuracy. + Ref predictions = ann.model_set_test(input_set); + PLOG_MSG(predictions->to_string()); // Testing out the model's preds for train set. + PLOG_MSG("ACCURACY: " + String::num(100 * ann.score()) + "%"); // Accuracy. } void MLPPTests::test_dynamically_sized_mann(bool ui) { MLPPLinAlg alg;