2023-01-23 21:13:26 +01:00
|
|
|
//
|
|
|
|
// AutoEncoder.cpp
|
|
|
|
//
|
|
|
|
// Created by Marc Melikyan on 11/4/20.
|
|
|
|
//
|
|
|
|
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "auto_encoder.h"
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "../activation/activation.h"
|
2023-01-24 19:00:54 +01:00
|
|
|
#include "../cost/cost.h"
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "../lin_alg/lin_alg.h"
|
|
|
|
#include "../utilities/utilities.h"
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
#include "core/log/logger.h"
|
|
|
|
|
2023-01-23 21:13:26 +01:00
|
|
|
#include <random>
|
|
|
|
|
2023-02-10 20:48:55 +01:00
|
|
|
//UDPATE
|
|
|
|
Ref<MLPPMatrix> MLPPAutoEncoder::get_input_set() {
|
2023-02-16 22:51:23 +01:00
|
|
|
return _input_set;
|
2023-02-10 20:48:55 +01:00
|
|
|
}
|
|
|
|
void MLPPAutoEncoder::set_input_set(const Ref<MLPPMatrix> &val) {
|
2023-02-16 22:51:23 +01:00
|
|
|
_input_set = val;
|
2023-02-10 20:48:55 +01:00
|
|
|
|
|
|
|
_initialized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MLPPAutoEncoder::get_n_hidden() {
|
|
|
|
return _n_hidden;
|
|
|
|
}
|
|
|
|
void MLPPAutoEncoder::set_n_hidden(const int val) {
|
|
|
|
_n_hidden = val;
|
|
|
|
|
|
|
|
_initialized = false;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
Ref<MLPPMatrix> MLPPAutoEncoder::model_set_test(const Ref<MLPPMatrix> &X) {
|
|
|
|
ERR_FAIL_COND_V(!_initialized, Ref<MLPPMatrix>());
|
2023-02-10 20:48:55 +01:00
|
|
|
|
|
|
|
return evaluatem(X);
|
|
|
|
}
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
Ref<MLPPVector> MLPPAutoEncoder::model_test(const Ref<MLPPVector> &x) {
|
|
|
|
ERR_FAIL_COND_V(!_initialized, Ref<MLPPVector>());
|
2023-02-10 20:48:55 +01:00
|
|
|
|
|
|
|
return evaluatev(x);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 20:48:55 +01:00
|
|
|
void MLPPAutoEncoder::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
|
|
|
|
ERR_FAIL_COND(!_initialized);
|
|
|
|
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t cost_prev = 0;
|
2023-01-24 19:00:54 +01:00
|
|
|
int epoch = 1;
|
2023-02-10 20:48:55 +01:00
|
|
|
|
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
while (true) {
|
2023-02-10 20:48:55 +01:00
|
|
|
cost_prev = cost(_y_hat, _input_set);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Calculating the errors
|
2023-04-22 14:23:51 +02:00
|
|
|
Ref<MLPPMatrix> error = alg.subtractionnm(_y_hat, _input_set);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Calculating the weight/bias gradients for layer 2
|
2023-04-22 14:23:51 +02:00
|
|
|
Ref<MLPPMatrix> D2_1 = alg.matmultnm(alg.transposenm(_a2), error);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// weights and bias updation for layer 2
|
2023-04-22 14:23:51 +02:00
|
|
|
_weights2 = alg.subtractionnm(_weights2, alg.scalar_multiplynm(learning_rate / _n, D2_1));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Calculating the bias gradients for layer 2
|
2023-04-22 14:46:25 +02:00
|
|
|
_bias2 = alg.subtract_matrix_rowsnv(_bias2, alg.scalar_multiplynm(learning_rate, error));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
//Calculating the weight/bias for layer 1
|
|
|
|
|
2023-04-22 14:23:51 +02:00
|
|
|
Ref<MLPPMatrix> D1_1 = alg.matmultnm(error, alg.transposenm(_weights2));
|
|
|
|
Ref<MLPPMatrix> D1_2 = alg.hadamard_productnm(D1_1, avn.sigmoid_derivm(_z2));
|
|
|
|
Ref<MLPPMatrix> D1_3 = alg.matmultnm(alg.transposenm(_input_set), D1_2);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// weight an bias updation for layer 1
|
2023-04-22 14:23:51 +02:00
|
|
|
_weights1 = alg.subtractionnm(_weights1, alg.scalar_multiplynm(learning_rate / _n, D1_3));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-04-22 14:46:25 +02:00
|
|
|
_bias1 = alg.subtract_matrix_rowsnv(_bias1, alg.scalar_multiplynm(learning_rate / _n, D1_2));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-10 20:48:55 +01:00
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// UI PORTION
|
2023-02-10 20:48:55 +01:00
|
|
|
if (ui) {
|
2023-02-16 22:51:23 +01:00
|
|
|
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);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
epoch++;
|
|
|
|
|
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:48:55 +01:00
|
|
|
void MLPPAutoEncoder::sgd(real_t learning_rate, int max_epoch, bool ui) {
|
|
|
|
ERR_FAIL_COND(!_initialized);
|
|
|
|
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t cost_prev = 0;
|
2023-01-24 19:00:54 +01:00
|
|
|
int epoch = 1;
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
std::random_device rd;
|
|
|
|
std::default_random_engine generator(rd());
|
|
|
|
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<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));
|
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
while (true) {
|
2023-02-16 22:51:23 +01:00
|
|
|
int output_index = distribution(generator);
|
|
|
|
|
2023-04-29 15:07:30 +02:00
|
|
|
_input_set->row_get_into_mlpp_vector(output_index, input_set_row_tmp);
|
|
|
|
input_set_mat_tmp->row_set_mlpp_vector(0, input_set_row_tmp);
|
2023-02-16 22:51:23 +01:00
|
|
|
|
|
|
|
Ref<MLPPVector> y_hat = evaluatev(input_set_row_tmp);
|
2023-04-29 15:07:30 +02:00
|
|
|
y_hat_mat_tmp->row_set_mlpp_vector(0, y_hat);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
PropagateVResult prop_res = propagatev(input_set_row_tmp);
|
2023-02-10 20:05:47 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
cost_prev = cost(y_hat_mat_tmp, input_set_mat_tmp);
|
|
|
|
Ref<MLPPVector> error = alg.subtractionnv(y_hat, input_set_row_tmp);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Weight updation for layer 2
|
2023-02-16 22:51:23 +01:00
|
|
|
Ref<MLPPMatrix> D2_1 = alg.outer_product(error, prop_res.a2);
|
2023-04-22 14:23:51 +02:00
|
|
|
_weights2 = alg.subtractionnm(_weights2, alg.scalar_multiplynm(learning_rate, alg.transposenm(D2_1)));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Bias updation for layer 2
|
2023-02-16 22:51:23 +01:00
|
|
|
_bias2 = alg.subtractionnv(_bias2, alg.scalar_multiplynv(learning_rate, error));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Weight updation for layer 1
|
2023-04-22 14:46:25 +02:00
|
|
|
Ref<MLPPVector> D1_1 = alg.mat_vec_multnv(_weights2, error);
|
2023-02-16 22:51:23 +01:00
|
|
|
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);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-04-22 14:23:51 +02:00
|
|
|
_weights1 = alg.subtractionnm(_weights1, alg.scalar_multiplynm(learning_rate, D1_3));
|
2023-01-24 19:00:54 +01:00
|
|
|
// Bias updation for layer 1
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
_bias1 = alg.subtractionnv(_bias1, alg.scalar_multiplynv(learning_rate, D1_2));
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
y_hat = evaluatev(input_set_row_tmp);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-10 20:48:55 +01:00
|
|
|
if (ui) {
|
2023-02-16 22:51:23 +01:00
|
|
|
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);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
epoch++;
|
|
|
|
|
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-10 20:48:55 +01:00
|
|
|
|
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 20:48:55 +01:00
|
|
|
void MLPPAutoEncoder::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui) {
|
|
|
|
ERR_FAIL_COND(!_initialized);
|
|
|
|
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t cost_prev = 0;
|
2023-01-24 19:00:54 +01:00
|
|
|
int epoch = 1;
|
|
|
|
|
|
|
|
// Creating the mini-batches
|
2023-02-10 20:48:55 +01:00
|
|
|
int n_mini_batch = _n / mini_batch_size;
|
2023-02-16 22:51:23 +01:00
|
|
|
Vector<Ref<MLPPMatrix>> batches = MLPPUtilities::create_mini_batchesm(_input_set, n_mini_batch);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
for (int i = 0; i < n_mini_batch; i++) {
|
2023-02-16 22:51:23 +01:00
|
|
|
Ref<MLPPMatrix> current_batch = batches[i];
|
|
|
|
|
|
|
|
Ref<MLPPMatrix> y_hat = evaluatem(current_batch);
|
2023-02-10 20:05:47 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
PropagateMResult prop_res = propagatem(current_batch);
|
2023-02-10 20:05:47 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
cost_prev = cost(y_hat, current_batch);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Calculating the errors
|
2023-04-22 14:23:51 +02:00
|
|
|
Ref<MLPPMatrix> error = alg.subtractionnm(y_hat, current_batch);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Calculating the weight/bias gradients for layer 2
|
|
|
|
|
2023-04-22 14:23:51 +02:00
|
|
|
Ref<MLPPMatrix> D2_1 = alg.matmultnm(alg.transposenm(prop_res.a2), error);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// weights and bias updation for layer 2
|
2023-04-22 14:23:51 +02:00
|
|
|
_weights2 = alg.subtractionnm(_weights2, alg.scalar_multiplynm(learning_rate / current_batch->size().y, D2_1));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Bias Updation for layer 2
|
2023-04-22 14:46:25 +02:00
|
|
|
_bias2 = alg.subtract_matrix_rowsnv(_bias2, alg.scalar_multiplynm(learning_rate, error));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
//Calculating the weight/bias for layer 1
|
|
|
|
|
2023-04-22 14:23:51 +02:00
|
|
|
Ref<MLPPMatrix> D1_1 = alg.matmultnm(error, alg.transposenm(_weights2));
|
|
|
|
Ref<MLPPMatrix> D1_2 = alg.hadamard_productnm(D1_1, avn.sigmoid_derivm(prop_res.z2));
|
|
|
|
Ref<MLPPMatrix> D1_3 = alg.matmultnm(alg.transposenm(current_batch), D1_2);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// weight an bias updation for layer 1
|
2023-04-22 14:23:51 +02:00
|
|
|
_weights1 = alg.subtractionnm(_weights1, alg.scalar_multiplynm(learning_rate / current_batch->size().x, D1_3));
|
2023-04-22 14:46:25 +02:00
|
|
|
_bias1 = alg.subtract_matrix_rowsnv(_bias1, alg.scalar_multiplynm(learning_rate / current_batch->size().x, D1_2));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
y_hat = evaluatem(current_batch);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-10 20:48:55 +01:00
|
|
|
if (ui) {
|
2023-02-16 22:51:23 +01:00
|
|
|
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);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
epoch++;
|
2023-02-16 22:51:23 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-10 20:48:55 +01:00
|
|
|
|
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPAutoEncoder::score() {
|
2023-02-10 20:48:55 +01:00
|
|
|
ERR_FAIL_COND_V(!_initialized, 0);
|
|
|
|
|
2023-02-10 20:05:47 +01:00
|
|
|
MLPPUtilities util;
|
2023-02-16 22:51:23 +01:00
|
|
|
return util.performance_mat(_y_hat, _input_set);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
void MLPPAutoEncoder::save(const String &file_name) {
|
2023-02-10 20:48:55 +01:00
|
|
|
ERR_FAIL_COND(!_initialized);
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
//MLPPUtilities util;
|
|
|
|
//util.saveParameters(fileName, _weights1, _bias1, false, 1);
|
|
|
|
//util.saveParameters(fileName, _weights2, _bias2, true, 2);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
MLPPAutoEncoder::MLPPAutoEncoder(const Ref<MLPPMatrix> &p_input_set, int p_n_hidden) {
|
2023-02-10 20:48:55 +01:00
|
|
|
_input_set = p_input_set;
|
2023-02-16 22:51:23 +01:00
|
|
|
_n_hidden = p_n_hidden;
|
|
|
|
_n = _input_set->size().y;
|
|
|
|
_k = _input_set->size().x;
|
2023-02-10 20:05:47 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
_y_hat.instance();
|
|
|
|
_y_hat->resize(_input_set->size());
|
|
|
|
|
|
|
|
MLPPUtilities utilities;
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
_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);
|
2023-02-10 20:05:47 +01:00
|
|
|
|
2023-02-10 20:48:55 +01:00
|
|
|
_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MLPPAutoEncoder::MLPPAutoEncoder() {
|
|
|
|
_initialized = false;
|
|
|
|
}
|
|
|
|
MLPPAutoEncoder::~MLPPAutoEncoder() {
|
2023-02-10 20:05:47 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
real_t MLPPAutoEncoder::cost(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
|
|
|
|
MLPPCost mlpp_cost;
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
return mlpp_cost.msem(y_hat, _input_set);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
Ref<MLPPVector> MLPPAutoEncoder::evaluatev(const Ref<MLPPVector> &x) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-04-22 14:46:25 +02:00
|
|
|
Ref<MLPPVector> z2 = alg.additionnv(alg.mat_vec_multnv(alg.transposenm(_weights1), x), _bias1);
|
2023-02-16 22:51:23 +01:00
|
|
|
Ref<MLPPVector> a2 = avn.sigmoid_normv(z2);
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-04-22 14:46:25 +02:00
|
|
|
return alg.additionnv(alg.mat_vec_multnv(alg.transposenm(_weights2), a2), _bias2);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
MLPPAutoEncoder::PropagateVResult MLPPAutoEncoder::propagatev(const Ref<MLPPVector> &x) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
PropagateVResult res;
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-04-22 14:46:25 +02:00
|
|
|
res.z2 = alg.additionnv(alg.mat_vec_multnv(alg.transposenm(_weights1), x), _bias1);
|
2023-02-16 22:51:23 +01:00
|
|
|
res.a2 = avn.sigmoid_normv(res.z2);
|
|
|
|
|
|
|
|
return res;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
Ref<MLPPMatrix> MLPPAutoEncoder::evaluatem(const Ref<MLPPMatrix> &X) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-04-22 14:46:25 +02:00
|
|
|
Ref<MLPPMatrix> z2 = alg.mat_vec_addnm(alg.matmultnm(X, _weights1), _bias1);
|
2023-02-16 22:51:23 +01:00
|
|
|
Ref<MLPPMatrix> a2 = avn.sigmoid_normm(z2);
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-04-22 14:46:25 +02:00
|
|
|
return alg.mat_vec_addnm(alg.matmultnm(a2, _weights2), _bias2);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
MLPPAutoEncoder::PropagateMResult MLPPAutoEncoder::propagatem(const Ref<MLPPMatrix> &X) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
PropagateMResult res;
|
|
|
|
|
2023-04-22 14:46:25 +02:00
|
|
|
res.z2 = alg.mat_vec_addnm(alg.matmultnm(X, _weights1), _bias1);
|
2023-02-16 22:51:23 +01:00
|
|
|
res.a2 = avn.sigmoid_normm(res.z2);
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-02-16 22:51:23 +01:00
|
|
|
return res;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 20:48:55 +01:00
|
|
|
void MLPPAutoEncoder::forward_pass() {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-02-10 20:48:55 +01:00
|
|
|
|
2023-04-22 14:46:25 +02:00
|
|
|
_z2 = alg.mat_vec_addnm(alg.matmultnm(_input_set, _weights1), _bias1);
|
2023-02-16 22:51:23 +01:00
|
|
|
_a2 = avn.sigmoid_normm(_z2);
|
2023-04-22 14:46:25 +02:00
|
|
|
_y_hat = alg.mat_vec_addnm(alg.matmultnm(_a2, _weights2), _bias2);
|
2023-02-10 20:48:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MLPPAutoEncoder::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPAutoEncoder::get_input_set);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_input_set", "val"), &MLPPAutoEncoder::set_input_set);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "input_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_input_set", "get_input_set");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_n_hidden"), &MLPPAutoEncoder::get_n_hidden);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_n_hidden", "val"), &MLPPAutoEncoder::set_n_hidden);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "n_hidden"), "set_n_hidden", "get_n_hidden");
|
|
|
|
|
|
|
|
/*
|
|
|
|
ClassDB::bind_method(D_METHOD("model_set_test", "X"), &MLPPAutoEncoder::model_set_test);
|
|
|
|
ClassDB::bind_method(D_METHOD("model_test", "x"), &MLPPAutoEncoder::model_test);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("gradient_descent", "learning_rate", "max_epoch", "ui"), &MLPPAutoEncoder::gradient_descent, false);
|
|
|
|
ClassDB::bind_method(D_METHOD("sgd", "learning_rate", "max_epoch", "ui"), &MLPPAutoEncoder::sgd, false);
|
|
|
|
ClassDB::bind_method(D_METHOD("mbgd", "learning_rate", "max_epoch", "mini_batch_size", "ui"), &MLPPAutoEncoder::mbgd, false);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("score"), &MLPPAutoEncoder::score);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("save", "file_name"), &MLPPAutoEncoder::save);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("is_initialized"), &MLPPAutoEncoder::is_initialized);
|
|
|
|
ClassDB::bind_method(D_METHOD("initialize"), &MLPPAutoEncoder::initialize);
|
|
|
|
*/
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|