From 1b3606c7ae3229580955910f50ee8341964a3648 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 16 Feb 2023 22:51:23 +0100 Subject: [PATCH] Now MLPPAutoEncoder uses engine classes. --- mlpp/auto_encoder/auto_encoder.cpp | 248 ++++++++++++++++------------- mlpp/auto_encoder/auto_encoder.h | 52 +++--- test/mlpp_tests.cpp | 10 +- 3 files changed, 177 insertions(+), 133 deletions(-) diff --git a/mlpp/auto_encoder/auto_encoder.cpp b/mlpp/auto_encoder/auto_encoder.cpp index 666eb21..e51f955 100644 --- a/mlpp/auto_encoder/auto_encoder.cpp +++ b/mlpp/auto_encoder/auto_encoder.cpp @@ -11,15 +11,16 @@ #include "../lin_alg/lin_alg.h" #include "../utilities/utilities.h" +#include "core/log/logger.h" + #include //UDPATE Ref MLPPAutoEncoder::get_input_set() { - return Ref(); - //return _input_set; + return _input_set; } void MLPPAutoEncoder::set_input_set(const Ref &val) { - //_input_set = val; + _input_set = val; _initialized = false; } @@ -33,14 +34,14 @@ void MLPPAutoEncoder::set_n_hidden(const int val) { _initialized = false; } -std::vector> MLPPAutoEncoder::model_set_test(std::vector> X) { - ERR_FAIL_COND_V(!_initialized, std::vector>()); +Ref MLPPAutoEncoder::model_set_test(const Ref &X) { + ERR_FAIL_COND_V(!_initialized, Ref()); return evaluatem(X); } -std::vector MLPPAutoEncoder::model_test(std::vector x) { - ERR_FAIL_COND_V(!_initialized, std::vector()); +Ref MLPPAutoEncoder::model_test(const Ref &x) { + ERR_FAIL_COND_V(!_initialized, Ref()); return evaluatev(x); } @@ -59,39 +60,37 @@ void MLPPAutoEncoder::gradient_descent(real_t learning_rate, int max_epoch, bool cost_prev = cost(_y_hat, _input_set); // Calculating the errors - std::vector> error = alg.subtraction(_y_hat, _input_set); + Ref error = alg.subtractionm(_y_hat, _input_set); // Calculating the weight/bias gradients for layer 2 - std::vector> D2_1 = alg.matmult(alg.transpose(_a2), error); + Ref D2_1 = alg.matmultm(alg.transposem(_a2), error); // weights and bias updation for layer 2 - _weights2 = alg.subtraction(_weights2, alg.scalarMultiply(learning_rate / _n, D2_1)); + _weights2 = alg.subtractionm(_weights2, alg.scalar_multiplym(learning_rate / _n, D2_1)); // Calculating the bias gradients for layer 2 - _bias2 = alg.subtractMatrixRows(_bias2, alg.scalarMultiply(learning_rate, error)); + _bias2 = alg.subtract_matrix_rows(_bias2, alg.scalar_multiplym(learning_rate, error)); //Calculating the weight/bias for layer 1 - std::vector> D1_1 = alg.matmult(error, alg.transpose(_weights2)); - - std::vector> D1_2 = alg.hadamard_product(D1_1, avn.sigmoid(_z2, 1)); - - std::vector> D1_3 = alg.matmult(alg.transpose(_input_set), D1_2); + Ref D1_1 = alg.matmultm(error, alg.transposem(_weights2)); + Ref D1_2 = alg.hadamard_productm(D1_1, avn.sigmoid_derivm(_z2)); + Ref D1_3 = alg.matmultm(alg.transposem(_input_set), D1_2); // weight an bias updation for layer 1 - _weights1 = alg.subtraction(_weights1, alg.scalarMultiply(learning_rate / _n, D1_3)); + _weights1 = alg.subtractionm(_weights1, alg.scalar_multiplym(learning_rate / _n, D1_3)); - _bias1 = alg.subtractMatrixRows(_bias1, alg.scalarMultiply(learning_rate / _n, D1_2)); + _bias1 = alg.subtract_matrix_rows(_bias1, alg.scalar_multiplym(learning_rate / _n, D1_2)); forward_pass(); // UI PORTION if (ui) { - MLPPUtilities::CostInfo(epoch, cost_prev, cost(_y_hat, _input_set)); - std::cout << "Layer 1:" << std::endl; - MLPPUtilities::UI(_weights1, _bias1); - std::cout << "Layer 2:" << std::endl; - MLPPUtilities::UI(_weights2, _bias2); + MLPPUtilities::cost_info(epoch, cost_prev, cost(_y_hat, _input_set)); + PLOG_MSG("Layer 1:"); + MLPPUtilities::print_ui_mb(_weights1, _bias1); + PLOG_MSG("Layer 2:"); + MLPPUtilities::print_ui_mb(_weights2, _bias2); } epoch++; @@ -110,45 +109,62 @@ void MLPPAutoEncoder::sgd(real_t learning_rate, int max_epoch, bool ui) { real_t cost_prev = 0; int epoch = 1; + 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 input_set_mat_tmp; + input_set_mat_tmp.instance(); + input_set_mat_tmp->resize(Size2i(_input_set->size().x, 1)); + + Ref y_hat_mat_tmp; + y_hat_mat_tmp.instance(); + y_hat_mat_tmp->resize(Size2i(_bias2->size(), 1)); + while (true) { - 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 = evaluatev(_input_set[outputIndex]); - auto prop_res = propagatev(_input_set[outputIndex]); - auto z2 = std::get<0>(prop_res); - auto a2 = std::get<1>(prop_res); + _input_set->get_row_into_mlpp_vector(output_index, input_set_row_tmp); + input_set_mat_tmp->set_row_mlpp_vector(0, input_set_row_tmp); - cost_prev = cost({ y_hat }, { _input_set[outputIndex] }); - std::vector error = alg.subtraction(y_hat, _input_set[outputIndex]); + Ref y_hat = evaluatev(input_set_row_tmp); + y_hat_mat_tmp->set_row_mlpp_vector(0, y_hat); + + PropagateVResult prop_res = propagatev(input_set_row_tmp); + + cost_prev = cost(y_hat_mat_tmp, input_set_mat_tmp); + Ref error = alg.subtractionnv(y_hat, input_set_row_tmp); // Weight updation for layer 2 - std::vector> D2_1 = alg.outerProduct(error, a2); - _weights2 = alg.subtraction(_weights2, alg.scalarMultiply(learning_rate, alg.transpose(D2_1))); + Ref D2_1 = alg.outer_product(error, prop_res.a2); + _weights2 = alg.subtractionm(_weights2, alg.scalar_multiplym(learning_rate, alg.transposem(D2_1))); // Bias updation for layer 2 - _bias2 = alg.subtraction(_bias2, alg.scalarMultiply(learning_rate, error)); + _bias2 = alg.subtractionnv(_bias2, alg.scalar_multiplynv(learning_rate, error)); // Weight updation for layer 1 - std::vector D1_1 = alg.mat_vec_mult(_weights2, error); - std::vector D1_2 = alg.hadamard_product(D1_1, avn.sigmoid(z2, 1)); - std::vector> D1_3 = alg.outerProduct(_input_set[outputIndex], D1_2); + Ref D1_1 = alg.mat_vec_multv(_weights2, error); + Ref D1_2 = alg.hadamard_productnv(D1_1, avn.sigmoid_derivv(prop_res.z2)); + Ref D1_3 = alg.outer_product(input_set_row_tmp, D1_2); - _weights1 = alg.subtraction(_weights1, alg.scalarMultiply(learning_rate, D1_3)); + _weights1 = alg.subtractionm(_weights1, alg.scalar_multiplym(learning_rate, D1_3)); // Bias updation for layer 1 - _bias1 = alg.subtraction(_bias1, alg.scalarMultiply(learning_rate, D1_2)); + _bias1 = alg.subtractionnv(_bias1, alg.scalar_multiplynv(learning_rate, D1_2)); - y_hat = evaluatev(_input_set[outputIndex]); + y_hat = evaluatev(input_set_row_tmp); if (ui) { - MLPPUtilities::CostInfo(epoch, cost_prev, cost({ y_hat }, { _input_set[outputIndex] })); - std::cout << "Layer 1:" << std::endl; - MLPPUtilities::UI(_weights1, _bias1); - std::cout << "Layer 2:" << std::endl; - MLPPUtilities::UI(_weights2, _bias2); + MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat_mat_tmp, input_set_mat_tmp)); + + PLOG_MSG("Layer 1:"); + MLPPUtilities::print_ui_mb(_weights1, _bias1); + PLOG_MSG("Layer 2:"); + MLPPUtilities::print_ui_mb(_weights2, _bias2); } epoch++; @@ -171,57 +187,54 @@ void MLPPAutoEncoder::mbgd(real_t learning_rate, int max_epoch, int mini_batch_s // Creating the mini-batches int n_mini_batch = _n / mini_batch_size; - std::vector>> inputMiniBatches = MLPPUtilities::createMiniBatches(_input_set, n_mini_batch); + Vector> batches = MLPPUtilities::create_mini_batchesm(_input_set, n_mini_batch); while (true) { for (int i = 0; i < n_mini_batch; i++) { - std::vector> y_hat = evaluatem(inputMiniBatches[i]); + Ref current_batch = batches[i]; - auto prop_res = propagatem(inputMiniBatches[i]); - auto z2 = std::get<0>(prop_res); - auto a2 = std::get<1>(prop_res); + Ref y_hat = evaluatem(current_batch); - cost_prev = cost(y_hat, inputMiniBatches[i]); + PropagateMResult prop_res = propagatem(current_batch); + + cost_prev = cost(y_hat, current_batch); // Calculating the errors - std::vector> error = alg.subtraction(y_hat, inputMiniBatches[i]); + Ref error = alg.subtractionm(y_hat, current_batch); // Calculating the weight/bias gradients for layer 2 - std::vector> D2_1 = alg.matmult(alg.transpose(a2), error); + Ref D2_1 = alg.matmultm(alg.transposem(prop_res.a2), error); // weights and bias updation for layer 2 - _weights2 = alg.subtraction(_weights2, alg.scalarMultiply(learning_rate / inputMiniBatches[i].size(), D2_1)); + _weights2 = alg.subtractionm(_weights2, alg.scalar_multiplym(learning_rate / current_batch->size().y, D2_1)); // Bias Updation for layer 2 - _bias2 = alg.subtractMatrixRows(_bias2, alg.scalarMultiply(learning_rate, error)); + _bias2 = alg.subtract_matrix_rows(_bias2, alg.scalar_multiplym(learning_rate, error)); //Calculating the weight/bias for layer 1 - std::vector> D1_1 = alg.matmult(error, alg.transpose(_weights2)); - - std::vector> D1_2 = alg.hadamard_product(D1_1, avn.sigmoid(z2, true)); - - std::vector> D1_3 = alg.matmult(alg.transpose(inputMiniBatches[i]), D1_2); + Ref D1_1 = alg.matmultm(error, alg.transposem(_weights2)); + Ref D1_2 = alg.hadamard_productm(D1_1, avn.sigmoid_derivm(prop_res.z2)); + Ref D1_3 = alg.matmultm(alg.transposem(current_batch), D1_2); // weight an bias updation for layer 1 - _weights1 = alg.subtraction(_weights1, alg.scalarMultiply(learning_rate / inputMiniBatches[i].size(), D1_3)); + _weights1 = alg.subtractionm(_weights1, alg.scalar_multiplym(learning_rate / current_batch->size().x, D1_3)); + _bias1 = alg.subtract_matrix_rows(_bias1, alg.scalar_multiplym(learning_rate / current_batch->size().x, D1_2)); - _bias1 = alg.subtractMatrixRows(_bias1, alg.scalarMultiply(learning_rate / inputMiniBatches[i].size(), D1_2)); - - y_hat = evaluatem(inputMiniBatches[i]); + y_hat = evaluatem(current_batch); if (ui) { - MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, inputMiniBatches[i])); - std::cout << "Layer 1:" << std::endl; - MLPPUtilities::UI(_weights1, _bias1); - std::cout << "Layer 2:" << std::endl; - MLPPUtilities::UI(_weights2, _bias2); + MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, current_batch)); + PLOG_MSG("Layer 1:"); + MLPPUtilities::print_ui_mb(_weights1, _bias1); + PLOG_MSG("Layer 2:"); + MLPPUtilities::print_ui_mb(_weights2, _bias2); } } epoch++; - + if (epoch > max_epoch) { break; } @@ -234,30 +247,43 @@ real_t MLPPAutoEncoder::score() { ERR_FAIL_COND_V(!_initialized, 0); MLPPUtilities util; - return util.performance(_y_hat, _input_set); + return util.performance_mat(_y_hat, _input_set); } -void MLPPAutoEncoder::save(std::string fileName) { +void MLPPAutoEncoder::save(const String &file_name) { ERR_FAIL_COND(!_initialized); - MLPPUtilities util; - util.saveParameters(fileName, _weights1, _bias1, false, 1); - util.saveParameters(fileName, _weights2, _bias2, true, 2); + //MLPPUtilities util; + //util.saveParameters(fileName, _weights1, _bias1, false, 1); + //util.saveParameters(fileName, _weights2, _bias2, true, 2); } -MLPPAutoEncoder::MLPPAutoEncoder(std::vector> p_input_set, int pn_hidden) { +MLPPAutoEncoder::MLPPAutoEncoder(const Ref &p_input_set, int p_n_hidden) { _input_set = p_input_set; - _n_hidden = pn_hidden; - _n = _input_set.size(); - _k = _input_set[0].size(); + _n_hidden = p_n_hidden; + _n = _input_set->size().y; + _k = _input_set->size().x; - MLPPActivation avn; - _y_hat.resize(_input_set.size()); + _y_hat.instance(); + _y_hat->resize(_input_set->size()); - _weights1 = MLPPUtilities::weightInitialization(_k, _n_hidden); - _weights2 = MLPPUtilities::weightInitialization(_n_hidden, _k); - _bias1 = MLPPUtilities::biasInitialization(_n_hidden); - _bias2 = MLPPUtilities::biasInitialization(_k); + MLPPUtilities utilities; + + _weights1.instance(); + _weights1->resize(Size2i(_n_hidden, _k)); + utilities.weight_initializationm(_weights1); + + _weights2.instance(); + _weights2->resize(Size2i(_k, _n_hidden)); + utilities.weight_initializationm(_weights2); + + _bias1.instance(); + _bias1->resize(_n_hidden); + utilities.bias_initializationv(_bias1); + + _bias2.instance(); + _bias2->resize(_k); + utilities.bias_initializationv(_bias2); _initialized = true; } @@ -268,59 +294,63 @@ MLPPAutoEncoder::MLPPAutoEncoder() { MLPPAutoEncoder::~MLPPAutoEncoder() { } -real_t MLPPAutoEncoder::cost(std::vector> y_hat, std::vector> y) { - class MLPPCost cost; +real_t MLPPAutoEncoder::cost(const Ref &y_hat, const Ref &y) { + MLPPCost mlpp_cost; - return cost.MSE(y_hat, _input_set); + return mlpp_cost.msem(y_hat, _input_set); } -std::vector MLPPAutoEncoder::evaluatev(std::vector x) { +Ref MLPPAutoEncoder::evaluatev(const Ref &x) { MLPPLinAlg alg; MLPPActivation avn; - std::vector z2 = alg.addition(alg.mat_vec_mult(alg.transpose(_weights1), x), _bias1); - std::vector a2 = avn.sigmoid(z2); + Ref z2 = alg.additionnv(alg.mat_vec_multv(alg.transposem(_weights1), x), _bias1); + Ref a2 = avn.sigmoid_normv(z2); - return alg.addition(alg.mat_vec_mult(alg.transpose(_weights2), a2), _bias2); + return alg.additionnv(alg.mat_vec_multv(alg.transposem(_weights2), a2), _bias2); } -std::tuple, std::vector> MLPPAutoEncoder::propagatev(std::vector x) { +MLPPAutoEncoder::PropagateVResult MLPPAutoEncoder::propagatev(const Ref &x) { MLPPLinAlg alg; MLPPActivation avn; - std::vector z2 = alg.addition(alg.mat_vec_mult(alg.transpose(_weights1), x), _bias1); - std::vector a2 = avn.sigmoid(z2); + PropagateVResult res; - return { z2, a2 }; + res.z2 = alg.additionnv(alg.mat_vec_multv(alg.transposem(_weights1), x), _bias1); + res.a2 = avn.sigmoid_normv(res.z2); + + return res; } -std::vector> MLPPAutoEncoder::evaluatem(std::vector> X) { +Ref MLPPAutoEncoder::evaluatem(const Ref &X) { MLPPLinAlg alg; MLPPActivation avn; - std::vector> z2 = alg.mat_vec_add(alg.matmult(X, _weights1), _bias1); - std::vector> a2 = avn.sigmoid(z2); + Ref z2 = alg.mat_vec_addv(alg.matmultm(X, _weights1), _bias1); + Ref a2 = avn.sigmoid_normm(z2); - return alg.mat_vec_add(alg.matmult(a2, _weights2), _bias2); + return alg.mat_vec_addv(alg.matmultm(a2, _weights2), _bias2); } -std::tuple>, std::vector>> MLPPAutoEncoder::propagatem(std::vector> X) { +MLPPAutoEncoder::PropagateMResult MLPPAutoEncoder::propagatem(const Ref &X) { MLPPLinAlg alg; MLPPActivation avn; - std::vector> z2 = alg.mat_vec_add(alg.matmult(X, _weights1), _bias1); - std::vector> a2 = avn.sigmoid(z2); + PropagateMResult res; - return { z2, a2 }; + res.z2 = alg.mat_vec_addv(alg.matmultm(X, _weights1), _bias1); + res.a2 = avn.sigmoid_normm(res.z2); + + return res; } void MLPPAutoEncoder::forward_pass() { MLPPLinAlg alg; MLPPActivation avn; - _z2 = alg.mat_vec_add(alg.matmult(_input_set, _weights1), _bias1); - _a2 = avn.sigmoid(_z2); - _y_hat = alg.mat_vec_add(alg.matmult(_a2, _weights2), _bias2); + _z2 = alg.mat_vec_addv(alg.matmultm(_input_set, _weights1), _bias1); + _a2 = avn.sigmoid_normm(_z2); + _y_hat = alg.mat_vec_addv(alg.matmultm(_a2, _weights2), _bias2); } void MLPPAutoEncoder::_bind_methods() { diff --git a/mlpp/auto_encoder/auto_encoder.h b/mlpp/auto_encoder/auto_encoder.h index 0592ae7..22b0280 100644 --- a/mlpp/auto_encoder/auto_encoder.h +++ b/mlpp/auto_encoder/auto_encoder.h @@ -17,10 +17,8 @@ #include "../regularization/reg.h" -//REMOVE -#include -#include -#include +#include "../lin_alg/mlpp_matrix.h" +#include "../lin_alg/mlpp_vector.h" class MLPPAutoEncoder : public Reference { GDCLASS(MLPPAutoEncoder, Reference); @@ -32,8 +30,8 @@ public: int get_n_hidden(); void set_n_hidden(const int val); - std::vector> model_set_test(std::vector> X); - std::vector model_test(std::vector x); + Ref model_set_test(const Ref &X); + Ref 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); @@ -41,37 +39,49 @@ public: real_t score(); - void save(std::string fileName); + void save(const String &file_name); - MLPPAutoEncoder(std::vector> inputSet, int n_hidden); + MLPPAutoEncoder(const Ref &p_input_set, int p_n_hidden); MLPPAutoEncoder(); ~MLPPAutoEncoder(); protected: - real_t cost(std::vector> y_hat, std::vector> y); + real_t cost(const Ref &y_hat, const Ref &y); - std::vector evaluatev(std::vector x); - std::tuple, std::vector> propagatev(std::vector x); + Ref evaluatev(const Ref &x); - std::vector> evaluatem(std::vector> X); - std::tuple>, std::vector>> propagatem(std::vector> X); + struct PropagateVResult { + Ref z2; + Ref a2; + }; + + PropagateVResult propagatev(const Ref &x); + + Ref evaluatem(const Ref &X); + + struct PropagateMResult { + Ref z2; + Ref a2; + }; + + PropagateMResult propagatem(const Ref &X); void forward_pass(); static void _bind_methods(); - std::vector> _input_set; - std::vector> _y_hat; + Ref _input_set; + Ref _y_hat; - std::vector> _weights1; - std::vector> _weights2; + Ref _weights1; + Ref _weights2; - std::vector _bias1; - std::vector _bias2; + Ref _bias1; + Ref _bias2; - std::vector> _z2; - std::vector> _a2; + Ref _z2; + Ref _a2; int _n; int _k; diff --git a/test/mlpp_tests.cpp b/test/mlpp_tests.cpp index 1bb1e97..49557d0 100644 --- a/test/mlpp_tests.cpp +++ b/test/mlpp_tests.cpp @@ -594,10 +594,14 @@ void MLPPTests::test_autoencoder(bool ui) { alg.printMatrix(model_old.modelSetTest(alg.transpose(inputSet))); std::cout << "ACCURACY (Old): " << 100 * model_old.score() << "%" << std::endl; - MLPPAutoEncoder model(alg.transpose(inputSet), 5); + Ref input_set; + input_set.instance(); + input_set->set_from_std_vectors(inputSet); + + MLPPAutoEncoder model(alg.transposem(input_set), 5); model.sgd(0.001, 300000, ui); - alg.printMatrix(model.model_set_test(alg.transpose(inputSet))); - std::cout << "ACCURACY: " << 100 * model.score() << "%" << std::endl; + PLOG_MSG(model.model_set_test(alg.transposem(input_set))->to_string()); + PLOG_MSG("ACCURACY: " + String::num(100 * model.score()) + "%"); } void MLPPTests::test_dynamically_sized_ann(bool ui) { MLPPLinAlg alg;