pmlpp/log_reg/log_reg.cpp

397 lines
12 KiB
C++
Raw Normal View History

2023-12-30 00:41:59 +01:00
/*************************************************************************/
/* log_reg.cpp */
/*************************************************************************/
/* This file is part of: */
/* PMLPP Machine Learning Library */
/* https://github.com/Relintai/pmlpp */
/*************************************************************************/
2023-12-30 00:43:39 +01:00
/* Copyright (c) 2023-present Péter Magyar. */
2023-12-30 00:41:59 +01:00
/* Copyright (c) 2022-2023 Marc Melikyan */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2023-01-24 18:12:23 +01:00
#include "log_reg.h"
2023-02-11 10:18:21 +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 <iostream>
#include <random>
2023-02-11 10:18:21 +01:00
/*
Ref<MLPPMatrix> MLPPLogReg::get_input_set() {
return _input_set;
}
void MLPPLogReg::set_input_set(const Ref<MLPPMatrix> &val) {
_input_set = val;
_initialized = false;
}
Ref<MLPPVector> MLPPLogReg::get_output_set() {
return _output_set;
}
void MLPPLogReg::set_output_set(const Ref<MLPPVector> &val) {
_output_set = val;
_initialized = false;
}
MLPPReg::RegularizationType MLPPLogReg::get_reg() {
return _reg;
}
void MLPPLogReg::set_reg(const MLPPReg::RegularizationType val) {
_reg = val;
_initialized = false;
}
real_t MLPPLogReg::get_lambda() {
return _lambda;
}
void MLPPLogReg::set_lambda(const real_t val) {
_lambda = val;
_initialized = false;
}
real_t MLPPLogReg::get_alpha() {
return _alpha;
}
void MLPPLogReg::set_alpha(const real_t val) {
_alpha = val;
2023-01-24 19:20:18 +01:00
2023-02-11 10:18:21 +01:00
_initialized = false;
2023-01-24 19:00:54 +01:00
}
2023-02-11 10:18:21 +01:00
*/
2023-02-14 13:34:28 +01:00
Ref<MLPPVector> MLPPLogReg::model_set_test(const Ref<MLPPMatrix> &X) {
ERR_FAIL_COND_V(!_initialized, Ref<MLPPVector>());
2023-01-24 19:00:54 +01:00
2023-02-11 10:18:21 +01:00
return evaluatem(X);
2023-01-24 19:00:54 +01:00
}
2023-02-14 13:34:28 +01:00
real_t MLPPLogReg::model_test(const Ref<MLPPVector> &x) {
2023-02-11 10:18:21 +01:00
ERR_FAIL_COND_V(!_initialized, 0);
return evaluatev(x);
2023-01-24 19:00:54 +01:00
}
2023-02-11 10:18:21 +01:00
void MLPPLogReg::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
ERR_FAIL_COND(!_initialized);
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-11 10:18:21 +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 10:18:21 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-11 10:18:21 +01:00
cost_prev = cost(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
Ref<MLPPVector> error = _y_hat->subn(_output_set);
2023-01-24 19:00:54 +01:00
// Calculating the weight gradients
_weights->sub(_input_set->transposen()->mult_vec(error)->scalar_multiplyn(learning_rate / _n));
2023-02-14 13:34:28 +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->sum_elements() / _n;
2023-02-14 13:34:28 +01:00
2023-02-11 10:18:21 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
2023-02-11 10:18:21 +01:00
if (ui) {
2023-02-14 13:34:28 +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 10:18:21 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
}
2023-02-11 10:18:21 +01:00
void MLPPLogReg::mle(real_t learning_rate, int max_epoch, bool ui) {
ERR_FAIL_COND(!_initialized);
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-11 10:18:21 +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 10:18:21 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-11 10:18:21 +01:00
cost_prev = cost(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
Ref<MLPPVector> error = _output_set->subn(_y_hat);
2023-01-24 19:00:54 +01:00
// Calculating the weight gradients
_weights->add(_input_set->transposen()->mult_vec(error)->scalar_multiplyn(learning_rate / _n));
2023-02-14 13:34:28 +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->sum_elements() / _n;
2023-02-11 10:18:21 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
2023-02-11 10:18:21 +01:00
if (ui) {
2023-02-14 13:34:28 +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 10:18:21 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-11 10:18:21 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
}
2023-02-11 10:18:21 +01:00
void MLPPLogReg::sgd(real_t learning_rate, int max_epoch, bool ui) {
ERR_FAIL_COND(!_initialized);
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-11 10:18:21 +01:00
std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_int_distribution<int> distribution(0, int(_n - 1));
2023-02-14 13:34:28 +01:00
Ref<MLPPVector> input_row_tmp;
input_row_tmp.instance();
input_row_tmp->resize(_input_set->size().x);
Ref<MLPPVector> y_hat_tmp;
y_hat_tmp.instance();
y_hat_tmp->resize(1);
Ref<MLPPVector> output_element_set_tmp;
output_element_set_tmp.instance();
output_element_set_tmp->resize(1);
2023-02-14 13:34:28 +01:00
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-14 13:34:28 +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_row_tmp);
real_t output_element_set = _output_set->element_get(output_index);
output_element_set_tmp->element_set(0, output_element_set);
2023-02-14 13:34:28 +01:00
real_t y_hat = evaluatev(input_row_tmp);
y_hat_tmp->element_set(0, y_hat);
2023-01-24 19:00:54 +01:00
cost_prev = cost(y_hat_tmp, output_element_set_tmp);
2023-01-24 19:00:54 +01:00
real_t error = y_hat - output_element_set;
2023-01-24 19:00:54 +01:00
// Weight updation
_weights->sub(input_row_tmp->scalar_multiplyn(learning_rate * error));
2023-02-14 13:34:28 +01:00
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Bias updation
2023-02-11 10:18:21 +01:00
_bias -= learning_rate * error;
2023-01-24 19:00:54 +01:00
2023-02-14 13:34:28 +01:00
y_hat = evaluatev(input_row_tmp);
2023-01-24 19:00:54 +01:00
2023-02-11 10:18:21 +01:00
if (ui) {
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat_tmp, output_element_set_tmp));
2023-02-14 13:34:28 +01:00
MLPPUtilities::print_ui_vb(_weights, _bias);
2023-01-24 19:00:54 +01:00
}
2023-02-11 10:18:21 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
2023-02-11 10:18:21 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-11 10:18:21 +01:00
void MLPPLogReg::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) {
ERR_FAIL_COND(!_initialized);
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;
// Creating the mini-batches
2023-02-11 10:18:21 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-02-14 13:34:28 +01:00
MLPPUtilities::CreateMiniBatchMVBatch bacthes = 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-14 13:34:28 +01:00
Ref<MLPPMatrix> current_mini_batch_input_entry = bacthes.input_sets[i];
Ref<MLPPVector> current_mini_batch_output_entry = bacthes.output_sets[i];
2023-01-24 19:00:54 +01:00
2023-02-14 13:34:28 +01:00
Ref<MLPPVector> y_hat = evaluatem(current_mini_batch_input_entry);
cost_prev = cost(y_hat, current_mini_batch_output_entry);
Ref<MLPPVector> error = y_hat->subn(current_mini_batch_output_entry);
2023-01-24 19:00:54 +01:00
// Calculating the weight gradients
_weights->sub(current_mini_batch_input_entry->transposen()->mult_vec(error)->scalar_multiplyn(learning_rate / current_mini_batch_output_entry->size()));
2023-02-14 13:34:28 +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->sum_elements() / current_mini_batch_output_entry->size();
2023-02-14 13:34:28 +01:00
y_hat = evaluatem(current_mini_batch_input_entry);
2023-01-24 19:00:54 +01:00
if (UI) {
2023-02-14 13:34:28 +01:00
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, current_mini_batch_output_entry));
MLPPUtilities::print_ui_vb(_weights, _bias);
2023-01-24 19:00:54 +01:00
}
}
2023-02-11 10:18:21 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-11 10:18:21 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-11 10:18:21 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-01-27 13:01:16 +01:00
real_t MLPPLogReg::score() {
2023-02-11 10:18:21 +01:00
ERR_FAIL_COND_V(!_initialized, 0);
2023-02-10 21:44:27 +01:00
MLPPUtilities util;
2023-02-14 13:34:28 +01:00
return util.performance_vec(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
}
2023-02-11 10:18:21 +01:00
void MLPPLogReg::save(std::string file_name) {
2023-02-14 13:34:28 +01:00
//ERR_FAIL_COND(!_initialized);
2023-02-11 10:18:21 +01:00
2023-02-14 13:34:28 +01:00
//MLPPUtilities util;
//util.saveParameters(file_name, _weights, _bias);
2023-02-11 10:18:21 +01:00
}
bool MLPPLogReg::is_initialized() {
return _initialized;
2023-01-24 19:00:54 +01:00
}
2023-02-11 10:18:21 +01:00
void MLPPLogReg::initialize() {
if (_initialized) {
return;
}
2023-01-24 19:00:54 +01:00
2023-02-11 10:18:21 +01:00
//ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
_initialized = true;
}
2023-02-14 14:17:28 +01:00
MLPPLogReg::MLPPLogReg(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 10:18:21 +01:00
_input_set = p_input_set;
_output_set = p_output_set;
2023-02-14 13:34:28 +01:00
_n = p_input_set->size().y;
_k = p_input_set->size().x;
2023-02-11 10:18:21 +01:00
_reg = p_reg;
_lambda = p_lambda;
_alpha = p_alpha;
2023-02-14 13:34:28 +01:00
_y_hat.instance();
_y_hat->resize(_n);
_weights.instance();
_weights->resize(_k);
MLPPUtilities utils;
utils.weight_initializationv(_weights);
_bias = utils.bias_initializationr();
2023-02-11 10:18:21 +01:00
_initialized = true;
}
MLPPLogReg::MLPPLogReg() {
_initialized = false;
}
MLPPLogReg::~MLPPLogReg() {
}
2023-02-14 13:34:28 +01:00
real_t MLPPLogReg::cost(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-01-24 19:37:08 +01:00
class MLPPCost cost;
2023-02-11 10:18:21 +01:00
2023-02-14 13:34:28 +01:00
return cost.log_lossv(y_hat, y) + regularization.reg_termv(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
}
2023-02-14 13:34:28 +01:00
real_t MLPPLogReg::evaluatev(const Ref<MLPPVector> &x) {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-02-11 10:18:21 +01:00
return avn.sigmoid_normr(_weights->dot(x) + _bias);
2023-01-24 19:00:54 +01:00
}
2023-02-14 13:34:28 +01:00
Ref<MLPPVector> MLPPLogReg::evaluatem(const Ref<MLPPMatrix> &X) {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-02-11 10:18:21 +01:00
return avn.sigmoid_normv(X->mult_vec(_weights)->scalar_addn(_bias));
2023-01-24 19:00:54 +01:00
}
// sigmoid ( wTx + b )
2023-02-11 10:18:21 +01:00
void MLPPLogReg::forward_pass() {
_y_hat = evaluatem(_input_set);
}
void MLPPLogReg::_bind_methods() {
/*
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPLogReg::get_input_set);
ClassDB::bind_method(D_METHOD("set_input_set", "val"), &MLPPLogReg::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"), &MLPPLogReg::get_output_set);
ClassDB::bind_method(D_METHOD("set_output_set", "val"), &MLPPLogReg::set_output_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "output_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "set_output_set", "get_output_set");
ClassDB::bind_method(D_METHOD("get_reg"), &MLPPLogReg::get_reg);
ClassDB::bind_method(D_METHOD("set_reg", "val"), &MLPPLogReg::set_reg);
ADD_PROPERTY(PropertyInfo(Variant::INT, "reg"), "set_reg", "get_reg");
ClassDB::bind_method(D_METHOD("get_lambda"), &MLPPLogReg::get_lambda);
ClassDB::bind_method(D_METHOD("set_lambda", "val"), &MLPPLogReg::set_lambda);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "lambda"), "set_lambda", "get_lambda");
ClassDB::bind_method(D_METHOD("get_alpha"), &MLPPLogReg::get_alpha);
ClassDB::bind_method(D_METHOD("set_alpha", "val"), &MLPPLogReg::set_alpha);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "alpha"), "set_alpha", "get_alpha");
ClassDB::bind_method(D_METHOD("model_test", "x"), &MLPPLogReg::model_test);
ClassDB::bind_method(D_METHOD("model_set_test", "X"), &MLPPLogReg::model_set_test);
ClassDB::bind_method(D_METHOD("gradient_descent", "learning_rate", "max_epoch", "ui"), &MLPPLogReg::gradient_descent, false);
ClassDB::bind_method(D_METHOD("sgd", "learning_rate", "max_epoch", "ui"), &MLPPLogReg::sgd, false);
ClassDB::bind_method(D_METHOD("mbgd", "learning_rate", "max_epoch", "mini_batch_size", "ui"), &MLPPLogReg::mbgd, false);
ClassDB::bind_method(D_METHOD("score"), &MLPPLogReg::score);
ClassDB::bind_method(D_METHOD("save", "file_name"), &MLPPLogReg::save);
ClassDB::bind_method(D_METHOD("is_initialized"), &MLPPLogReg::is_initialized);
ClassDB::bind_method(D_METHOD("initialize"), &MLPPLogReg::initialize);
*/
2023-01-24 19:00:54 +01:00
}