mirror of
https://github.com/Relintai/pmlpp.git
synced 2024-12-22 15:06:47 +01:00
Initial cleanup pass on AutoEncoder.
This commit is contained in:
parent
e6e50025ad
commit
4e30b31833
@ -5,67 +5,95 @@
|
||||
//
|
||||
|
||||
#include "auto_encoder.h"
|
||||
|
||||
#include "../activation/activation.h"
|
||||
#include "../cost/cost.h"
|
||||
#include "../lin_alg/lin_alg.h"
|
||||
#include "../utilities/utilities.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
|
||||
std::vector<std::vector<real_t>> MLPPAutoEncoder::modelSetTest(std::vector<std::vector<real_t>> X) {
|
||||
return Evaluate(X);
|
||||
//UDPATE
|
||||
Ref<MLPPMatrix> MLPPAutoEncoder::get_input_set() {
|
||||
return Ref<MLPPMatrix>();
|
||||
//return _input_set;
|
||||
}
|
||||
void MLPPAutoEncoder::set_input_set(const Ref<MLPPMatrix> &val) {
|
||||
//_input_set = val;
|
||||
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
std::vector<real_t> MLPPAutoEncoder::modelTest(std::vector<real_t> x) {
|
||||
return Evaluate(x);
|
||||
int MLPPAutoEncoder::get_n_hidden() {
|
||||
return _n_hidden;
|
||||
}
|
||||
void MLPPAutoEncoder::set_n_hidden(const int val) {
|
||||
_n_hidden = val;
|
||||
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
void MLPPAutoEncoder::gradientDescent(real_t learning_rate, int max_epoch, bool UI) {
|
||||
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>>());
|
||||
|
||||
return evaluatem(X);
|
||||
}
|
||||
|
||||
std::vector<real_t> MLPPAutoEncoder::model_test(std::vector<real_t> x) {
|
||||
ERR_FAIL_COND_V(!_initialized, std::vector<real_t>());
|
||||
|
||||
return evaluatev(x);
|
||||
}
|
||||
|
||||
void MLPPAutoEncoder::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
|
||||
ERR_FAIL_COND(!_initialized);
|
||||
|
||||
MLPPActivation avn;
|
||||
MLPPLinAlg alg;
|
||||
real_t cost_prev = 0;
|
||||
int epoch = 1;
|
||||
forwardPass();
|
||||
|
||||
forward_pass();
|
||||
|
||||
while (true) {
|
||||
cost_prev = Cost(y_hat, inputSet);
|
||||
cost_prev = cost(_y_hat, _input_set);
|
||||
|
||||
// Calculating the errors
|
||||
std::vector<std::vector<real_t>> error = alg.subtraction(y_hat, inputSet);
|
||||
std::vector<std::vector<real_t>> error = alg.subtraction(_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);
|
||||
std::vector<std::vector<real_t>> D2_1 = alg.matmult(alg.transpose(_a2), error);
|
||||
|
||||
// weights and bias updation for layer 2
|
||||
weights2 = alg.subtraction(weights2, alg.scalarMultiply(learning_rate / n, D2_1));
|
||||
_weights2 = alg.subtraction(_weights2, alg.scalarMultiply(learning_rate / _n, D2_1));
|
||||
|
||||
// Calculating the bias gradients for layer 2
|
||||
bias2 = alg.subtractMatrixRows(bias2, alg.scalarMultiply(learning_rate, error));
|
||||
_bias2 = alg.subtractMatrixRows(_bias2, alg.scalarMultiply(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_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_2 = alg.hadamard_product(D1_1, avn.sigmoid(_z2, 1));
|
||||
|
||||
std::vector<std::vector<real_t>> D1_3 = alg.matmult(alg.transpose(inputSet), 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
|
||||
weights1 = alg.subtraction(weights1, alg.scalarMultiply(learning_rate / n, D1_3));
|
||||
_weights1 = alg.subtraction(_weights1, alg.scalarMultiply(learning_rate / _n, D1_3));
|
||||
|
||||
bias1 = alg.subtractMatrixRows(bias1, alg.scalarMultiply(learning_rate / n, D1_2));
|
||||
_bias1 = alg.subtractMatrixRows(_bias1, alg.scalarMultiply(learning_rate / _n, D1_2));
|
||||
|
||||
forwardPass();
|
||||
forward_pass();
|
||||
|
||||
// UI PORTION
|
||||
if (UI) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, inputSet));
|
||||
if (ui) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, cost(_y_hat, _input_set));
|
||||
std::cout << "Layer 1:" << std::endl;
|
||||
MLPPUtilities::UI(weights1, bias1);
|
||||
MLPPUtilities::UI(_weights1, _bias1);
|
||||
std::cout << "Layer 2:" << std::endl;
|
||||
MLPPUtilities::UI(weights2, bias2);
|
||||
MLPPUtilities::UI(_weights2, _bias2);
|
||||
}
|
||||
|
||||
epoch++;
|
||||
|
||||
if (epoch > max_epoch) {
|
||||
@ -74,7 +102,9 @@ void MLPPAutoEncoder::gradientDescent(real_t learning_rate, int max_epoch, bool
|
||||
}
|
||||
}
|
||||
|
||||
void MLPPAutoEncoder::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
||||
void MLPPAutoEncoder::sgd(real_t learning_rate, int max_epoch, bool ui) {
|
||||
ERR_FAIL_COND(!_initialized);
|
||||
|
||||
MLPPActivation avn;
|
||||
MLPPLinAlg alg;
|
||||
real_t cost_prev = 0;
|
||||
@ -83,70 +113,75 @@ void MLPPAutoEncoder::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
||||
while (true) {
|
||||
std::random_device 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 = Evaluate(inputSet[outputIndex]);
|
||||
auto prop_res = propagate(inputSet[outputIndex]);
|
||||
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);
|
||||
|
||||
cost_prev = Cost({ y_hat }, { inputSet[outputIndex] });
|
||||
std::vector<real_t> error = alg.subtraction(y_hat, inputSet[outputIndex]);
|
||||
cost_prev = cost({ y_hat }, { _input_set[outputIndex] });
|
||||
std::vector<real_t> error = alg.subtraction(y_hat, _input_set[outputIndex]);
|
||||
|
||||
// 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)));
|
||||
_weights2 = alg.subtraction(_weights2, alg.scalarMultiply(learning_rate, alg.transpose(D2_1)));
|
||||
|
||||
// Bias updation for layer 2
|
||||
bias2 = alg.subtraction(bias2, alg.scalarMultiply(learning_rate, error));
|
||||
_bias2 = alg.subtraction(_bias2, alg.scalarMultiply(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_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(inputSet[outputIndex], D1_2);
|
||||
std::vector<std::vector<real_t>> D1_3 = alg.outerProduct(_input_set[outputIndex], D1_2);
|
||||
|
||||
weights1 = alg.subtraction(weights1, alg.scalarMultiply(learning_rate, D1_3));
|
||||
_weights1 = alg.subtraction(_weights1, alg.scalarMultiply(learning_rate, D1_3));
|
||||
// Bias updation for layer 1
|
||||
|
||||
bias1 = alg.subtraction(bias1, alg.scalarMultiply(learning_rate, D1_2));
|
||||
_bias1 = alg.subtraction(_bias1, alg.scalarMultiply(learning_rate, D1_2));
|
||||
|
||||
y_hat = Evaluate(inputSet[outputIndex]);
|
||||
if (UI) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost({ y_hat }, { inputSet[outputIndex] }));
|
||||
y_hat = evaluatev(_input_set[outputIndex]);
|
||||
|
||||
if (ui) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, cost({ y_hat }, { _input_set[outputIndex] }));
|
||||
std::cout << "Layer 1:" << std::endl;
|
||||
MLPPUtilities::UI(weights1, bias1);
|
||||
MLPPUtilities::UI(_weights1, _bias1);
|
||||
std::cout << "Layer 2:" << std::endl;
|
||||
MLPPUtilities::UI(weights2, bias2);
|
||||
MLPPUtilities::UI(_weights2, _bias2);
|
||||
}
|
||||
|
||||
epoch++;
|
||||
|
||||
if (epoch > max_epoch) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
forwardPass();
|
||||
|
||||
forward_pass();
|
||||
}
|
||||
|
||||
void MLPPAutoEncoder::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) {
|
||||
void MLPPAutoEncoder::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui) {
|
||||
ERR_FAIL_COND(!_initialized);
|
||||
|
||||
MLPPActivation avn;
|
||||
MLPPLinAlg alg;
|
||||
real_t cost_prev = 0;
|
||||
int epoch = 1;
|
||||
|
||||
// Creating the mini-batches
|
||||
int n_mini_batch = n / mini_batch_size;
|
||||
std::vector<std::vector<std::vector<real_t>>> inputMiniBatches = MLPPUtilities::createMiniBatches(inputSet, n_mini_batch);
|
||||
int n_mini_batch = _n / mini_batch_size;
|
||||
std::vector<std::vector<std::vector<real_t>>> inputMiniBatches = MLPPUtilities::createMiniBatches(_input_set, n_mini_batch);
|
||||
|
||||
while (true) {
|
||||
for (int i = 0; i < n_mini_batch; i++) {
|
||||
std::vector<std::vector<real_t>> y_hat = Evaluate(inputMiniBatches[i]);
|
||||
std::vector<std::vector<real_t>> y_hat = evaluatem(inputMiniBatches[i]);
|
||||
|
||||
auto prop_res = propagate(inputMiniBatches[i]);
|
||||
auto prop_res = propagatem(inputMiniBatches[i]);
|
||||
auto z2 = std::get<0>(prop_res);
|
||||
auto a2 = std::get<1>(prop_res);
|
||||
|
||||
cost_prev = Cost(y_hat, inputMiniBatches[i]);
|
||||
cost_prev = cost(y_hat, inputMiniBatches[i]);
|
||||
|
||||
// Calculating the errors
|
||||
std::vector<std::vector<real_t>> error = alg.subtraction(y_hat, inputMiniBatches[i]);
|
||||
@ -156,109 +191,160 @@ void MLPPAutoEncoder::MBGD(real_t learning_rate, int max_epoch, int mini_batch_s
|
||||
std::vector<std::vector<real_t>> D2_1 = alg.matmult(alg.transpose(a2), error);
|
||||
|
||||
// weights and bias updation for layer 2
|
||||
weights2 = alg.subtraction(weights2, alg.scalarMultiply(learning_rate / inputMiniBatches[i].size(), D2_1));
|
||||
_weights2 = alg.subtraction(_weights2, alg.scalarMultiply(learning_rate / inputMiniBatches[i].size(), D2_1));
|
||||
|
||||
// Bias Updation for layer 2
|
||||
bias2 = alg.subtractMatrixRows(bias2, alg.scalarMultiply(learning_rate, error));
|
||||
_bias2 = alg.subtractMatrixRows(_bias2, alg.scalarMultiply(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_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_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);
|
||||
|
||||
// weight an bias updation for layer 1
|
||||
weights1 = alg.subtraction(weights1, alg.scalarMultiply(learning_rate / inputMiniBatches[i].size(), D1_3));
|
||||
_weights1 = alg.subtraction(_weights1, alg.scalarMultiply(learning_rate / inputMiniBatches[i].size(), D1_3));
|
||||
|
||||
bias1 = alg.subtractMatrixRows(bias1, alg.scalarMultiply(learning_rate / inputMiniBatches[i].size(), D1_2));
|
||||
_bias1 = alg.subtractMatrixRows(_bias1, alg.scalarMultiply(learning_rate / inputMiniBatches[i].size(), D1_2));
|
||||
|
||||
y_hat = Evaluate(inputMiniBatches[i]);
|
||||
y_hat = evaluatem(inputMiniBatches[i]);
|
||||
|
||||
if (UI) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, inputMiniBatches[i]));
|
||||
if (ui) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, inputMiniBatches[i]));
|
||||
std::cout << "Layer 1:" << std::endl;
|
||||
MLPPUtilities::UI(weights1, bias1);
|
||||
MLPPUtilities::UI(_weights1, _bias1);
|
||||
std::cout << "Layer 2:" << std::endl;
|
||||
MLPPUtilities::UI(weights2, bias2);
|
||||
MLPPUtilities::UI(_weights2, _bias2);
|
||||
}
|
||||
}
|
||||
|
||||
epoch++;
|
||||
|
||||
if (epoch > max_epoch) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
forwardPass();
|
||||
|
||||
forward_pass();
|
||||
}
|
||||
|
||||
real_t MLPPAutoEncoder::score() {
|
||||
ERR_FAIL_COND_V(!_initialized, 0);
|
||||
|
||||
MLPPUtilities util;
|
||||
return util.performance(y_hat, inputSet);
|
||||
return util.performance(_y_hat, _input_set);
|
||||
}
|
||||
|
||||
void MLPPAutoEncoder::save(std::string fileName) {
|
||||
ERR_FAIL_COND(!_initialized);
|
||||
|
||||
MLPPUtilities util;
|
||||
util.saveParameters(fileName, weights1, bias1, 0, 1);
|
||||
util.saveParameters(fileName, weights2, bias2, 1, 2);
|
||||
util.saveParameters(fileName, _weights1, _bias1, false, 1);
|
||||
util.saveParameters(fileName, _weights2, _bias2, true, 2);
|
||||
}
|
||||
|
||||
MLPPAutoEncoder::MLPPAutoEncoder(std::vector<std::vector<real_t>> pinputSet, int pn_hidden) {
|
||||
inputSet = pinputSet;
|
||||
n_hidden = pn_hidden;
|
||||
n = inputSet.size();
|
||||
k = inputSet[0].size();
|
||||
MLPPAutoEncoder::MLPPAutoEncoder(std::vector<std::vector<real_t>> p_input_set, int pn_hidden) {
|
||||
_input_set = p_input_set;
|
||||
_n_hidden = pn_hidden;
|
||||
_n = _input_set.size();
|
||||
_k = _input_set[0].size();
|
||||
|
||||
MLPPActivation avn;
|
||||
y_hat.resize(inputSet.size());
|
||||
_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);
|
||||
_weights1 = MLPPUtilities::weightInitialization(_k, _n_hidden);
|
||||
_weights2 = MLPPUtilities::weightInitialization(_n_hidden, _k);
|
||||
_bias1 = MLPPUtilities::biasInitialization(_n_hidden);
|
||||
_bias2 = MLPPUtilities::biasInitialization(_k);
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
real_t MLPPAutoEncoder::Cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y) {
|
||||
MLPPAutoEncoder::MLPPAutoEncoder() {
|
||||
_initialized = false;
|
||||
}
|
||||
MLPPAutoEncoder::~MLPPAutoEncoder() {
|
||||
}
|
||||
|
||||
real_t MLPPAutoEncoder::cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y) {
|
||||
class MLPPCost cost;
|
||||
return cost.MSE(y_hat, inputSet);
|
||||
|
||||
return cost.MSE(y_hat, _input_set);
|
||||
}
|
||||
|
||||
std::vector<std::vector<real_t>> MLPPAutoEncoder::Evaluate(std::vector<std::vector<real_t>> X) {
|
||||
std::vector<real_t> MLPPAutoEncoder::evaluatev(std::vector<real_t> 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);
|
||||
return alg.mat_vec_add(alg.matmult(a2, weights2), bias2);
|
||||
|
||||
std::vector<real_t> z2 = alg.addition(alg.mat_vec_mult(alg.transpose(_weights1), x), _bias1);
|
||||
std::vector<real_t> a2 = avn.sigmoid(z2);
|
||||
|
||||
return alg.addition(alg.mat_vec_mult(alg.transpose(_weights2), a2), _bias2);
|
||||
}
|
||||
|
||||
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> MLPPAutoEncoder::propagate(std::vector<std::vector<real_t>> X) {
|
||||
std::tuple<std::vector<real_t>, std::vector<real_t>> MLPPAutoEncoder::propagatev(std::vector<real_t> 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);
|
||||
|
||||
std::vector<real_t> z2 = alg.addition(alg.mat_vec_mult(alg.transpose(_weights1), x), _bias1);
|
||||
std::vector<real_t> a2 = avn.sigmoid(z2);
|
||||
|
||||
return { z2, a2 };
|
||||
}
|
||||
|
||||
std::vector<real_t> MLPPAutoEncoder::Evaluate(std::vector<real_t> x) {
|
||||
std::vector<std::vector<real_t>> MLPPAutoEncoder::evaluatem(std::vector<std::vector<real_t>> 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);
|
||||
return alg.addition(alg.mat_vec_mult(alg.transpose(weights2), a2), bias2);
|
||||
|
||||
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);
|
||||
|
||||
return alg.mat_vec_add(alg.matmult(a2, _weights2), _bias2);
|
||||
}
|
||||
|
||||
std::tuple<std::vector<real_t>, std::vector<real_t>> MLPPAutoEncoder::propagate(std::vector<real_t> x) {
|
||||
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> MLPPAutoEncoder::propagatem(std::vector<std::vector<real_t>> 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);
|
||||
|
||||
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);
|
||||
|
||||
return { z2, a2 };
|
||||
}
|
||||
|
||||
void MLPPAutoEncoder::forwardPass() {
|
||||
void MLPPAutoEncoder::forward_pass() {
|
||||
MLPPLinAlg alg;
|
||||
MLPPActivation avn;
|
||||
z2 = alg.mat_vec_add(alg.matmult(inputSet, weights1), bias1);
|
||||
a2 = avn.sigmoid(z2);
|
||||
y_hat = alg.mat_vec_add(alg.matmult(a2, weights2), bias2);
|
||||
|
||||
_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);
|
||||
}
|
||||
|
||||
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);
|
||||
*/
|
||||
}
|
||||
|
@ -10,18 +10,34 @@
|
||||
|
||||
#include "core/math/math_defs.h"
|
||||
|
||||
#include "core/object/reference.h"
|
||||
|
||||
#include "../lin_alg/mlpp_matrix.h"
|
||||
#include "../lin_alg/mlpp_vector.h"
|
||||
|
||||
#include "../regularization/reg.h"
|
||||
|
||||
//REMOVE
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
class MLPPAutoEncoder {
|
||||
public:
|
||||
std::vector<std::vector<real_t>> modelSetTest(std::vector<std::vector<real_t>> X);
|
||||
std::vector<real_t> modelTest(std::vector<real_t> x);
|
||||
class MLPPAutoEncoder : public Reference {
|
||||
GDCLASS(MLPPAutoEncoder, Reference);
|
||||
|
||||
void gradientDescent(real_t learning_rate, int max_epoch, bool UI = false);
|
||||
void SGD(real_t learning_rate, int max_epoch, bool UI = false);
|
||||
void MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI = false);
|
||||
public:
|
||||
Ref<MLPPMatrix> get_input_set();
|
||||
void set_input_set(const Ref<MLPPMatrix> &val);
|
||||
|
||||
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);
|
||||
|
||||
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 mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui = false);
|
||||
|
||||
real_t score();
|
||||
|
||||
@ -29,30 +45,39 @@ public:
|
||||
|
||||
MLPPAutoEncoder(std::vector<std::vector<real_t>> inputSet, int n_hidden);
|
||||
|
||||
private:
|
||||
real_t Cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y);
|
||||
MLPPAutoEncoder();
|
||||
~MLPPAutoEncoder();
|
||||
|
||||
std::vector<std::vector<real_t>> Evaluate(std::vector<std::vector<real_t>> X);
|
||||
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> propagate(std::vector<std::vector<real_t>> X);
|
||||
std::vector<real_t> Evaluate(std::vector<real_t> x);
|
||||
std::tuple<std::vector<real_t>, std::vector<real_t>> propagate(std::vector<real_t> x);
|
||||
void forwardPass();
|
||||
protected:
|
||||
real_t cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y);
|
||||
|
||||
std::vector<std::vector<real_t>> inputSet;
|
||||
std::vector<std::vector<real_t>> y_hat;
|
||||
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);
|
||||
|
||||
std::vector<std::vector<real_t>> weights1;
|
||||
std::vector<std::vector<real_t>> weights2;
|
||||
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);
|
||||
|
||||
std::vector<real_t> bias1;
|
||||
std::vector<real_t> bias2;
|
||||
void forward_pass();
|
||||
|
||||
std::vector<std::vector<real_t>> z2;
|
||||
std::vector<std::vector<real_t>> a2;
|
||||
static void _bind_methods();
|
||||
|
||||
int n;
|
||||
int k;
|
||||
int n_hidden;
|
||||
std::vector<std::vector<real_t>> _input_set;
|
||||
std::vector<std::vector<real_t>> _y_hat;
|
||||
|
||||
std::vector<std::vector<real_t>> _weights1;
|
||||
std::vector<std::vector<real_t>> _weights2;
|
||||
|
||||
std::vector<real_t> _bias1;
|
||||
std::vector<real_t> _bias2;
|
||||
|
||||
std::vector<std::vector<real_t>> _z2;
|
||||
std::vector<std::vector<real_t>> _a2;
|
||||
|
||||
int _n;
|
||||
int _k;
|
||||
int _n_hidden;
|
||||
|
||||
bool _initialized;
|
||||
};
|
||||
|
||||
#endif /* AutoEncoder_hpp */
|
||||
|
@ -45,6 +45,7 @@ SOFTWARE.
|
||||
#include "mlpp/probit_reg/probit_reg.h"
|
||||
#include "mlpp/svc/svc.h"
|
||||
#include "mlpp/softmax_reg/softmax_reg.h"
|
||||
#include "mlpp/auto_encoder/auto_encoder.h"
|
||||
|
||||
#include "mlpp/mlp/mlp.h"
|
||||
|
||||
@ -75,6 +76,7 @@ void register_pmlpp_types(ModuleRegistrationLevel p_level) {
|
||||
ClassDB::register_class<MLPPProbitReg>();
|
||||
ClassDB::register_class<MLPPSVC>();
|
||||
ClassDB::register_class<MLPPSoftmaxReg>();
|
||||
ClassDB::register_class<MLPPAutoEncoder>();
|
||||
|
||||
ClassDB::register_class<MLPPDataESimple>();
|
||||
ClassDB::register_class<MLPPDataSimple>();
|
||||
|
@ -503,6 +503,11 @@ void MLPPTests::test_autoencoder(bool ui) {
|
||||
model_old.SGD(0.001, 300000, 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);
|
||||
model.sgd(0.001, 300000, ui);
|
||||
alg.printMatrix(model.model_set_test(alg.transpose(inputSet)));
|
||||
std::cout << "ACCURACY: " << 100 * model.score() << "%" << std::endl;
|
||||
}
|
||||
void MLPPTests::test_dynamically_sized_ann(bool ui) {
|
||||
MLPPLinAlg alg;
|
||||
|
Loading…
Reference in New Issue
Block a user