mirror of
https://github.com/Relintai/pmlpp.git
synced 2024-12-22 15:06:47 +01:00
Now MLPPAutoEncoder uses engine classes.
This commit is contained in:
parent
5ad25ad918
commit
1b3606c7ae
@ -11,15 +11,16 @@
|
|||||||
#include "../lin_alg/lin_alg.h"
|
#include "../lin_alg/lin_alg.h"
|
||||||
#include "../utilities/utilities.h"
|
#include "../utilities/utilities.h"
|
||||||
|
|
||||||
|
#include "core/log/logger.h"
|
||||||
|
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
//UDPATE
|
//UDPATE
|
||||||
Ref<MLPPMatrix> MLPPAutoEncoder::get_input_set() {
|
Ref<MLPPMatrix> MLPPAutoEncoder::get_input_set() {
|
||||||
return Ref<MLPPMatrix>();
|
return _input_set;
|
||||||
//return _input_set;
|
|
||||||
}
|
}
|
||||||
void MLPPAutoEncoder::set_input_set(const Ref<MLPPMatrix> &val) {
|
void MLPPAutoEncoder::set_input_set(const Ref<MLPPMatrix> &val) {
|
||||||
//_input_set = val;
|
_input_set = val;
|
||||||
|
|
||||||
_initialized = false;
|
_initialized = false;
|
||||||
}
|
}
|
||||||
@ -33,14 +34,14 @@ void MLPPAutoEncoder::set_n_hidden(const int val) {
|
|||||||
_initialized = false;
|
_initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> MLPPAutoEncoder::model_set_test(std::vector<std::vector<real_t>> X) {
|
Ref<MLPPMatrix> MLPPAutoEncoder::model_set_test(const Ref<MLPPMatrix> &X) {
|
||||||
ERR_FAIL_COND_V(!_initialized, std::vector<std::vector<real_t>>());
|
ERR_FAIL_COND_V(!_initialized, Ref<MLPPMatrix>());
|
||||||
|
|
||||||
return evaluatem(X);
|
return evaluatem(X);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<real_t> MLPPAutoEncoder::model_test(std::vector<real_t> x) {
|
Ref<MLPPVector> MLPPAutoEncoder::model_test(const Ref<MLPPVector> &x) {
|
||||||
ERR_FAIL_COND_V(!_initialized, std::vector<real_t>());
|
ERR_FAIL_COND_V(!_initialized, Ref<MLPPVector>());
|
||||||
|
|
||||||
return evaluatev(x);
|
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);
|
cost_prev = cost(_y_hat, _input_set);
|
||||||
|
|
||||||
// Calculating the errors
|
// 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
|
// 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
|
// 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
|
// 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
|
//Calculating the weight/bias for layer 1
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> D1_1 = alg.matmult(error, alg.transpose(_weights2));
|
Ref<MLPPMatrix> D1_1 = alg.matmultm(error, alg.transposem(_weights2));
|
||||||
|
Ref<MLPPMatrix> D1_2 = alg.hadamard_productm(D1_1, avn.sigmoid_derivm(_z2));
|
||||||
std::vector<std::vector<real_t>> D1_2 = alg.hadamard_product(D1_1, avn.sigmoid(_z2, 1));
|
Ref<MLPPMatrix> D1_3 = alg.matmultm(alg.transposem(_input_set), D1_2);
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> D1_3 = alg.matmult(alg.transpose(_input_set), D1_2);
|
|
||||||
|
|
||||||
// weight an bias updation for layer 1
|
// 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();
|
forward_pass();
|
||||||
|
|
||||||
// UI PORTION
|
// UI PORTION
|
||||||
if (ui) {
|
if (ui) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, cost(_y_hat, _input_set));
|
MLPPUtilities::cost_info(epoch, cost_prev, cost(_y_hat, _input_set));
|
||||||
std::cout << "Layer 1:" << std::endl;
|
PLOG_MSG("Layer 1:");
|
||||||
MLPPUtilities::UI(_weights1, _bias1);
|
MLPPUtilities::print_ui_mb(_weights1, _bias1);
|
||||||
std::cout << "Layer 2:" << std::endl;
|
PLOG_MSG("Layer 2:");
|
||||||
MLPPUtilities::UI(_weights2, _bias2);
|
MLPPUtilities::print_ui_mb(_weights2, _bias2);
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
@ -110,45 +109,62 @@ void MLPPAutoEncoder::sgd(real_t learning_rate, int max_epoch, bool ui) {
|
|||||||
real_t cost_prev = 0;
|
real_t cost_prev = 0;
|
||||||
int epoch = 1;
|
int epoch = 1;
|
||||||
|
|
||||||
while (true) {
|
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
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));
|
||||||
int outputIndex = distribution(generator);
|
|
||||||
|
|
||||||
std::vector<real_t> y_hat = evaluatev(_input_set[outputIndex]);
|
Ref<MLPPVector> input_set_row_tmp;
|
||||||
auto prop_res = propagatev(_input_set[outputIndex]);
|
input_set_row_tmp.instance();
|
||||||
auto z2 = std::get<0>(prop_res);
|
input_set_row_tmp->resize(_input_set->size().x);
|
||||||
auto a2 = std::get<1>(prop_res);
|
|
||||||
|
|
||||||
cost_prev = cost({ y_hat }, { _input_set[outputIndex] });
|
Ref<MLPPMatrix> input_set_mat_tmp;
|
||||||
std::vector<real_t> error = alg.subtraction(y_hat, _input_set[outputIndex]);
|
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
|
// Weight updation for layer 2
|
||||||
std::vector<std::vector<real_t>> D2_1 = alg.outerProduct(error, a2);
|
Ref<MLPPMatrix> D2_1 = alg.outer_product(error, prop_res.a2);
|
||||||
_weights2 = alg.subtraction(_weights2, alg.scalarMultiply(learning_rate, alg.transpose(D2_1)));
|
_weights2 = alg.subtractionm(_weights2, alg.scalar_multiplym(learning_rate, alg.transposem(D2_1)));
|
||||||
|
|
||||||
// Bias updation for layer 2
|
// 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
|
// Weight updation for layer 1
|
||||||
std::vector<real_t> D1_1 = alg.mat_vec_mult(_weights2, error);
|
Ref<MLPPVector> D1_1 = alg.mat_vec_multv(_weights2, error);
|
||||||
std::vector<real_t> D1_2 = alg.hadamard_product(D1_1, avn.sigmoid(z2, 1));
|
Ref<MLPPVector> D1_2 = alg.hadamard_productnv(D1_1, avn.sigmoid_derivv(prop_res.z2));
|
||||||
std::vector<std::vector<real_t>> D1_3 = alg.outerProduct(_input_set[outputIndex], D1_2);
|
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
|
// 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) {
|
if (ui) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, cost({ y_hat }, { _input_set[outputIndex] }));
|
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat_mat_tmp, input_set_mat_tmp));
|
||||||
std::cout << "Layer 1:" << std::endl;
|
|
||||||
MLPPUtilities::UI(_weights1, _bias1);
|
PLOG_MSG("Layer 1:");
|
||||||
std::cout << "Layer 2:" << std::endl;
|
MLPPUtilities::print_ui_mb(_weights1, _bias1);
|
||||||
MLPPUtilities::UI(_weights2, _bias2);
|
PLOG_MSG("Layer 2:");
|
||||||
|
MLPPUtilities::print_ui_mb(_weights2, _bias2);
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
@ -171,52 +187,49 @@ void MLPPAutoEncoder::mbgd(real_t learning_rate, int max_epoch, int mini_batch_s
|
|||||||
|
|
||||||
// Creating the mini-batches
|
// Creating the mini-batches
|
||||||
int n_mini_batch = _n / mini_batch_size;
|
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) {
|
while (true) {
|
||||||
for (int i = 0; i < n_mini_batch; i++) {
|
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]);
|
Ref<MLPPMatrix> y_hat = evaluatem(current_batch);
|
||||||
auto z2 = std::get<0>(prop_res);
|
|
||||||
auto a2 = std::get<1>(prop_res);
|
|
||||||
|
|
||||||
cost_prev = cost(y_hat, inputMiniBatches[i]);
|
PropagateMResult prop_res = propagatem(current_batch);
|
||||||
|
|
||||||
|
cost_prev = cost(y_hat, current_batch);
|
||||||
|
|
||||||
// Calculating the errors
|
// 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
|
// 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
|
// 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
|
// 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
|
//Calculating the weight/bias for layer 1
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> D1_1 = alg.matmult(error, alg.transpose(_weights2));
|
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));
|
||||||
std::vector<std::vector<real_t>> D1_2 = alg.hadamard_product(D1_1, avn.sigmoid(z2, true));
|
Ref<MLPPMatrix> D1_3 = alg.matmultm(alg.transposem(current_batch), D1_2);
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> D1_3 = alg.matmult(alg.transpose(inputMiniBatches[i]), D1_2);
|
|
||||||
|
|
||||||
// weight an bias updation for layer 1
|
// 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(current_batch);
|
||||||
|
|
||||||
y_hat = evaluatem(inputMiniBatches[i]);
|
|
||||||
|
|
||||||
if (ui) {
|
if (ui) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, inputMiniBatches[i]));
|
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, current_batch));
|
||||||
std::cout << "Layer 1:" << std::endl;
|
PLOG_MSG("Layer 1:");
|
||||||
MLPPUtilities::UI(_weights1, _bias1);
|
MLPPUtilities::print_ui_mb(_weights1, _bias1);
|
||||||
std::cout << "Layer 2:" << std::endl;
|
PLOG_MSG("Layer 2:");
|
||||||
MLPPUtilities::UI(_weights2, _bias2);
|
MLPPUtilities::print_ui_mb(_weights2, _bias2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,30 +247,43 @@ real_t MLPPAutoEncoder::score() {
|
|||||||
ERR_FAIL_COND_V(!_initialized, 0);
|
ERR_FAIL_COND_V(!_initialized, 0);
|
||||||
|
|
||||||
MLPPUtilities util;
|
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);
|
ERR_FAIL_COND(!_initialized);
|
||||||
|
|
||||||
MLPPUtilities util;
|
//MLPPUtilities util;
|
||||||
util.saveParameters(fileName, _weights1, _bias1, false, 1);
|
//util.saveParameters(fileName, _weights1, _bias1, false, 1);
|
||||||
util.saveParameters(fileName, _weights2, _bias2, true, 2);
|
//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;
|
_input_set = p_input_set;
|
||||||
_n_hidden = pn_hidden;
|
_n_hidden = p_n_hidden;
|
||||||
_n = _input_set.size();
|
_n = _input_set->size().y;
|
||||||
_k = _input_set[0].size();
|
_k = _input_set->size().x;
|
||||||
|
|
||||||
MLPPActivation avn;
|
_y_hat.instance();
|
||||||
_y_hat.resize(_input_set.size());
|
_y_hat->resize(_input_set->size());
|
||||||
|
|
||||||
_weights1 = MLPPUtilities::weightInitialization(_k, _n_hidden);
|
MLPPUtilities utilities;
|
||||||
_weights2 = MLPPUtilities::weightInitialization(_n_hidden, _k);
|
|
||||||
_bias1 = MLPPUtilities::biasInitialization(_n_hidden);
|
_weights1.instance();
|
||||||
_bias2 = MLPPUtilities::biasInitialization(_k);
|
_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;
|
_initialized = true;
|
||||||
}
|
}
|
||||||
@ -268,59 +294,63 @@ MLPPAutoEncoder::MLPPAutoEncoder() {
|
|||||||
MLPPAutoEncoder::~MLPPAutoEncoder() {
|
MLPPAutoEncoder::~MLPPAutoEncoder() {
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPAutoEncoder::cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y) {
|
real_t MLPPAutoEncoder::cost(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
|
||||||
class MLPPCost cost;
|
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;
|
MLPPLinAlg alg;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
|
|
||||||
std::vector<real_t> z2 = alg.addition(alg.mat_vec_mult(alg.transpose(_weights1), x), _bias1);
|
Ref<MLPPVector> z2 = alg.additionnv(alg.mat_vec_multv(alg.transposem(_weights1), x), _bias1);
|
||||||
std::vector<real_t> a2 = avn.sigmoid(z2);
|
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;
|
MLPPLinAlg alg;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
|
|
||||||
std::vector<real_t> z2 = alg.addition(alg.mat_vec_mult(alg.transpose(_weights1), x), _bias1);
|
PropagateVResult res;
|
||||||
std::vector<real_t> a2 = avn.sigmoid(z2);
|
|
||||||
|
|
||||||
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;
|
MLPPLinAlg alg;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> z2 = alg.mat_vec_add(alg.matmult(X, _weights1), _bias1);
|
Ref<MLPPMatrix> z2 = alg.mat_vec_addv(alg.matmultm(X, _weights1), _bias1);
|
||||||
std::vector<std::vector<real_t>> a2 = avn.sigmoid(z2);
|
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;
|
MLPPLinAlg alg;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> z2 = alg.mat_vec_add(alg.matmult(X, _weights1), _bias1);
|
PropagateMResult res;
|
||||||
std::vector<std::vector<real_t>> a2 = avn.sigmoid(z2);
|
|
||||||
|
|
||||||
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() {
|
void MLPPAutoEncoder::forward_pass() {
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
|
|
||||||
_z2 = alg.mat_vec_add(alg.matmult(_input_set, _weights1), _bias1);
|
_z2 = alg.mat_vec_addv(alg.matmultm(_input_set, _weights1), _bias1);
|
||||||
_a2 = avn.sigmoid(_z2);
|
_a2 = avn.sigmoid_normm(_z2);
|
||||||
_y_hat = alg.mat_vec_add(alg.matmult(_a2, _weights2), _bias2);
|
_y_hat = alg.mat_vec_addv(alg.matmultm(_a2, _weights2), _bias2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPAutoEncoder::_bind_methods() {
|
void MLPPAutoEncoder::_bind_methods() {
|
||||||
|
@ -17,10 +17,8 @@
|
|||||||
|
|
||||||
#include "../regularization/reg.h"
|
#include "../regularization/reg.h"
|
||||||
|
|
||||||
//REMOVE
|
#include "../lin_alg/mlpp_matrix.h"
|
||||||
#include <iostream>
|
#include "../lin_alg/mlpp_vector.h"
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class MLPPAutoEncoder : public Reference {
|
class MLPPAutoEncoder : public Reference {
|
||||||
GDCLASS(MLPPAutoEncoder, Reference);
|
GDCLASS(MLPPAutoEncoder, Reference);
|
||||||
@ -32,8 +30,8 @@ public:
|
|||||||
int get_n_hidden();
|
int get_n_hidden();
|
||||||
void set_n_hidden(const int val);
|
void set_n_hidden(const int val);
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> model_set_test(std::vector<std::vector<real_t>> X);
|
Ref<MLPPMatrix> model_set_test(const Ref<MLPPMatrix> &X);
|
||||||
std::vector<real_t> model_test(std::vector<real_t> x);
|
Ref<MLPPVector> 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);
|
||||||
@ -41,37 +39,49 @@ public:
|
|||||||
|
|
||||||
real_t score();
|
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();
|
||||||
~MLPPAutoEncoder();
|
~MLPPAutoEncoder();
|
||||||
|
|
||||||
protected:
|
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);
|
Ref<MLPPVector> evaluatev(const Ref<MLPPVector> &x);
|
||||||
std::tuple<std::vector<real_t>, std::vector<real_t>> propagatev(std::vector<real_t> x);
|
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> evaluatem(std::vector<std::vector<real_t>> X);
|
struct PropagateVResult {
|
||||||
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> propagatem(std::vector<std::vector<real_t>> X);
|
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();
|
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<std::vector<real_t>> _y_hat;
|
Ref<MLPPMatrix> _y_hat;
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> _weights1;
|
Ref<MLPPMatrix> _weights1;
|
||||||
std::vector<std::vector<real_t>> _weights2;
|
Ref<MLPPMatrix> _weights2;
|
||||||
|
|
||||||
std::vector<real_t> _bias1;
|
Ref<MLPPVector> _bias1;
|
||||||
std::vector<real_t> _bias2;
|
Ref<MLPPVector> _bias2;
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> _z2;
|
Ref<MLPPMatrix> _z2;
|
||||||
std::vector<std::vector<real_t>> _a2;
|
Ref<MLPPMatrix> _a2;
|
||||||
|
|
||||||
int _n;
|
int _n;
|
||||||
int _k;
|
int _k;
|
||||||
|
@ -594,10 +594,14 @@ void MLPPTests::test_autoencoder(bool ui) {
|
|||||||
alg.printMatrix(model_old.modelSetTest(alg.transpose(inputSet)));
|
alg.printMatrix(model_old.modelSetTest(alg.transpose(inputSet)));
|
||||||
std::cout << "ACCURACY (Old): " << 100 * model_old.score() << "%" << std::endl;
|
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);
|
model.sgd(0.001, 300000, ui);
|
||||||
alg.printMatrix(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_dynamically_sized_ann(bool ui) {
|
void MLPPTests::test_dynamically_sized_ann(bool ui) {
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
|
Loading…
Reference in New Issue
Block a user