From 741475a4ab5d787d22d8ffd66802e44935969f22 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 16 Feb 2023 19:56:18 +0100 Subject: [PATCH] Now MLPPCLogLogReg uses engine classes. --- mlpp/c_log_log_reg/c_log_log_reg.cpp | 152 ++++++++++++++++----------- mlpp/c_log_log_reg/c_log_log_reg.h | 34 +++--- test/mlpp_tests.cpp | 14 ++- 3 files changed, 121 insertions(+), 79 deletions(-) diff --git a/mlpp/c_log_log_reg/c_log_log_reg.cpp b/mlpp/c_log_log_reg/c_log_log_reg.cpp index c932279..de6aeb3 100644 --- a/mlpp/c_log_log_reg/c_log_log_reg.cpp +++ b/mlpp/c_log_log_reg/c_log_log_reg.cpp @@ -11,14 +11,13 @@ #include "../regularization/reg.h" #include "../utilities/utilities.h" -#include #include -std::vector MLPPCLogLogReg::model_set_test(std::vector> X) { +Ref MLPPCLogLogReg::model_set_test(const Ref &X) { return evaluatem(X); } -real_t MLPPCLogLogReg::model_test(std::vector x) { +real_t MLPPCLogLogReg::model_test(const Ref &x) { return evaluatev(x); } @@ -35,20 +34,20 @@ void MLPPCLogLogReg::gradient_descent(real_t learning_rate, int max_epoch, bool while (true) { cost_prev = cost(_y_hat, _output_set); - std::vector error = alg.subtraction(_y_hat, _output_set); + Ref error = alg.subtractionnv(_y_hat, _output_set); // Calculating the weight gradients - _weights = alg.subtraction(_weights, alg.scalarMultiply(learning_rate / _n, alg.mat_vec_mult(alg.transpose(_input_set), alg.hadamard_product(error, avn.cloglog(_z, true))))); - _weights = regularization.regWeights(_weights, _lambda, _alpha, _reg); + _weights = alg.subtractionnv(_weights, alg.scalar_multiplynv(learning_rate / _n, alg.mat_vec_multv(alg.transposem(_input_set), alg.hadamard_productnv(error, avn.cloglog_derivv(_z))))); + _weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg); // Calculating the bias gradients - bias -= learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(_z, true))) / _n; + bias -= learning_rate * alg.sum_elementsv(alg.hadamard_productnv(error, avn.cloglog_derivv(_z))) / _n; forward_pass(); if (ui) { - MLPPUtilities::CostInfo(epoch, cost_prev, cost(_y_hat, _output_set)); - MLPPUtilities::UI(_weights, bias); + MLPPUtilities::cost_info(epoch, cost_prev, cost(_y_hat, _output_set)); + MLPPUtilities::print_ui_vb(_weights, bias); } epoch++; @@ -72,19 +71,19 @@ void MLPPCLogLogReg::mle(real_t learning_rate, int max_epoch, bool ui) { while (true) { cost_prev = cost(_y_hat, _output_set); - std::vector error = alg.subtraction(_y_hat, _output_set); + Ref error = alg.subtractionnv(_y_hat, _output_set); - _weights = alg.addition(_weights, alg.scalarMultiply(learning_rate / _n, alg.mat_vec_mult(alg.transpose(_input_set), alg.hadamard_product(error, avn.cloglog(_z, true))))); - _weights = regularization.regWeights(_weights, _lambda, _alpha, _reg); + _weights = alg.additionnv(_weights, alg.scalar_multiplynv(learning_rate / _n, alg.mat_vec_multv(alg.transposem(_input_set), alg.hadamard_productnv(error, avn.cloglog_derivv(_z))))); + _weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg); // Calculating the bias gradients - bias += learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(_z, true))) / _n; + bias += learning_rate * alg.sum_elementsv(alg.hadamard_productnv(error, avn.cloglog_derivv(_z))) / _n; forward_pass(); if (ui) { - MLPPUtilities::CostInfo(epoch, cost_prev, cost(_y_hat, _output_set)); - MLPPUtilities::UI(_weights, bias); + MLPPUtilities::cost_info(epoch, cost_prev, cost(_y_hat, _output_set)); + MLPPUtilities::print_ui_vb(_weights, bias); } epoch++; @@ -102,32 +101,52 @@ void MLPPCLogLogReg::sgd(real_t learning_rate, int max_epoch, bool p_) { 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)); + forward_pass(); + 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) { - 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); - real_t y_hat = evaluatev(_input_set[outputIndex]); - real_t z = propagatev(_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); - real_t error = y_hat - _output_set[outputIndex]; + real_t y_hat = evaluatev(input_set_row_tmp); + y_hat_row_tmp->set_element(0, y_hat); + + real_t z = propagatev(input_set_row_tmp); + + cost_prev = cost(y_hat_row_tmp, output_set_row_tmp); + + real_t error = y_hat - output_set_element; // Weight Updation - _weights = alg.subtraction(_weights, alg.scalarMultiply(learning_rate * error * exp(z - exp(z)), _input_set[outputIndex])); - _weights = regularization.regWeights(_weights, _lambda, _alpha, _reg); + _weights = alg.subtractionnv(_weights, alg.scalar_multiplynv(learning_rate * error * Math::exp(z - Math::exp(z)), input_set_row_tmp)); + _weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg); // Bias updation bias -= learning_rate * error * exp(z - exp(z)); - y_hat = evaluatev(_input_set[outputIndex]); + y_hat = evaluatev(input_set_row_tmp); if (p_) { - MLPPUtilities::CostInfo(epoch, cost_prev, cost({ y_hat }, { _output_set[outputIndex] })); - MLPPUtilities::UI(_weights, bias); + MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat_row_tmp, output_set_row_tmp)); + MLPPUtilities::print_ui_vb(_weights, bias); } epoch++; @@ -149,32 +168,33 @@ void MLPPCLogLogReg::mbgd(real_t learning_rate, int max_epoch, int mini_batch_si // Creating the mini-batches int n_mini_batch = _n / mini_batch_size; - auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch); - auto inputMiniBatches = std::get<0>(batches); - auto outputMiniBatches = std::get<1>(batches); + MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch); while (true) { for (int i = 0; i < n_mini_batch; i++) { - std::vector y_hat = evaluatem(inputMiniBatches[i]); - std::vector z = propagatem(inputMiniBatches[i]); - cost_prev = cost(y_hat, outputMiniBatches[i]); + Ref current_input_batch = batches.input_sets[i]; + Ref current_output_batch = batches.output_sets[i]; - std::vector error = alg.subtraction(y_hat, outputMiniBatches[i]); + Ref y_hat = evaluatem(current_input_batch); + Ref z = propagatem(current_input_batch); + cost_prev = cost(y_hat, current_output_batch); + + Ref error = alg.subtractionnv(y_hat, current_output_batch); // Calculating the weight gradients - _weights = alg.subtraction(_weights, alg.scalarMultiply(learning_rate / _n, alg.mat_vec_mult(alg.transpose(inputMiniBatches[i]), alg.hadamard_product(error, avn.cloglog(z, 1))))); - _weights = regularization.regWeights(_weights, _lambda, _alpha, _reg); + _weights = alg.subtractionnv(_weights, alg.scalar_multiplynv(learning_rate / _n, alg.mat_vec_multv(alg.transposem(current_input_batch), alg.hadamard_productnv(error, avn.cloglog_derivv(z))))); + _weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg); // Calculating the bias gradients - bias -= learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / _n; + bias -= learning_rate * alg.sum_elementsv(alg.hadamard_productnv(error, avn.cloglog_derivv(z))) / _n; forward_pass(); - y_hat = evaluatem(inputMiniBatches[i]); + y_hat = evaluatem(current_input_batch); if (p_) { - MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, outputMiniBatches[i])); - MLPPUtilities::UI(_weights, bias); + MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, current_output_batch)); + MLPPUtilities::print_ui_vb(_weights, bias); } } @@ -190,22 +210,29 @@ void MLPPCLogLogReg::mbgd(real_t learning_rate, int max_epoch, int mini_batch_si real_t MLPPCLogLogReg::score() { MLPPUtilities util; - return util.performance(_y_hat, _output_set); + + return util.performance_vec(_y_hat, _output_set); } -MLPPCLogLogReg::MLPPCLogLogReg(std::vector> p_input_set, std::vector p_output_set, std::string p_reg, real_t p_lambda, real_t p_alpha) { +MLPPCLogLogReg::MLPPCLogLogReg(const Ref &p_input_set, const Ref &p_output_set, MLPPReg::RegularizationType p_reg, real_t p_lambda, real_t p_alpha) { _input_set = p_input_set; _output_set = p_output_set; - _n = _input_set.size(); - _k = _input_set[0].size(); + _n = _input_set->size().y; + _k = _input_set->size().x; _reg = p_reg; _lambda = p_lambda; _alpha = p_alpha; - _y_hat.resize(_n); + _y_hat.instance(); + _y_hat->resize(_n); - _weights = MLPPUtilities::weightInitialization(_k); - bias = MLPPUtilities::biasInitialization(); + MLPPUtilities utilities; + + _weights.instance(); + _weights->resize(_k); + utilities.weight_initializationv(_weights); + + bias = utilities.bias_initializationr(); } MLPPCLogLogReg::MLPPCLogLogReg() { @@ -213,32 +240,37 @@ MLPPCLogLogReg::MLPPCLogLogReg() { MLPPCLogLogReg::~MLPPCLogLogReg() { } -real_t MLPPCLogLogReg::cost(std::vector y_hat, std::vector y) { +real_t MLPPCLogLogReg::cost(const Ref &y_hat, const Ref &y) { MLPPReg regularization; - class MLPPCost cost; - return cost.MSE(y_hat, y) + regularization.regTerm(_weights, _lambda, _alpha, _reg); + MLPPCost mlpp_cost; + + return mlpp_cost.msev(y_hat, y) + regularization.reg_termv(_weights, _lambda, _alpha, _reg); } -real_t MLPPCLogLogReg::evaluatev(std::vector x) { +real_t MLPPCLogLogReg::evaluatev(const Ref &x) { MLPPLinAlg alg; MLPPActivation avn; - return avn.cloglog(alg.dot(_weights, x) + bias); + + return avn.cloglog_normr(alg.dotv(_weights, x) + bias); } -real_t MLPPCLogLogReg::propagatev(std::vector x) { +real_t MLPPCLogLogReg::propagatev(const Ref &x) { MLPPLinAlg alg; - return alg.dot(_weights, x) + bias; + + return alg.dotv(_weights, x) + bias; } -std::vector MLPPCLogLogReg::evaluatem(std::vector> X) { +Ref MLPPCLogLogReg::evaluatem(const Ref &X) { MLPPLinAlg alg; MLPPActivation avn; - return avn.cloglog(alg.scalarAdd(bias, alg.mat_vec_mult(X, _weights))); + + return avn.cloglog_normv(alg.scalar_addnv(bias, alg.mat_vec_multv(X, _weights))); } -std::vector MLPPCLogLogReg::propagatem(std::vector> X) { +Ref MLPPCLogLogReg::propagatem(const Ref &X) { MLPPLinAlg alg; - return alg.scalarAdd(bias, alg.mat_vec_mult(X, _weights)); + + return alg.scalar_addnv(bias, alg.mat_vec_multv(X, _weights)); } // cloglog ( wTx + b ) @@ -246,7 +278,7 @@ void MLPPCLogLogReg::forward_pass() { MLPPActivation avn; _z = propagatem(_input_set); - _y_hat = avn.cloglog(_z); + _y_hat = avn.cloglog_normv(_z); } void MLPPCLogLogReg::_bind_methods() { diff --git a/mlpp/c_log_log_reg/c_log_log_reg.h b/mlpp/c_log_log_reg/c_log_log_reg.h index 57f547c..a0e8a82 100644 --- a/mlpp/c_log_log_reg/c_log_log_reg.h +++ b/mlpp/c_log_log_reg/c_log_log_reg.h @@ -12,15 +12,17 @@ #include "core/object/reference.h" -#include -#include +#include "../regularization/reg.h" + +#include "../lin_alg/mlpp_matrix.h" +#include "../lin_alg/mlpp_vector.h" class MLPPCLogLogReg : public Reference { GDCLASS(MLPPCLogLogReg, Reference); public: - std::vector model_set_test(std::vector> X); - real_t model_test(std::vector x); + 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 mle(real_t learning_rate, int max_epoch, bool ui = false); @@ -29,7 +31,7 @@ public: real_t score(); - MLPPCLogLogReg(std::vector> p_input_set, std::vector p_output_set, std::string p_reg = "None", real_t p_lambda = 0.5, real_t p_alpha = 0.5); + MLPPCLogLogReg(const Ref &p_input_set, const Ref &p_output_set, MLPPReg::RegularizationType p_reg = MLPPReg::REGULARIZATION_TYPE_NONE, real_t p_lambda = 0.5, real_t p_alpha = 0.5); MLPPCLogLogReg(); ~MLPPCLogLogReg(); @@ -38,30 +40,30 @@ protected: void weight_initialization(int k); void bias_initialization(); - real_t cost(std::vector y_hat, std::vector y); + real_t cost(const Ref &y_hat, const Ref &y); - real_t evaluatev(std::vector x); - real_t propagatev(std::vector x); + real_t evaluatev(const Ref &x); + real_t propagatev(const Ref &x); - std::vector evaluatem(std::vector> X); - std::vector propagatem(std::vector> X); + Ref evaluatem(const Ref &X); + Ref propagatem(const Ref &X); void forward_pass(); static void _bind_methods(); - std::vector> _input_set; - std::vector _output_set; - std::vector _y_hat; - std::vector _z; - std::vector _weights; + Ref _input_set; + Ref _output_set; + Ref _y_hat; + Ref _z; + Ref _weights; real_t bias; int _n; int _k; // Regularization Params - std::string _reg; + MLPPReg::RegularizationType _reg; real_t _lambda; real_t _alpha; /* This is the controlling param for Elastic Net*/ }; diff --git a/test/mlpp_tests.cpp b/test/mlpp_tests.cpp index 8068204..2cac3ae 100644 --- a/test/mlpp_tests.cpp +++ b/test/mlpp_tests.cpp @@ -430,10 +430,18 @@ void MLPPTests::test_c_log_log_regression(bool ui) { alg.printVector(model_old.modelSetTest(alg.transpose(inputSet))); std::cout << "ACCURACY: " << 100 * model_old.score() << "%" << std::endl; - MLPPCLogLogReg model(alg.transpose(inputSet), outputSet); + Ref input_set; + input_set.instance(); + input_set->set_from_std_vectors(alg.transpose(inputSet)); + + Ref output_set; + output_set.instance(); + output_set->set_from_std_vector(outputSet); + + MLPPCLogLogReg model(alg.transposem(input_set), output_set); model.sgd(0.1, 10000, ui); - alg.printVector(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_exp_reg_regression(bool ui) { MLPPLinAlg alg;