pmlpp/mlpp/tanh_reg/tanh_reg.cpp

425 lines
12 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 "../regularization/reg.h"
#include "../utilities/utilities.h"
#include <random>
Ref<MLPPMatrix> MLPPTanhReg::get_input_set() const {
2023-02-11 01:18:25 +01:00
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
}
Ref<MLPPMatrix> MLPPTanhReg::get_output_set() const {
2023-02-11 01:18:25 +01:00
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;
}
MLPPReg::RegularizationType MLPPTanhReg::get_reg() const {
2023-02-11 01:18:25 +01:00
return _reg;
}
void MLPPTanhReg::set_reg(const MLPPReg::RegularizationType val) {
_reg = val;
}
real_t MLPPTanhReg::get_lambda() const {
2023-02-11 01:18:25 +01:00
return _lambda;
}
void MLPPTanhReg::set_lambda(const real_t val) {
_lambda = val;
}
real_t MLPPTanhReg::get_alpha() const {
2023-02-11 01:18:25 +01:00
return _alpha;
}
void MLPPTanhReg::set_alpha(const real_t val) {
_alpha = val;
}
2023-02-13 21:10:19 +01:00
Ref<MLPPVector> MLPPTanhReg::data_z_get() const {
return _z;
}
void MLPPTanhReg::data_z_set(const Ref<MLPPVector> &val) {
2023-04-29 18:33:27 +02:00
if (!val.is_valid()) {
return;
}
_z = val;
}
Ref<MLPPVector> MLPPTanhReg::data_y_hat_get() const {
return _y_hat;
}
void MLPPTanhReg::data_y_hat_set(const Ref<MLPPVector> &val) {
2023-04-29 18:33:27 +02:00
if (!val.is_valid()) {
return;
}
_y_hat = val;
}
Ref<MLPPVector> MLPPTanhReg::data_weights_get() const {
return _weights;
}
void MLPPTanhReg::data_weights_set(const Ref<MLPPVector> &val) {
2023-04-29 18:33:27 +02:00
if (!val.is_valid()) {
return;
}
_weights = val;
}
real_t MLPPTanhReg::data_bias_get() const {
return _bias;
}
void MLPPTanhReg::data_bias_set(const real_t val) {
_bias = val;
}
bool MLPPTanhReg::needs_init() const {
if (!_input_set.is_valid()) {
return true;
}
if (!_output_set.is_valid()) {
return true;
}
int n = _input_set->size().y;
int k = _input_set->size().x;
if (_y_hat->size() != n) {
return true;
}
if (_weights->size() != k) {
return true;
}
return false;
}
void MLPPTanhReg::initialize() {
ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
int n = _input_set->size().y;
int k = _input_set->size().x;
_y_hat->resize(n);
_weights->resize(k);
MLPPUtilities utils;
utils.weight_initializationv(_weights);
_bias = utils.bias_initializationr();
}
2023-02-13 21:10:19 +01:00
Ref<MLPPVector> MLPPTanhReg::model_set_test(const Ref<MLPPMatrix> &X) {
ERR_FAIL_COND_V(needs_init(), Ref<MLPPVector>());
2023-04-28 18:36:21 +02:00
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) {
ERR_FAIL_COND_V(needs_init(), 0);
2023-04-28 18:36:21 +02:00
2023-02-11 01:18:25 +01:00
return evaluatev(x);
}
void MLPPTanhReg::train_gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
ERR_FAIL_COND(needs_init());
2023-04-28 18:36:21 +02:00
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
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
int n = _input_set->size().y;
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-04-28 18:36:21 +02:00
Ref<MLPPVector> error = _y_hat->subn(_output_set);
2023-01-24 19:00:54 +01:00
_weights->sub(_input_set->transposen()->mult_vec(error->hadamard_productn(avn.tanh_derivv(_z)))->scalar_multiplyn(learning_rate / n));
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
_bias -= learning_rate * error->hadamard_productn(avn.tanh_derivv(_z))->sum_elements() / 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;
}
}
}
void MLPPTanhReg::train_sgd(real_t learning_rate, int max_epoch, bool ui) {
ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
ERR_FAIL_COND(needs_init());
int n = _input_set->size().y;
2023-04-28 18:36:21 +02:00
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-11 01:18:25 +01:00
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);
2023-04-29 15:07:30 +02:00
_input_set->row_get_into_mlpp_vector(output_index, input_set_row_tmp);
real_t output_set_entry = _output_set->element_get(output_index);
output_set_row_tmp->element_set(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->element_set(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-04-28 18:36:21 +02:00
_weights->subn(input_set_row_tmp->scalar_multiplyn(learning_rate * error * (1 - y_hat * y_hat)));
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
}
void MLPPTanhReg::train_mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui) {
ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
ERR_FAIL_COND(needs_init());
int n = _input_set->size().y;
2023-04-28 18:36:21 +02:00
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
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
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-04-28 18:36:21 +02:00
Ref<MLPPVector> error = y_hat->subn(current_output_batch_entry);
2023-01-24 19:00:54 +01:00
// Calculating the weight gradients
2023-04-28 18:36:21 +02:00
_weights->sub(current_input_batch_entry->transposen()->mult_vec(error->hadamard_productn(avn.tanh_derivv(z)))->scalar_multiplyn(learning_rate / n));
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
_bias -= learning_rate * error->hadamard_productn(avn.tanh_derivv(_z))->sum_elements() / 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() {
ERR_FAIL_COND_V(!_input_set.is_valid() || !_output_set.is_valid(), 0);
ERR_FAIL_COND_V(needs_init(), 0);
2023-04-28 18:36:21 +02:00
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
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;
_reg = p_reg;
_lambda = p_lambda;
_alpha = p_alpha;
_bias = 0;
_z.instance();
2023-02-13 21:10:19 +01:00
_y_hat.instance();
_weights.instance();
2023-04-28 18:36:21 +02:00
initialize();
2023-02-11 01:18:25 +01:00
}
MLPPTanhReg::MLPPTanhReg() {
_reg = MLPPReg::REGULARIZATION_TYPE_NONE;
_lambda = 0;
_alpha = 0;
_bias = 0;
2023-04-28 18:36:21 +02:00
_z.instance();
2023-04-28 18:36:21 +02:00
_y_hat.instance();
_weights.instance();
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-24 19:23:30 +01:00
MLPPActivation avn;
2023-02-13 21:10:19 +01:00
2023-04-28 18:36:21 +02:00
return avn.tanh_normr(_weights->dot(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-04-28 18:36:21 +02:00
return _weights->dot(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-24 19:23:30 +01:00
MLPPActivation avn;
2023-02-13 21:10:19 +01:00
2023-04-28 18:36:21 +02:00
return avn.tanh_normv(X->mult_vec(_weights)->scalar_addn(_bias));
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-04-28 18:36:21 +02:00
return X->mult_vec(_weights)->scalar_addn(_bias);
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");
2023-04-29 18:33:27 +02:00
ADD_GROUP("Data", "data");
ClassDB::bind_method(D_METHOD("data_z_get"), &MLPPTanhReg::data_z_get);
ClassDB::bind_method(D_METHOD("data_z_set", "val"), &MLPPTanhReg::set_output_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data_z", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "data_z_set", "data_z_get");
2023-02-11 01:18:25 +01:00
ClassDB::bind_method(D_METHOD("data_y_hat_get"), &MLPPTanhReg::data_y_hat_get);
ClassDB::bind_method(D_METHOD("data_y_hat_set", "val"), &MLPPTanhReg::data_y_hat_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data_y_hat", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "data_y_hat_set", "data_y_hat_get");
2023-02-11 01:18:25 +01:00
ClassDB::bind_method(D_METHOD("data_weights_get"), &MLPPTanhReg::data_weights_get);
ClassDB::bind_method(D_METHOD("data_weights_set", "val"), &MLPPTanhReg::data_weights_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data_weights", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "data_weights_set", "data_weights_get");
2023-02-11 01:18:25 +01:00
ClassDB::bind_method(D_METHOD("data_bias_get"), &MLPPTanhReg::data_bias_get);
ClassDB::bind_method(D_METHOD("data_bias_set", "val"), &MLPPTanhReg::data_bias_set);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "data_bias"), "data_bias_set", "data_bias_get");
2023-02-11 01:18:25 +01:00
ClassDB::bind_method(D_METHOD("needs_init"), &MLPPTanhReg::needs_init);
2023-02-11 01:18:25 +01:00
ClassDB::bind_method(D_METHOD("initialize"), &MLPPTanhReg::initialize);
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("train_gradient_descent", "learning_rate", "max_epoch", "ui"), &MLPPTanhReg::train_gradient_descent, false);
ClassDB::bind_method(D_METHOD("train_sgd", "learning_rate", "max_epoch", "ui"), &MLPPTanhReg::train_sgd, false);
ClassDB::bind_method(D_METHOD("train_mbgd", "learning_rate", "max_epoch", "mini_batch_size", "ui"), &MLPPTanhReg::train_mbgd, false);
ClassDB::bind_method(D_METHOD("score"), &MLPPTanhReg::score);
2023-01-24 19:00:54 +01:00
}