pmlpp/mlpp/tanh_reg/tanh_reg.cpp

356 lines
9.6 KiB
C++
Raw Normal View History

//
// TanhReg.cpp
//
// Created by Marc Melikyan on 10/2/20.
//
2023-01-24 18:12:23 +01:00
#include "tanh_reg.h"
2023-02-10 20:53:26 +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 "../regularization/reg.h"
#include "../utilities/utilities.h"
#include <random>
2023-02-11 01:18:25 +01:00
/*
Ref<MLPPMatrix> MLPPTanhReg::get_input_set() {
return _input_set;
2023-01-24 19:00:54 +01:00
}
2023-02-11 01:18:25 +01:00
void MLPPTanhReg::set_input_set(const Ref<MLPPMatrix> &val) {
_input_set = val;
2023-01-24 19:00:54 +01:00
2023-02-11 01:18:25 +01:00
_initialized = false;
2023-01-24 19:00:54 +01:00
}
2023-02-11 01:18:25 +01:00
Ref<MLPPMatrix> MLPPTanhReg::get_output_set() {
return _output_set;
2023-01-24 19:00:54 +01:00
}
2023-02-11 01:18:25 +01:00
void MLPPTanhReg::set_output_set(const Ref<MLPPMatrix> &val) {
_output_set = val;
2023-01-24 19:00:54 +01:00
2023-02-11 01:18:25 +01:00
_initialized = false;
}
MLPPReg::RegularizationType MLPPTanhReg::get_reg() {
return _reg;
}
void MLPPTanhReg::set_reg(const MLPPReg::RegularizationType val) {
_reg = val;
_initialized = false;
}
real_t MLPPTanhReg::get_lambda() {
return _lambda;
}
void MLPPTanhReg::set_lambda(const real_t val) {
_lambda = val;
_initialized = false;
}
real_t MLPPTanhReg::get_alpha() {
return _alpha;
}
void MLPPTanhReg::set_alpha(const real_t val) {
_alpha = val;
_initialized = false;
}
*/
2023-02-13 21:10:19 +01:00
// Ref<MLPPVector> model_set_test(const Ref<MLPPMatrix> &X);
// real_t model_test(const Ref<MLPPVector> &x);
Ref<MLPPVector> MLPPTanhReg::model_set_test(const Ref<MLPPMatrix> &X) {
2023-02-11 01:18:25 +01:00
return evaluatem(X);
}
2023-02-13 21:10:19 +01:00
real_t MLPPTanhReg::model_test(const Ref<MLPPVector> &x) {
2023-02-11 01:18:25 +01:00
return evaluatev(x);
}
void MLPPTanhReg::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-11 01:18:25 +01:00
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-11 01:18:25 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-11 01:18:25 +01:00
cost_prev = cost(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
2023-02-13 21:10:19 +01:00
Ref<MLPPVector> error = alg.subtractionnv(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
2023-04-22 14:46:25 +02:00
_weights = alg.subtractionnv(_weights, alg.scalar_multiplynv(learning_rate / _n, alg.mat_vec_multnv(alg.transposenm(_input_set), alg.hadamard_productnv(error, avn.tanh_derivv(_z)))));
2023-02-14 00:00:52 +01:00
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradients
2023-02-13 21:10:19 +01:00
_bias -= learning_rate * alg.sum_elementsv(alg.hadamard_productnv(error, avn.tanh_derivv(_z))) / _n;
2023-01-24 19:00:54 +01:00
2023-02-11 01:18:25 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
// UI PORTION
2023-02-11 01:18:25 +01:00
if (ui) {
2023-02-13 21:10:19 +01:00
MLPPUtilities::cost_info(epoch, cost_prev, cost(_y_hat, _output_set));
MLPPUtilities::print_ui_vb(_weights, _bias);
2023-01-24 19:00:54 +01:00
}
2023-02-11 01:18:25 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
}
2023-02-11 01:18:25 +01:00
void MLPPTanhReg::sgd(real_t learning_rate, int max_epoch, bool ui) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-11 01:18:25 +01:00
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-11 01:18:25 +01:00
std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_int_distribution<int> distribution(0, int(_n - 1));
2023-02-13 21:10:19 +01:00
Ref<MLPPVector> input_set_row_tmp;
input_set_row_tmp.instance();
input_set_row_tmp->resize(_input_set->size().x);
Ref<MLPPVector> output_set_row_tmp;
output_set_row_tmp.instance();
output_set_row_tmp->resize(1);
Ref<MLPPVector> y_hat_row_tmp;
y_hat_row_tmp.instance();
y_hat_row_tmp->resize(1);
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-13 21:10:19 +01:00
int output_index = distribution(generator);
_input_set->get_row_into_mlpp_vector(output_index, input_set_row_tmp);
real_t output_set_entry = _output_set->get_element(output_index);
output_set_row_tmp->set_element(0, output_set_entry);
2023-01-24 19:00:54 +01:00
2023-02-13 21:10:19 +01:00
real_t y_hat = evaluatev(input_set_row_tmp);
y_hat_row_tmp->set_element(0, y_hat);
2023-01-24 19:00:54 +01:00
2023-02-13 21:10:19 +01:00
cost_prev = cost(y_hat_row_tmp, output_set_row_tmp);
real_t error = y_hat - output_set_entry;
2023-01-24 19:00:54 +01:00
// Weight Updation
2023-02-13 21:10:19 +01:00
_weights = alg.subtractionnv(_weights, alg.scalar_multiplynv(learning_rate * error * (1 - y_hat * y_hat), input_set_row_tmp));
2023-02-14 00:00:52 +01:00
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Bias updation
2023-02-11 01:18:25 +01:00
_bias -= learning_rate * error * (1 - y_hat * y_hat);
2023-01-24 19:00:54 +01:00
2023-02-13 21:10:19 +01:00
y_hat = evaluatev(input_set_row_tmp);
2023-01-24 19:00:54 +01:00
2023-02-11 01:18:25 +01:00
if (ui) {
2023-02-13 21:10:19 +01:00
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat_row_tmp, output_set_row_tmp));
MLPPUtilities::print_ui_vb(_weights, _bias);
2023-01-24 19:00:54 +01:00
}
2023-02-13 21:10:19 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
2023-02-11 01:18:25 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-11 01:18:25 +01:00
void MLPPTanhReg::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui) {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-10 20:53:26 +01:00
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-11 01:18:25 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-02-13 21:10:19 +01:00
MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_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-13 21:10:19 +01:00
Ref<MLPPMatrix> current_input_batch_entry = batches.input_sets[i];
Ref<MLPPVector> current_output_batch_entry = batches.output_sets[i];
Ref<MLPPVector> y_hat = evaluatem(current_input_batch_entry);
Ref<MLPPVector> z = propagatem(current_input_batch_entry);
cost_prev = cost(y_hat, current_output_batch_entry);
2023-01-24 19:00:54 +01:00
2023-02-13 21:10:19 +01:00
Ref<MLPPVector> error = alg.subtractionnv(y_hat, current_output_batch_entry);
2023-01-24 19:00:54 +01:00
// Calculating the weight gradients
2023-04-22 14:46:25 +02:00
_weights = alg.subtractionnv(_weights, alg.scalar_multiplynv(learning_rate / _n, alg.mat_vec_multnv(alg.transposenm(current_input_batch_entry), alg.hadamard_productnv(error, avn.tanh_derivv(z)))));
2023-02-14 00:00:52 +01:00
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradients
2023-02-13 21:10:19 +01:00
_bias -= learning_rate * alg.sum_elementsv(alg.hadamard_productnv(error, avn.tanh_derivv(_z))) / _n;
2023-01-24 19:00:54 +01:00
2023-02-11 01:18:25 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
2023-02-13 21:10:19 +01:00
y_hat = evaluatem(current_input_batch_entry);
2023-01-24 19:00:54 +01:00
2023-02-11 01:18:25 +01:00
if (ui) {
2023-02-13 21:10:19 +01:00
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, current_output_batch_entry));
MLPPUtilities::print_ui_vb(_weights, _bias);
2023-01-24 19:00:54 +01:00
}
}
2023-02-11 01:18:25 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-11 01:18:25 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-11 01:18:25 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-01-27 13:01:16 +01:00
real_t MLPPTanhReg::score() {
2023-02-10 20:53:26 +01:00
MLPPUtilities util;
2023-02-11 01:18:25 +01:00
2023-02-13 21:10:19 +01:00
return util.performance_vec(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
}
2023-02-13 21:10:19 +01:00
void MLPPTanhReg::save(const String &file_name) {
//MLPPUtilities util;
2023-02-11 01:18:25 +01:00
2023-02-13 21:10:19 +01:00
//util.saveParameters(file_name, _weights, _bias);
2023-02-11 01:18:25 +01:00
}
bool MLPPTanhReg::is_initialized() {
return _initialized;
}
void MLPPTanhReg::initialize() {
if (_initialized) {
return;
}
//ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
_initialized = true;
}
2023-02-13 21:10:19 +01:00
MLPPTanhReg::MLPPTanhReg(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPVector> &p_output_set, MLPPReg::RegularizationType p_reg, real_t p_lambda, real_t p_alpha) {
2023-02-11 01:18:25 +01:00
_input_set = p_input_set;
_output_set = p_output_set;
2023-02-13 21:10:19 +01:00
_n = _input_set->size().y;
_k = _input_set->size().x;
2023-02-11 01:18:25 +01:00
_reg = p_reg;
_lambda = p_lambda;
_alpha = p_alpha;
2023-02-13 21:10:19 +01:00
_y_hat.instance();
_y_hat->resize(_n);
MLPPUtilities utils;
_weights.instance();
_weights->resize(_k);
utils.weight_initializationv(_weights);
_bias = utils.bias_initializationr();
_initialized = true;
2023-02-11 01:18:25 +01:00
}
MLPPTanhReg::MLPPTanhReg() {
2023-02-13 21:10:19 +01:00
_initialized = false;
2023-02-11 01:18:25 +01:00
}
MLPPTanhReg::~MLPPTanhReg() {
2023-01-24 19:00:54 +01:00
}
2023-02-13 21:10:19 +01:00
real_t MLPPTanhReg::cost(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-13 21:10:19 +01:00
MLPPCost mlpp_cost;
2023-02-11 01:18:25 +01:00
2023-02-14 00:00:52 +01:00
return mlpp_cost.msev(y_hat, y) + regularization.reg_termv(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
}
2023-02-13 21:10:19 +01:00
real_t MLPPTanhReg::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-13 21:10:19 +01:00
2023-04-22 14:46:25 +02:00
return avn.tanh_normr(alg.dotnv(_weights, x) + _bias);
2023-01-24 19:00:54 +01:00
}
2023-02-13 21:10:19 +01:00
real_t MLPPTanhReg::propagatev(const Ref<MLPPVector> &x) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-02-13 21:10:19 +01:00
2023-04-22 14:46:25 +02:00
return alg.dotnv(_weights, x) + _bias;
2023-01-24 19:00:54 +01:00
}
2023-02-13 21:10:19 +01:00
Ref<MLPPVector> MLPPTanhReg::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-13 21:10:19 +01:00
2023-04-22 14:46:25 +02:00
return avn.tanh_normv(alg.scalar_addnv(_bias, alg.mat_vec_multnv(X, _weights)));
2023-01-24 19:00:54 +01:00
}
2023-02-13 21:10:19 +01:00
Ref<MLPPVector> MLPPTanhReg::propagatem(const Ref<MLPPMatrix> &X) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-02-13 21:10:19 +01:00
2023-04-22 14:46:25 +02:00
return alg.scalar_addnv(_bias, alg.mat_vec_multnv(X, _weights));
2023-01-24 19:00:54 +01:00
}
// Tanh ( wTx + b )
2023-02-11 01:18:25 +01:00
void MLPPTanhReg::forward_pass() {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-24 19:00:54 +01:00
2023-02-11 01:18:25 +01:00
_z = propagatem(_input_set);
2023-02-13 21:10:19 +01:00
_y_hat = avn.tanh_normv(_z);
2023-02-11 01:18:25 +01:00
}
void MLPPTanhReg::_bind_methods() {
/*
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPTanhReg::get_input_set);
ClassDB::bind_method(D_METHOD("set_input_set", "val"), &MLPPTanhReg::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_output_set"), &MLPPTanhReg::get_output_set);
ClassDB::bind_method(D_METHOD("set_output_set", "val"), &MLPPTanhReg::set_output_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "output_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_output_set", "get_output_set");
ClassDB::bind_method(D_METHOD("get_reg"), &MLPPTanhReg::get_reg);
ClassDB::bind_method(D_METHOD("set_reg", "val"), &MLPPTanhReg::set_reg);
ADD_PROPERTY(PropertyInfo(Variant::INT, "reg"), "set_reg", "get_reg");
ClassDB::bind_method(D_METHOD("get_lambda"), &MLPPTanhReg::get_lambda);
ClassDB::bind_method(D_METHOD("set_lambda", "val"), &MLPPTanhReg::set_lambda);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "lambda"), "set_lambda", "get_lambda");
ClassDB::bind_method(D_METHOD("get_alpha"), &MLPPTanhReg::get_alpha);
ClassDB::bind_method(D_METHOD("set_alpha", "val"), &MLPPTanhReg::set_alpha);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "alpha"), "set_alpha", "get_alpha");
ClassDB::bind_method(D_METHOD("model_test", "x"), &MLPPTanhReg::model_test);
ClassDB::bind_method(D_METHOD("model_set_test", "X"), &MLPPTanhReg::model_set_test);
ClassDB::bind_method(D_METHOD("gradient_descent", "learning_rate", "max_epoch", "ui"), &MLPPTanhReg::gradient_descent, false);
ClassDB::bind_method(D_METHOD("sgd", "learning_rate", "max_epoch", "ui"), &MLPPTanhReg::sgd, false);
ClassDB::bind_method(D_METHOD("mbgd", "learning_rate", "max_epoch", "mini_batch_size", "ui"), &MLPPTanhReg::mbgd, false);
ClassDB::bind_method(D_METHOD("score"), &MLPPTanhReg::score);
ClassDB::bind_method(D_METHOD("save", "file_name"), &MLPPTanhReg::save);
ClassDB::bind_method(D_METHOD("is_initialized"), &MLPPTanhReg::is_initialized);
ClassDB::bind_method(D_METHOD("initialize"), &MLPPTanhReg::initialize);
*/
2023-01-24 19:00:54 +01:00
}