pmlpp/mlpp/softmax_reg/softmax_reg.cpp

403 lines
12 KiB
C++
Raw Normal View History

//
// SoftmaxReg.cpp
//
// Created by Marc Melikyan on 10/2/20.
//
2023-01-24 18:12:23 +01:00
#include "softmax_reg.h"
2023-02-10 19:31:54 +01:00
2023-01-24 19:00:54 +01:00
#include "../activation/activation.h"
#include "../cost/cost.h"
2023-01-24 18:12:23 +01:00
#include "../regularization/reg.h"
#include "../utilities/utilities.h"
#include <random>
2023-04-28 21:23:37 +02:00
Ref<MLPPMatrix> MLPPSoftmaxReg::get_input_set() const {
2023-02-10 19:31:54 +01:00
return _input_set;
}
void MLPPSoftmaxReg::set_input_set(const Ref<MLPPMatrix> &val) {
_input_set = val;
}
2023-04-28 21:23:37 +02:00
Ref<MLPPMatrix> MLPPSoftmaxReg::get_output_set() const {
2023-02-10 19:31:54 +01:00
return _output_set;
}
void MLPPSoftmaxReg::set_output_set(const Ref<MLPPMatrix> &val) {
_output_set = val;
}
2023-04-28 21:23:37 +02:00
MLPPReg::RegularizationType MLPPSoftmaxReg::get_reg() const {
2023-02-10 19:31:54 +01:00
return _reg;
2023-01-24 19:00:54 +01:00
}
2023-02-10 19:31:54 +01:00
void MLPPSoftmaxReg::set_reg(const MLPPReg::RegularizationType val) {
_reg = val;
2023-01-24 19:00:54 +01:00
}
2023-04-28 21:23:37 +02:00
real_t MLPPSoftmaxReg::get_lambda() const {
2023-02-10 19:31:54 +01:00
return _lambda;
2023-01-24 19:00:54 +01:00
}
2023-02-10 19:31:54 +01:00
void MLPPSoftmaxReg::set_lambda(const real_t val) {
_lambda = val;
}
2023-04-28 21:23:37 +02:00
real_t MLPPSoftmaxReg::get_alpha() const {
2023-02-10 19:31:54 +01:00
return _alpha;
}
void MLPPSoftmaxReg::set_alpha(const real_t val) {
_alpha = val;
2023-04-28 21:23:37 +02:00
}
Ref<MLPPMatrix> MLPPSoftmaxReg::data_y_hat_get() const {
return _y_hat;
}
void MLPPSoftmaxReg::data_y_hat_set(const Ref<MLPPMatrix> &val) {
_y_hat = val;
}
2023-02-10 19:31:54 +01:00
2023-04-28 21:23:37 +02:00
Ref<MLPPMatrix> MLPPSoftmaxReg::data_weights_get() const {
return _weights;
}
void MLPPSoftmaxReg::data_weights_set(const Ref<MLPPMatrix> &val) {
_weights = val;
}
Ref<MLPPVector> MLPPSoftmaxReg::data_bias_get() const {
return _bias;
}
void MLPPSoftmaxReg::data_bias_set(const Ref<MLPPVector> &val) {
_bias = val;
2023-02-10 19:31:54 +01:00
}
Ref<MLPPVector> MLPPSoftmaxReg::model_test(const Ref<MLPPVector> &x) {
2023-04-28 21:23:37 +02:00
ERR_FAIL_COND_V(!_input_set.is_valid() || !_output_set.is_valid(), Ref<MLPPVector>());
ERR_FAIL_COND_V(needs_init(), Ref<MLPPVector>());
2023-02-10 19:31:54 +01:00
return evaluatev(x);
}
Ref<MLPPMatrix> MLPPSoftmaxReg::model_set_test(const Ref<MLPPMatrix> &X) {
2023-04-28 21:23:37 +02:00
ERR_FAIL_COND_V(!_input_set.is_valid() || !_output_set.is_valid(), Ref<MLPPVector>());
ERR_FAIL_COND_V(needs_init(), Ref<MLPPMatrix>());
2023-02-10 19:31:54 +01:00
return evaluatem(X);
}
2023-04-28 21:23:37 +02:00
void MLPPSoftmaxReg::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-01-24 19:00:54 +01:00
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
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 19:31:54 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-10 19:31:54 +01:00
cost_prev = cost(_y_hat, _output_set);
2023-04-28 21:07:35 +02:00
Ref<MLPPMatrix> error = _y_hat->subn(_output_set);
2023-01-24 19:00:54 +01:00
//Calculating the weight gradients
2023-04-28 21:07:35 +02:00
Ref<MLPPMatrix> w_gradient = _input_set->transposen()->multn(error);
2023-01-24 19:00:54 +01:00
//Weight updation
2023-04-28 21:07:35 +02:00
_weights->sub(w_gradient->scalar_multiplyn(learning_rate));
2023-02-10 19:31:54 +01:00
_weights = regularization.reg_weightsm(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradients
2023-01-27 13:01:16 +01:00
//real_t b_gradient = alg.sum_elements(error);
2023-01-24 19:00:54 +01:00
// Bias Updation
2023-04-28 21:07:35 +02:00
_bias->subtract_matrix_rows(error->scalar_multiplyn(learning_rate));
2023-01-24 19:00:54 +01:00
2023-02-10 19:31:54 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
// UI PORTION
2023-02-10 19:31:54 +01:00
if (ui) {
MLPPUtilities::cost_info(epoch, cost_prev, cost(_y_hat, _output_set));
MLPPUtilities::print_ui_mb(_weights, _bias);
2023-01-24 19:00:54 +01:00
}
2023-02-10 19:31:54 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
}
2023-04-28 21:23:37 +02:00
void MLPPSoftmaxReg::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());
2023-02-10 19:31:54 +01:00
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-10 19:31:54 +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-04-28 21:23:37 +02:00
int n = _input_set->size().y;
2023-01-24 19:00:54 +01:00
2023-02-10 19:31:54 +01:00
std::random_device rd;
std::default_random_engine generator(rd());
2023-04-28 21:23:37 +02:00
std::uniform_int_distribution<int> distribution(0, int(n - 1));
2023-02-10 19:31:54 +01:00
Ref<MLPPVector> input_set_row_tmp;
input_set_row_tmp.instance();
input_set_row_tmp->resize(_input_set->size().x);
Ref<MLPPMatrix> y_hat_matrix_tmp;
y_hat_matrix_tmp.instance();
//y_hat_matrix_tmp->resize(Size2i(_input_set->size().y, 1));
Ref<MLPPVector> output_set_row_tmp;
output_set_row_tmp.instance();
output_set_row_tmp->resize(_output_set->size().x);
Ref<MLPPMatrix> output_set_row_matrix_tmp;
output_set_row_matrix_tmp.instance();
output_set_row_matrix_tmp->resize(Size2i(_output_set->size().x, 1));
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-10 19:31:54 +01:00
real_t output_index = distribution(generator);
_input_set->get_row_into_mlpp_vector(output_index, input_set_row_tmp);
Ref<MLPPVector> y_hat = evaluatev(input_set_row_tmp);
y_hat_matrix_tmp->resize(Size2i(y_hat->size(), 1));
y_hat_matrix_tmp->set_row_mlpp_vector(0, y_hat);
_output_set->get_row_into_mlpp_vector(output_index, output_set_row_tmp);
output_set_row_matrix_tmp->set_row_mlpp_vector(0, output_set_row_tmp);
2023-01-24 19:00:54 +01:00
2023-02-10 19:31:54 +01:00
cost_prev = cost(y_hat_matrix_tmp, output_set_row_matrix_tmp);
2023-01-24 19:00:54 +01:00
// Calculating the weight gradients
2023-04-28 21:07:35 +02:00
Ref<MLPPMatrix> w_gradient = input_set_row_tmp->outer_product(y_hat->subn(output_set_row_tmp));
2023-01-24 19:00:54 +01:00
// Weight Updation
2023-04-28 21:07:35 +02:00
_weights->sub(w_gradient->scalar_multiplyn(learning_rate));
2023-02-10 19:31:54 +01:00
_weights = regularization.reg_weightsm(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradients
2023-04-28 21:07:35 +02:00
Ref<MLPPVector> b_gradient = y_hat->subn(output_set_row_tmp);
2023-01-24 19:00:54 +01:00
// Bias updation
2023-04-28 21:07:35 +02:00
_bias->sub(b_gradient->scalar_multiplyn(learning_rate));
2023-01-24 19:00:54 +01:00
2023-02-10 19:31:54 +01:00
y_hat = evaluatev(output_set_row_tmp);
2023-01-24 19:00:54 +01:00
2023-02-10 19:31:54 +01:00
if (ui) {
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat_matrix_tmp, output_set_row_matrix_tmp));
MLPPUtilities::print_ui_mb(_weights, _bias);
2023-01-24 19:00:54 +01:00
}
2023-02-10 19:31:54 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
2023-02-10 19:31:54 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-04-28 21:23:37 +02:00
void MLPPSoftmaxReg::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());
2023-02-10 19:31:54 +01:00
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
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-04-28 21:23:37 +02:00
int n = _input_set->size().y;
2023-01-24 19:00:54 +01:00
// Creating the mini-batches
2023-04-28 21:23:37 +02:00
int n_mini_batch = n / mini_batch_size;
2023-02-10 19:31:54 +01:00
MLPPUtilities::CreateMiniBatchMMBatch batches = MLPPUtilities::create_mini_batchesmm(_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-10 19:31:54 +01:00
Ref<MLPPMatrix> current_inputs = batches.input_sets[i];
Ref<MLPPMatrix> current_outputs = batches.output_sets[i];
2023-01-24 19:00:54 +01:00
2023-02-10 19:31:54 +01:00
Ref<MLPPMatrix> y_hat = evaluatem(current_inputs);
cost_prev = cost(y_hat, current_outputs);
2023-04-28 21:07:35 +02:00
Ref<MLPPMatrix> error = y_hat->subn(current_outputs);
2023-01-24 19:00:54 +01:00
// Calculating the weight gradients
2023-04-28 21:07:35 +02:00
Ref<MLPPMatrix> w_gradient = current_inputs->transposen()->multn(error);
2023-01-24 19:00:54 +01:00
//Weight updation
2023-04-28 21:07:35 +02:00
_weights->sub(w_gradient->scalar_multiplyn(learning_rate));
2023-02-10 19:31:54 +01:00
_weights = regularization.reg_weightsm(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradients
2023-04-28 21:07:35 +02:00
_bias->subtract_matrix_rows(error->scalar_multiplyn(learning_rate));
2023-02-10 19:31:54 +01:00
y_hat = evaluatem(current_inputs);
2023-01-24 19:00:54 +01:00
2023-02-10 19:31:54 +01:00
if (ui) {
MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, current_outputs));
MLPPUtilities::print_ui_mb(_weights, _bias);
2023-01-24 19:00:54 +01:00
}
}
2023-02-10 19:31:54 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-10 19:31:54 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-10 19:31:54 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-01-27 13:01:16 +01:00
real_t MLPPSoftmaxReg::score() {
2023-04-28 21:23:37 +02:00
ERR_FAIL_COND_V(!_input_set.is_valid() || !_output_set.is_valid(), 0);
ERR_FAIL_COND_V(needs_init(), 0);
2023-02-10 19:31:54 +01:00
2023-02-10 14:15:49 +01:00
MLPPUtilities util;
2023-02-10 19:31:54 +01:00
return util.performance_mat(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
}
2023-04-28 21:23:37 +02:00
bool MLPPSoftmaxReg::needs_init() const {
if (!_input_set.is_valid()) {
return true;
}
2023-02-10 19:31:54 +01:00
2023-04-28 21:23:37 +02:00
if (!_output_set.is_valid()) {
return true;
}
2023-02-10 19:31:54 +01:00
2023-04-28 21:23:37 +02:00
int n = _input_set->size().y;
int k = _input_set->size().x;
int n_class = _output_set->size().x;
2023-01-24 19:00:54 +01:00
2023-04-28 21:23:37 +02:00
if (_y_hat->size().x != n) {
return true;
2023-02-10 19:31:54 +01:00
}
2023-04-28 21:23:37 +02:00
if (_weights->size() != Size2i(n_class, k)) {
return true;
}
if (_bias->size() != n_class) {
return true;
}
return false;
}
void MLPPSoftmaxReg::initialize() {
2023-02-10 19:31:54 +01:00
ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
2023-04-28 21:23:37 +02:00
int n = _input_set->size().y;
int k = _input_set->size().x;
int n_class = _output_set->size().x;
2023-02-10 19:31:54 +01:00
2023-04-28 21:23:37 +02:00
_y_hat->resize(Size2i(n, 0));
2023-02-10 19:31:54 +01:00
MLPPUtilities util;
2023-04-28 21:23:37 +02:00
_weights->resize(Size2i(n_class, k));
_bias->resize(n_class);
2023-02-10 19:31:54 +01:00
util.weight_initializationm(_weights);
util.bias_initializationv(_bias);
}
MLPPSoftmaxReg::MLPPSoftmaxReg(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPMatrix> &p_output_set, MLPPReg::RegularizationType p_reg, real_t p_lambda, real_t p_alpha) {
_input_set = p_input_set;
_output_set = p_output_set;
_reg = p_reg;
_lambda = p_lambda;
_alpha = p_alpha;
2023-04-28 21:23:37 +02:00
_y_hat.instance();
_weights.instance();
_bias.instance();
2023-02-10 19:31:54 +01:00
}
MLPPSoftmaxReg::MLPPSoftmaxReg() {
// Regularization Params
_reg = MLPPReg::REGULARIZATION_TYPE_NONE;
_lambda = 0.5;
_alpha = 0.5; /* This is the controlling param for Elastic Net*/
2023-04-28 21:23:37 +02:00
_y_hat.instance();
_weights.instance();
_bias.instance();
2023-02-10 19:31:54 +01:00
}
MLPPSoftmaxReg::~MLPPSoftmaxReg() {
}
real_t MLPPSoftmaxReg::cost(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-01-24 19:37:08 +01:00
class MLPPCost cost;
2023-02-10 19:31:54 +01:00
return cost.cross_entropym(y_hat, y) + regularization.reg_termm(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
}
2023-02-10 19:31:54 +01:00
Ref<MLPPVector> MLPPSoftmaxReg::evaluatev(const Ref<MLPPVector> &x) {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-04-28 21:07:35 +02:00
return avn.softmax_normv(_bias->addn(_weights->transposen()->mult_vec(x)));
2023-01-24 19:00:54 +01:00
}
2023-02-10 19:31:54 +01:00
Ref<MLPPMatrix> MLPPSoftmaxReg::evaluatem(const Ref<MLPPMatrix> &X) {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-24 19:00:54 +01:00
2023-04-28 21:07:35 +02:00
return avn.softmax_normm(X->multn(_weights)->add_vecn(_bias));
2023-01-24 19:00:54 +01:00
}
// softmax ( wTx + b )
2023-02-10 19:31:54 +01:00
void MLPPSoftmaxReg::forward_pass() {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-24 19:00:54 +01:00
2023-04-28 21:07:35 +02:00
_y_hat = avn.softmax_normm(_input_set->multn(_weights)->add_vecn(_bias));
2023-02-10 19:31:54 +01:00
}
void MLPPSoftmaxReg::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPSoftmaxReg::get_input_set);
ClassDB::bind_method(D_METHOD("set_input_set", "val"), &MLPPSoftmaxReg::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"), &MLPPSoftmaxReg::get_output_set);
ClassDB::bind_method(D_METHOD("set_output_set", "val"), &MLPPSoftmaxReg::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"), &MLPPSoftmaxReg::get_reg);
ClassDB::bind_method(D_METHOD("set_reg", "val"), &MLPPSoftmaxReg::set_reg);
ADD_PROPERTY(PropertyInfo(Variant::INT, "reg"), "set_reg", "get_reg");
ClassDB::bind_method(D_METHOD("get_lambda"), &MLPPSoftmaxReg::get_lambda);
ClassDB::bind_method(D_METHOD("set_lambda", "val"), &MLPPSoftmaxReg::set_lambda);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "lambda"), "set_lambda", "get_lambda");
ClassDB::bind_method(D_METHOD("get_alpha"), &MLPPSoftmaxReg::get_alpha);
ClassDB::bind_method(D_METHOD("set_alpha", "val"), &MLPPSoftmaxReg::set_alpha);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "alpha"), "set_alpha", "get_alpha");
2023-04-28 21:23:37 +02:00
ClassDB::bind_method(D_METHOD("data_y_hat_get"), &MLPPSoftmaxReg::data_y_hat_get);
ClassDB::bind_method(D_METHOD("data_y_hat_set", "val"), &MLPPSoftmaxReg::data_y_hat_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data_y_hat", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "data_y_hat_set", "data_y_hat_get");
ClassDB::bind_method(D_METHOD("data_weights_get"), &MLPPSoftmaxReg::data_weights_get);
ClassDB::bind_method(D_METHOD("data_weights_set", "val"), &MLPPSoftmaxReg::data_weights_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data_weights", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "data_weights_set", "data_weights_get");
ClassDB::bind_method(D_METHOD("data_bias_get"), &MLPPSoftmaxReg::data_bias_get);
ClassDB::bind_method(D_METHOD("data_bias_set", "val"), &MLPPSoftmaxReg::data_bias_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data_bias", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "data_bias_set", "data_bias_get");
2023-02-10 19:31:54 +01:00
ClassDB::bind_method(D_METHOD("model_test", "x"), &MLPPSoftmaxReg::model_test);
ClassDB::bind_method(D_METHOD("model_set_test", "X"), &MLPPSoftmaxReg::model_set_test);
2023-04-28 21:23:37 +02:00
ClassDB::bind_method(D_METHOD("train_gradient_descent", "learning_rate", "max_epoch", "ui"), &MLPPSoftmaxReg::train_gradient_descent, false);
ClassDB::bind_method(D_METHOD("train_sgd", "learning_rate", "max_epoch", "ui"), &MLPPSoftmaxReg::train_sgd, false);
ClassDB::bind_method(D_METHOD("train_mbgd", "learning_rate", "max_epoch", "mini_batch_size", "ui"), &MLPPSoftmaxReg::train_mbgd, false);
2023-02-10 19:31:54 +01:00
ClassDB::bind_method(D_METHOD("score"), &MLPPSoftmaxReg::score);
2023-04-28 21:23:37 +02:00
ClassDB::bind_method(D_METHOD("needs_init"), &MLPPSoftmaxReg::needs_init);
2023-02-10 19:31:54 +01:00
ClassDB::bind_method(D_METHOD("initialize"), &MLPPSoftmaxReg::initialize);
2023-01-24 19:00:54 +01:00
}