Now MLPPAutoEncoder uses engine classes.

This commit is contained in:
Relintai 2023-02-16 22:51:23 +01:00
parent 5ad25ad918
commit 1b3606c7ae
3 changed files with 177 additions and 133 deletions

View File

@ -11,15 +11,16 @@
#include "../lin_alg/lin_alg.h"
#include "../utilities/utilities.h"
#include "core/log/logger.h"
#include <random>
//UDPATE
Ref<MLPPMatrix> MLPPAutoEncoder::get_input_set() {
return Ref<MLPPMatrix>();
//return _input_set;
return _input_set;
}
void MLPPAutoEncoder::set_input_set(const Ref<MLPPMatrix> &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<std::vector<real_t>> MLPPAutoEncoder::model_set_test(std::vector<std::vector<real_t>> X) {
ERR_FAIL_COND_V(!_initialized, std::vector<std::vector<real_t>>());
Ref<MLPPMatrix> MLPPAutoEncoder::model_set_test(const Ref<MLPPMatrix> &X) {
ERR_FAIL_COND_V(!_initialized, Ref<MLPPMatrix>());
return evaluatem(X);
}
std::vector<real_t> MLPPAutoEncoder::model_test(std::vector<real_t> x) {
ERR_FAIL_COND_V(!_initialized, std::vector<real_t>());
Ref<MLPPVector> MLPPAutoEncoder::model_test(const Ref<MLPPVector> &x) {
ERR_FAIL_COND_V(!_initialized, Ref<MLPPVector>());
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<std::vector<real_t>> error = alg.subtraction(_y_hat, _input_set);
Ref<MLPPMatrix> error = alg.subtractionm(_y_hat, _input_set);
// Calculating the weight/bias gradients for layer 2
std::vector<std::vector<real_t>> D2_1 = alg.matmult(alg.transpose(_a2), error);
Ref<MLPPMatrix> 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<std::vector<real_t>> D1_1 = alg.matmult(error, alg.transpose(_weights2));
std::vector<std::vector<real_t>> D1_2 = alg.hadamard_product(D1_1, avn.sigmoid(_z2, 1));
std::vector<std::vector<real_t>> D1_3 = alg.matmult(alg.transpose(_input_set), D1_2);
Ref<MLPPMatrix> D1_1 = alg.matmultm(error, alg.transposem(_weights2));
Ref<MLPPMatrix> D1_2 = alg.hadamard_productm(D1_1, avn.sigmoid_derivm(_z2));
Ref<MLPPMatrix> 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;
while (true) {
std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_int_distribution<int> distribution(0, int(_n - 1));
int outputIndex = distribution(generator);
std::vector<real_t> 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);
Ref<MLPPVector> input_set_row_tmp;
input_set_row_tmp.instance();
input_set_row_tmp->resize(_input_set->size().x);
cost_prev = cost({ y_hat }, { _input_set[outputIndex] });
std::vector<real_t> error = alg.subtraction(y_hat, _input_set[outputIndex]);
Ref<MLPPMatrix> input_set_mat_tmp;
input_set_mat_tmp.instance();
input_set_mat_tmp->resize(Size2i(_input_set->size().x, 1));
Ref<MLPPMatrix> y_hat_mat_tmp;
y_hat_mat_tmp.instance();
y_hat_mat_tmp->resize(Size2i(_bias2->size(), 1));
while (true) {
int output_index = distribution(generator);
_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);
Ref<MLPPVector> 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<MLPPVector> error = alg.subtractionnv(y_hat, input_set_row_tmp);
// Weight updation for layer 2
std::vector<std::vector<real_t>> D2_1 = alg.outerProduct(error, a2);
_weights2 = alg.subtraction(_weights2, alg.scalarMultiply(learning_rate, alg.transpose(D2_1)));
Ref<MLPPMatrix> 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<real_t> D1_1 = alg.mat_vec_mult(_weights2, error);
std::vector<real_t> D1_2 = alg.hadamard_product(D1_1, avn.sigmoid(z2, 1));
std::vector<std::vector<real_t>> D1_3 = alg.outerProduct(_input_set[outputIndex], D1_2);
Ref<MLPPVector> D1_1 = alg.mat_vec_multv(_weights2, error);
Ref<MLPPVector> D1_2 = alg.hadamard_productnv(D1_1, avn.sigmoid_derivv(prop_res.z2));
Ref<MLPPMatrix> 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,52 +187,49 @@ 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<std::vector<std::vector<real_t>>> inputMiniBatches = MLPPUtilities::createMiniBatches(_input_set, n_mini_batch);
Vector<Ref<MLPPMatrix>> batches = MLPPUtilities::create_mini_batchesm(_input_set, n_mini_batch);
while (true) {
for (int i = 0; i < n_mini_batch; i++) {
std::vector<std::vector<real_t>> y_hat = evaluatem(inputMiniBatches[i]);
Ref<MLPPMatrix> 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<MLPPMatrix> 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<std::vector<real_t>> error = alg.subtraction(y_hat, inputMiniBatches[i]);
Ref<MLPPMatrix> error = alg.subtractionm(y_hat, current_batch);
// Calculating the weight/bias gradients for layer 2
std::vector<std::vector<real_t>> D2_1 = alg.matmult(alg.transpose(a2), error);
Ref<MLPPMatrix> 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<std::vector<real_t>> D1_1 = alg.matmult(error, alg.transpose(_weights2));
std::vector<std::vector<real_t>> D1_2 = alg.hadamard_product(D1_1, avn.sigmoid(z2, true));
std::vector<std::vector<real_t>> D1_3 = alg.matmult(alg.transpose(inputMiniBatches[i]), D1_2);
Ref<MLPPMatrix> D1_1 = alg.matmultm(error, alg.transposem(_weights2));
Ref<MLPPMatrix> D1_2 = alg.hadamard_productm(D1_1, avn.sigmoid_derivm(prop_res.z2));
Ref<MLPPMatrix> 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);
}
}
@ -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<std::vector<real_t>> p_input_set, int pn_hidden) {
MLPPAutoEncoder::MLPPAutoEncoder(const Ref<MLPPMatrix> &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<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y) {
class MLPPCost cost;
real_t MLPPAutoEncoder::cost(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
MLPPCost mlpp_cost;
return cost.MSE(y_hat, _input_set);
return mlpp_cost.msem(y_hat, _input_set);
}
std::vector<real_t> MLPPAutoEncoder::evaluatev(std::vector<real_t> x) {
Ref<MLPPVector> MLPPAutoEncoder::evaluatev(const Ref<MLPPVector> &x) {
MLPPLinAlg alg;
MLPPActivation avn;
std::vector<real_t> z2 = alg.addition(alg.mat_vec_mult(alg.transpose(_weights1), x), _bias1);
std::vector<real_t> a2 = avn.sigmoid(z2);
Ref<MLPPVector> z2 = alg.additionnv(alg.mat_vec_multv(alg.transposem(_weights1), x), _bias1);
Ref<MLPPVector> 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<real_t>, std::vector<real_t>> MLPPAutoEncoder::propagatev(std::vector<real_t> x) {
MLPPAutoEncoder::PropagateVResult MLPPAutoEncoder::propagatev(const Ref<MLPPVector> &x) {
MLPPLinAlg alg;
MLPPActivation avn;
std::vector<real_t> z2 = alg.addition(alg.mat_vec_mult(alg.transpose(_weights1), x), _bias1);
std::vector<real_t> 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<std::vector<real_t>> MLPPAutoEncoder::evaluatem(std::vector<std::vector<real_t>> X) {
Ref<MLPPMatrix> MLPPAutoEncoder::evaluatem(const Ref<MLPPMatrix> &X) {
MLPPLinAlg alg;
MLPPActivation avn;
std::vector<std::vector<real_t>> z2 = alg.mat_vec_add(alg.matmult(X, _weights1), _bias1);
std::vector<std::vector<real_t>> a2 = avn.sigmoid(z2);
Ref<MLPPMatrix> z2 = alg.mat_vec_addv(alg.matmultm(X, _weights1), _bias1);
Ref<MLPPMatrix> 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<std::vector<real_t>>, std::vector<std::vector<real_t>>> MLPPAutoEncoder::propagatem(std::vector<std::vector<real_t>> X) {
MLPPAutoEncoder::PropagateMResult MLPPAutoEncoder::propagatem(const Ref<MLPPMatrix> &X) {
MLPPLinAlg alg;
MLPPActivation avn;
std::vector<std::vector<real_t>> z2 = alg.mat_vec_add(alg.matmult(X, _weights1), _bias1);
std::vector<std::vector<real_t>> 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() {

View File

@ -17,10 +17,8 @@
#include "../regularization/reg.h"
//REMOVE
#include <iostream>
#include <string>
#include <vector>
#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<std::vector<real_t>> model_set_test(std::vector<std::vector<real_t>> X);
std::vector<real_t> model_test(std::vector<real_t> x);
Ref<MLPPMatrix> model_set_test(const Ref<MLPPMatrix> &X);
Ref<MLPPVector> model_test(const Ref<MLPPVector> &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<std::vector<real_t>> inputSet, int n_hidden);
MLPPAutoEncoder(const Ref<MLPPMatrix> &p_input_set, int p_n_hidden);
MLPPAutoEncoder();
~MLPPAutoEncoder();
protected:
real_t cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y);
real_t cost(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
std::vector<real_t> evaluatev(std::vector<real_t> x);
std::tuple<std::vector<real_t>, std::vector<real_t>> propagatev(std::vector<real_t> x);
Ref<MLPPVector> evaluatev(const Ref<MLPPVector> &x);
std::vector<std::vector<real_t>> evaluatem(std::vector<std::vector<real_t>> X);
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> propagatem(std::vector<std::vector<real_t>> X);
struct PropagateVResult {
Ref<MLPPVector> z2;
Ref<MLPPVector> a2;
};
PropagateVResult propagatev(const Ref<MLPPVector> &x);
Ref<MLPPMatrix> evaluatem(const Ref<MLPPMatrix> &X);
struct PropagateMResult {
Ref<MLPPMatrix> z2;
Ref<MLPPMatrix> a2;
};
PropagateMResult propagatem(const Ref<MLPPMatrix> &X);
void forward_pass();
static void _bind_methods();
std::vector<std::vector<real_t>> _input_set;
std::vector<std::vector<real_t>> _y_hat;
Ref<MLPPMatrix> _input_set;
Ref<MLPPMatrix> _y_hat;
std::vector<std::vector<real_t>> _weights1;
std::vector<std::vector<real_t>> _weights2;
Ref<MLPPMatrix> _weights1;
Ref<MLPPMatrix> _weights2;
std::vector<real_t> _bias1;
std::vector<real_t> _bias2;
Ref<MLPPVector> _bias1;
Ref<MLPPVector> _bias2;
std::vector<std::vector<real_t>> _z2;
std::vector<std::vector<real_t>> _a2;
Ref<MLPPMatrix> _z2;
Ref<MLPPMatrix> _a2;
int _n;
int _k;

View File

@ -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<MLPPMatrix> 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;