Now MLPPExpReg uses engine classes.

This commit is contained in:
Relintai 2023-02-16 18:43:35 +01:00
parent 737b34f53d
commit 17486baae9
3 changed files with 129 additions and 83 deletions

View File

@ -14,11 +14,11 @@
#include <iostream> #include <iostream>
#include <random> #include <random>
std::vector<real_t> MLPPExpReg::model_set_test(std::vector<std::vector<real_t>> X) { Ref<MLPPVector> MLPPExpReg::model_set_test(const Ref<MLPPMatrix> &X) {
return evaluatem(X); return evaluatem(X);
} }
real_t MLPPExpReg::model_test(std::vector<real_t> x) { real_t MLPPExpReg::model_test(const Ref<MLPPVector> &x) {
return evaluatev(x); return evaluatev(x);
} }
@ -34,35 +34,35 @@ void MLPPExpReg::gradient_descent(real_t learning_rate, int max_epoch, bool ui)
while (true) { while (true) {
cost_prev = cost(_y_hat, _output_set); cost_prev = cost(_y_hat, _output_set);
std::vector<real_t> error = alg.subtraction(_y_hat, _output_set); Ref<MLPPVector> error = alg.subtractionnv(_y_hat, _output_set);
for (int i = 0; i < _k; i++) { for (int i = 0; i < _k; i++) {
// Calculating the weight gradient // Calculating the weight gradient
real_t sum = 0; real_t sum = 0;
for (int j = 0; j < _n; j++) { for (int j = 0; j < _n; j++) {
sum += error[j] * _input_set[j][i] * std::pow(_weights[i], _input_set[j][i] - 1); sum += error->get_element(j) * _input_set->get_element(j, i) * Math::pow(_weights->get_element(i), _input_set->get_element(j, i) - 1);
} }
real_t w_gradient = sum / _n; real_t w_gradient = sum / _n;
// Calculating the initial gradient // Calculating the initial gradient
real_t sum2 = 0; real_t sum2 = 0;
for (int j = 0; j < _n; j++) { for (int j = 0; j < _n; j++) {
sum2 += error[j] * std::pow(_weights[i], _input_set[j][i]); sum2 += error->get_element(j) * Math::pow(_weights->get_element(i), _input_set->get_element(j, i));
} }
real_t i_gradient = sum2 / _n; real_t i_gradient = sum2 / _n;
// Weight/initial updation // Weight/initial updation
_weights[i] -= learning_rate * w_gradient; _weights->set_element(i, _weights->get_element(i) - learning_rate * w_gradient);
_initial[i] -= learning_rate * i_gradient; _initial->set_element(i, _initial->get_element(i) - learning_rate * i_gradient);
} }
_weights = regularization.regWeights(_weights, _lambda, _alpha, _reg); _weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
// Calculating the bias gradient // Calculating the bias gradient
real_t sum = 0; real_t sum = 0;
for (int j = 0; j < _n; j++) { for (int j = 0; j < _n; j++) {
sum += (_y_hat[j] - _output_set[j]); sum += (_y_hat->get_element(j) - _output_set->get_element(j));
} }
real_t b_gradient = sum / _n; real_t b_gradient = sum / _n;
@ -72,8 +72,8 @@ void MLPPExpReg::gradient_descent(real_t learning_rate, int max_epoch, bool ui)
forward_pass(); forward_pass();
if (ui) { if (ui) {
MLPPUtilities::CostInfo(epoch, cost_prev, cost(_y_hat, _output_set)); MLPPUtilities::cost_info(epoch, cost_prev, cost(_y_hat, _output_set));
MLPPUtilities::UI(_weights, _bias); MLPPUtilities::print_ui_vb(_weights, _bias);
} }
epoch++; epoch++;
@ -94,35 +94,53 @@ void MLPPExpReg::sgd(real_t learning_rate, int max_epoch, bool ui) {
std::default_random_engine generator(rd()); std::default_random_engine generator(rd());
std::uniform_int_distribution<int> distribution(0, int(_n - 1)); std::uniform_int_distribution<int> distribution(0, int(_n - 1));
Ref<MLPPVector> input_set_row_tmp;
input_set_row_tmp.instance();
input_set_row_tmp->resize(_input_set->size().x);
Ref<MLPPVector> y_hat_row_tmp;
y_hat_row_tmp.instance();
y_hat_row_tmp->resize(1);
Ref<MLPPVector> output_set_row_tmp;
output_set_row_tmp.instance();
output_set_row_tmp->resize(1);
while (true) { while (true) {
int output_index = distribution(generator); int output_index = distribution(generator);
real_t y_hat = evaluatev(_input_set[output_index]); _input_set->get_row_into_mlpp_vector(output_index, input_set_row_tmp);
cost_prev = cost({ y_hat }, { _output_set[output_index] }); real_t output_set_element = _output_set->get_element(output_index);
output_set_row_tmp->set_element(0, output_set_element);
real_t y_hat = evaluatev(input_set_row_tmp);
y_hat_row_tmp->set_element(0, y_hat);
cost_prev = cost(y_hat_row_tmp, output_set_row_tmp);
for (int i = 0; i < _k; i++) { for (int i = 0; i < _k; i++) {
// Calculating the weight gradients // Calculating the weight gradients
real_t w_gradient = (y_hat - _output_set[output_index]) * _input_set[output_index][i] * std::pow(_weights[i], _input_set[output_index][i] - 1); real_t w_gradient = (y_hat - output_set_element) * input_set_row_tmp->get_element(i) * Math::pow(_weights->get_element(i), _input_set->get_element(output_index, i) - 1);
real_t i_gradient = (y_hat - _output_set[output_index]) * std::pow(_weights[i], _input_set[output_index][i]); real_t i_gradient = (y_hat - output_set_element) * Math::pow(_weights->get_element(i), _input_set->get_element(output_index, i));
// Weight/initial updation // Weight/initial updation
_weights[i] -= learning_rate * w_gradient; _weights->set_element(i, _weights->get_element(i) - learning_rate * w_gradient);
_initial[i] -= learning_rate * i_gradient; _initial->set_element(i, _initial->get_element(i) - learning_rate * i_gradient);
} }
_weights = regularization.regWeights(_weights, _lambda, _alpha, _reg); _weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
// Calculating the bias gradients // Calculating the bias gradients
real_t b_gradient = (y_hat - _output_set[output_index]); real_t b_gradient = (y_hat - output_set_element);
// Bias updation // Bias updation
_bias -= learning_rate * b_gradient; _bias -= learning_rate * b_gradient;
y_hat = evaluatev(_input_set[output_index]); y_hat = evaluatev(input_set_row_tmp);
if (ui) { if (ui) {
MLPPUtilities::CostInfo(epoch, cost_prev, cost({ y_hat }, { _output_set[output_index] })); MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat_row_tmp, output_set_row_tmp));
MLPPUtilities::UI(_weights, _bias); MLPPUtilities::print_ui_vb(_weights, _bias);
} }
epoch++; epoch++;
@ -144,51 +162,52 @@ void MLPPExpReg::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size,
// Creating the mini-batches // Creating the mini-batches
int n_mini_batch = _n / mini_batch_size; int n_mini_batch = _n / mini_batch_size;
auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch); MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch);
auto input_mini_batches = std::get<0>(batches);
auto output_mini_batches = std::get<1>(batches);
while (true) { while (true) {
for (int i = 0; i < n_mini_batch; i++) { for (int i = 0; i < n_mini_batch; i++) {
std::vector<real_t> y_hat = evaluatem(input_mini_batches[i]); Ref<MLPPMatrix> current_input_batch = batches.input_sets[i];
cost_prev = cost(y_hat, output_mini_batches[i]); Ref<MLPPVector> current_output_batch = batches.output_sets[i];
std::vector<real_t> error = alg.subtraction(y_hat, output_mini_batches[i]);
Ref<MLPPVector> y_hat = evaluatem(current_input_batch);
cost_prev = cost(y_hat, current_output_batch);
Ref<MLPPVector> error = alg.subtractionnv(y_hat, current_output_batch);
for (int j = 0; j < _k; j++) { for (int j = 0; j < _k; j++) {
// Calculating the weight gradient // Calculating the weight gradient
real_t sum = 0; real_t sum = 0;
for (uint32_t k = 0; k < output_mini_batches[i].size(); k++) { for (int k = 0; k < current_output_batch->size(); k++) {
sum += error[k] * input_mini_batches[i][k][j] * std::pow(_weights[j], input_mini_batches[i][k][j] - 1); sum += error->get_element(k) * current_input_batch->get_element(k, j) * Math::pow(_weights->get_element(j), current_input_batch->get_element(k, j) - 1);
} }
real_t w_gradient = sum / output_mini_batches[i].size(); real_t w_gradient = sum / current_output_batch->size();
// Calculating the initial gradient // Calculating the initial gradient
real_t sum2 = 0; real_t sum2 = 0;
for (uint32_t k = 0; k < output_mini_batches[i].size(); k++) { for (int k = 0; k < current_output_batch->size(); k++) {
sum2 += error[k] * std::pow(_weights[j], input_mini_batches[i][k][j]); sum2 += error->get_element(k) * Math::pow(_weights->get_element(j), current_input_batch->get_element(k, j));
} }
real_t i_gradient = sum2 / output_mini_batches[i].size(); real_t i_gradient = sum2 / current_output_batch->size();
// Weight/initial updation // Weight/initial updation
_weights[j] -= learning_rate * w_gradient; _weights->set_element(i, _weights->get_element(i) - learning_rate * w_gradient);
_initial[j] -= learning_rate * i_gradient; _initial->set_element(i, _initial->get_element(i) - learning_rate * i_gradient);
} }
_weights = regularization.regWeights(_weights, _lambda, _alpha, _reg); _weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
// Calculating the bias gradient // Calculating the bias gradient
real_t sum = 0; real_t sum = 0;
for (uint32_t j = 0; j < output_mini_batches[i].size(); j++) { for (int j = 0; j < current_output_batch->size(); j++) {
sum += (y_hat[j] - output_mini_batches[i][j]); sum += (y_hat->get_element(j) - current_output_batch->get_element(j));
} }
//real_t b_gradient = sum / output_mini_batches[i].size(); //real_t b_gradient = sum / output_mini_batches[i].size();
y_hat = evaluatem(input_mini_batches[i]); y_hat = evaluatem(current_input_batch);
if (ui) { if (ui) {
MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, output_mini_batches[i])); MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, current_output_batch));
MLPPUtilities::UI(_weights, _bias); MLPPUtilities::print_ui_vb(_weights, _bias);
} }
} }
@ -205,28 +224,40 @@ void MLPPExpReg::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size,
real_t MLPPExpReg::score() { real_t MLPPExpReg::score() {
MLPPUtilities util; MLPPUtilities util;
return util.performance(_y_hat, _output_set); return util.performance_vec(_y_hat, _output_set);
} }
void MLPPExpReg::save(std::string file_name) { void MLPPExpReg::save(const String &file_name) {
MLPPUtilities util; MLPPUtilities util;
util.saveParameters(file_name, _weights, _initial, _bias); //util.saveParameters(file_name, _weights, _initial, _bias);
} }
MLPPExpReg::MLPPExpReg(std::vector<std::vector<real_t>> p_input_set, std::vector<real_t> p_output_set, std::string p_reg, real_t p_lambda, real_t p_alpha) { MLPPExpReg::MLPPExpReg(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPVector> &p_output_set, MLPPReg::RegularizationType p_reg, real_t p_lambda, real_t p_alpha) {
_input_set = p_input_set; _input_set = p_input_set;
_output_set = p_output_set; _output_set = p_output_set;
_n = p_input_set.size(); _n = p_input_set->size().y;
_k = p_input_set[0].size(); _k = p_input_set->size().x;
_reg = p_reg; _reg = p_reg;
_lambda = p_lambda; _lambda = p_lambda;
_alpha = p_alpha; _alpha = p_alpha;
_y_hat.resize(_n); _y_hat.instance();
_weights = MLPPUtilities::weightInitialization(_k); _y_hat->resize(_n);
_initial = MLPPUtilities::weightInitialization(_k);
_bias = MLPPUtilities::biasInitialization(); MLPPUtilities util;
_weights.instance();
_weights->resize(_k);
util.weight_initializationv(_weights);
_initial.instance();
_initial->resize(_k);
util.weight_initializationv(_initial);
_bias = util.bias_initializationr();
} }
MLPPExpReg::MLPPExpReg() { MLPPExpReg::MLPPExpReg() {
@ -234,33 +265,38 @@ MLPPExpReg::MLPPExpReg() {
MLPPExpReg::~MLPPExpReg() { MLPPExpReg::~MLPPExpReg() {
} }
real_t MLPPExpReg::cost(std::vector<real_t> y_hat, std::vector<real_t> y) { real_t MLPPExpReg::cost(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
MLPPReg regularization; MLPPReg regularization;
MLPPCost mlpp_cost; MLPPCost mlpp_cost;
return mlpp_cost.MSE(y_hat, y) + regularization.regTerm(_weights, _lambda, _alpha, _reg); return mlpp_cost.msev(y_hat, y) + regularization.reg_termv(_weights, _lambda, _alpha, _reg);
} }
real_t MLPPExpReg::evaluatev(std::vector<real_t> x) { real_t MLPPExpReg::evaluatev(const Ref<MLPPVector> &x) {
real_t y_hat = 0; real_t y_hat = 0;
for (uint32_t i = 0; i < x.size(); i++) { for (int i = 0; i < x->size(); i++) {
y_hat += _initial[i] * std::pow(_weights[i], x[i]); y_hat += _initial->get_element(i) * Math::pow(_weights->get_element(i), x->get_element(i));
} }
return y_hat + _bias; return y_hat + _bias;
} }
std::vector<real_t> MLPPExpReg::evaluatem(std::vector<std::vector<real_t>> X) { Ref<MLPPVector> MLPPExpReg::evaluatem(const Ref<MLPPMatrix> &X) {
std::vector<real_t> y_hat; Ref<MLPPVector> y_hat;
y_hat.resize(X.size()); y_hat.instance();
y_hat->resize(X->size().y);
for (uint32_t i = 0; i < X.size(); i++) { for (int i = 0; i < X->size().y; i++) {
y_hat[i] = 0; real_t y;
for (uint32_t j = 0; j < X[i].size(); j++) {
y_hat[i] += _initial[j] * std::pow(_weights[j], X[i][j]); for (int j = 0; j < X->size().x; j++) {
y += _initial->get_element(j) * Math::pow(_weights->get_element(j), X->get_element(i, j));
} }
y_hat[i] += _bias;
y += _bias;
y_hat->set_element(i, y);
} }
return y_hat; return y_hat;

View File

@ -12,15 +12,17 @@
#include "core/object/reference.h" #include "core/object/reference.h"
#include <string> #include "../regularization/reg.h"
#include <vector>
#include "../lin_alg/mlpp_matrix.h"
#include "../lin_alg/mlpp_vector.h"
class MLPPExpReg : public Reference { class MLPPExpReg : public Reference {
GDCLASS(MLPPExpReg, Reference); GDCLASS(MLPPExpReg, Reference);
public: public:
std::vector<real_t> model_set_test(std::vector<std::vector<real_t>> X); Ref<MLPPVector> model_set_test(const Ref<MLPPMatrix> &X);
real_t model_test(std::vector<real_t> x); real_t model_test(const Ref<MLPPVector> &x);
void gradient_descent(real_t learning_rate, int max_epoch, bool ui = false); 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); void sgd(real_t learning_rate, int max_epoch, bool ui = false);
@ -28,35 +30,35 @@ public:
real_t score(); real_t score();
void save(std::string file_name); void save(const String &file_name);
MLPPExpReg(std::vector<std::vector<real_t>> p_input_set, std::vector<real_t> p_output_set, std::string p_reg = "None", real_t p_lambda = 0.5, real_t p_alpha = 0.5); MLPPExpReg(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPVector> &p_output_set, MLPPReg::RegularizationType p_reg = MLPPReg::REGULARIZATION_TYPE_NONE, real_t p_lambda = 0.5, real_t p_alpha = 0.5);
MLPPExpReg(); MLPPExpReg();
~MLPPExpReg(); ~MLPPExpReg();
protected: protected:
real_t cost(std::vector<real_t> y_hat, std::vector<real_t> y); real_t cost(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
real_t evaluatev(std::vector<real_t> x); real_t evaluatev(const Ref<MLPPVector> &x);
std::vector<real_t> evaluatem(std::vector<std::vector<real_t>> X); Ref<MLPPVector> evaluatem(const Ref<MLPPMatrix> &X);
void forward_pass(); void forward_pass();
static void _bind_methods(); static void _bind_methods();
std::vector<std::vector<real_t>> _input_set; Ref<MLPPMatrix> _input_set;
std::vector<real_t> _output_set; Ref<MLPPVector> _output_set;
std::vector<real_t> _y_hat; Ref<MLPPVector> _y_hat;
std::vector<real_t> _weights; Ref<MLPPVector> _weights;
std::vector<real_t> _initial; Ref<MLPPVector> _initial;
real_t _bias; real_t _bias;
int _n; int _n;
int _k; int _k;
// Regularization Params // Regularization Params
std::string _reg; MLPPReg::RegularizationType _reg;
real_t _lambda; real_t _lambda;
real_t _alpha; /* This is the controlling param for Elastic Net*/ real_t _alpha; /* This is the controlling param for Elastic Net*/
}; };

View File

@ -447,10 +447,18 @@ void MLPPTests::test_exp_reg_regression(bool ui) {
alg.printVector(model_old.modelSetTest(alg.transpose(inputSet))); alg.printVector(model_old.modelSetTest(alg.transpose(inputSet)));
std::cout << "ACCURACY: " << 100 * model_old.score() << "%" << std::endl; std::cout << "ACCURACY: " << 100 * model_old.score() << "%" << std::endl;
MLPPExpReg model(alg.transpose(inputSet), outputSet); Ref<MLPPMatrix> input_set;
input_set.instance();
input_set->set_from_std_vectors(inputSet);
Ref<MLPPVector> output_set;
output_set.instance();
output_set->set_from_std_vector(outputSet);
MLPPExpReg model(alg.transposem(input_set), output_set);
model.sgd(0.001, 10000, ui); model.sgd(0.001, 10000, ui);
alg.printVector(model.model_set_test(alg.transpose(inputSet))); PLOG_MSG(model.model_set_test(alg.transposem(input_set))->to_string());
std::cout << "ACCURACY: " << 100 * model.score() << "%" << std::endl; PLOG_MSG("ACCURACY: " + String::num(100 * model.score()) + "%");
} }
void MLPPTests::test_tanh_regression(bool ui) { void MLPPTests::test_tanh_regression(bool ui) {
MLPPLinAlg alg; MLPPLinAlg alg;