pmlpp/exp_reg/exp_reg.cpp

333 lines
10 KiB
C++
Raw Permalink Normal View History

2023-12-30 00:41:59 +01:00
/*************************************************************************/
/* exp_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 "exp_reg.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"
2023-01-24 19:00:54 +01:00
#include "../stat/stat.h"
2023-01-24 18:12:23 +01:00
#include "../utilities/utilities.h"
#include <iostream>
#include <random>
2023-02-16 18:43:35 +01:00
Ref<MLPPVector> MLPPExpReg::model_set_test(const Ref<MLPPMatrix> &X) {
2023-02-12 10:52:46 +01:00
return evaluatem(X);
2023-01-24 19:00:54 +01:00
}
2023-02-16 18:43:35 +01:00
real_t MLPPExpReg::model_test(const Ref<MLPPVector> &x) {
2023-02-12 10:52:46 +01:00
return evaluatev(x);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
void MLPPExpReg::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-12 10:52:46 +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-12 10:52:46 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-12 10:52:46 +01:00
cost_prev = cost(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
2023-04-30 18:46:53 +02:00
Ref<MLPPVector> error = _y_hat->subn(_output_set);
2023-01-24 19:00:54 +01:00
2023-02-12 10:52:46 +01:00
for (int i = 0; i < _k; i++) {
2023-01-24 19:00:54 +01:00
// Calculating the weight gradient
2023-01-27 13:01:16 +01:00
real_t sum = 0;
2023-02-12 10:52:46 +01:00
for (int j = 0; j < _n; j++) {
sum += error->element_get(j) * _input_set->element_get(j, i) * Math::pow(_weights->element_get(i), _input_set->element_get(j, i) - 1);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
real_t w_gradient = sum / _n;
2023-01-24 19:00:54 +01:00
// Calculating the initial gradient
2023-01-27 13:01:16 +01:00
real_t sum2 = 0;
2023-02-12 10:52:46 +01:00
for (int j = 0; j < _n; j++) {
sum2 += error->element_get(j) * Math::pow(_weights->element_get(i), _input_set->element_get(j, i));
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
real_t i_gradient = sum2 / _n;
2023-01-24 19:00:54 +01:00
// Weight/initial updation
_weights->element_set(i, _weights->element_get(i) - learning_rate * w_gradient);
_initial->element_set(i, _initial->element_get(i) - learning_rate * i_gradient);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
2023-02-16 18:43:35 +01:00
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradient
2023-01-27 13:01:16 +01:00
real_t sum = 0;
2023-02-12 10:52:46 +01:00
for (int j = 0; j < _n; j++) {
sum += (_y_hat->element_get(j) - _output_set->element_get(j));
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
real_t b_gradient = sum / _n;
2023-01-24 19:00:54 +01:00
// bias updation
2023-02-12 10:52:46 +01:00
_bias -= learning_rate * b_gradient;
2023-01-24 19:00:54 +01:00
2023-02-12 10:52:46 +01:00
forward_pass();
if (ui) {
2023-02-16 18:43:35 +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-12 10:52:46 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
}
2023-02-12 10:52:46 +01:00
void MLPPExpReg::sgd(real_t learning_rate, int max_epoch, bool ui) {
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-12 10:52:46 +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-12 10:52:46 +01:00
std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_int_distribution<int> distribution(0, int(_n - 1));
2023-02-16 18:43:35 +01:00
Ref<MLPPVector> input_set_row_tmp;
input_set_row_tmp.instance();
input_set_row_tmp->resize(_input_set->size().x);
Ref<MLPPVector> y_hat_row_tmp;
y_hat_row_tmp.instance();
y_hat_row_tmp->resize(1);
Ref<MLPPVector> output_set_row_tmp;
output_set_row_tmp.instance();
output_set_row_tmp->resize(1);
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-12 10:52:46 +01:00
int output_index = distribution(generator);
2023-01-24 19:00:54 +01:00
2023-04-29 15:07:30 +02:00
_input_set->row_get_into_mlpp_vector(output_index, input_set_row_tmp);
real_t output_element_set = _output_set->element_get(output_index);
output_set_row_tmp->element_set(0, output_element_set);
2023-02-16 18:43:35 +01:00
real_t y_hat = evaluatev(input_set_row_tmp);
y_hat_row_tmp->element_set(0, y_hat);
2023-02-16 18:43:35 +01:00
cost_prev = cost(y_hat_row_tmp, output_set_row_tmp);
2023-01-24 19:00:54 +01:00
2023-02-12 10:52:46 +01:00
for (int i = 0; i < _k; i++) {
2023-01-24 19:00:54 +01:00
// Calculating the weight gradients
real_t w_gradient = (y_hat - output_element_set) * input_set_row_tmp->element_get(i) * Math::pow(_weights->element_get(i), _input_set->element_get(output_index, i) - 1);
real_t i_gradient = (y_hat - output_element_set) * Math::pow(_weights->element_get(i), _input_set->element_get(output_index, i));
2023-01-24 19:00:54 +01:00
// Weight/initial updation
_weights->element_set(i, _weights->element_get(i) - learning_rate * w_gradient);
_initial->element_set(i, _initial->element_get(i) - learning_rate * i_gradient);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
2023-02-16 18:43:35 +01:00
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradients
real_t b_gradient = (y_hat - output_element_set);
2023-01-24 19:00:54 +01:00
// Bias updation
2023-02-12 10:52:46 +01:00
_bias -= learning_rate * b_gradient;
2023-02-16 18:43:35 +01:00
y_hat = evaluatev(input_set_row_tmp);
2023-01-24 19:00:54 +01:00
2023-02-12 10:52:46 +01:00
if (ui) {
2023-02-16 18:43:35 +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-12 10:52:46 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
2023-02-12 10:52:46 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
void MLPPExpReg::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui) {
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-12 10:52:46 +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-12 10:52:46 +01:00
int n_mini_batch = _n / mini_batch_size;
2023-02-16 18:43:35 +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-16 18:43:35 +01:00
Ref<MLPPMatrix> current_input_batch = batches.input_sets[i];
Ref<MLPPVector> current_output_batch = batches.output_sets[i];
Ref<MLPPVector> y_hat = evaluatem(current_input_batch);
cost_prev = cost(y_hat, current_output_batch);
2023-04-30 18:46:53 +02:00
Ref<MLPPVector> error = y_hat->subn(current_output_batch);
2023-01-24 19:00:54 +01:00
2023-02-12 10:52:46 +01:00
for (int j = 0; j < _k; j++) {
2023-01-24 19:00:54 +01:00
// Calculating the weight gradient
2023-01-27 13:01:16 +01:00
real_t sum = 0;
2023-02-16 18:43:35 +01:00
for (int k = 0; k < current_output_batch->size(); k++) {
sum += error->element_get(k) * current_input_batch->element_get(k, j) * Math::pow(_weights->element_get(j), current_input_batch->element_get(k, j) - 1);
2023-01-24 19:00:54 +01:00
}
2023-02-16 18:43:35 +01:00
real_t w_gradient = sum / current_output_batch->size();
2023-01-24 19:00:54 +01:00
// Calculating the initial gradient
2023-01-27 13:01:16 +01:00
real_t sum2 = 0;
2023-02-16 18:43:35 +01:00
for (int k = 0; k < current_output_batch->size(); k++) {
sum2 += error->element_get(k) * Math::pow(_weights->element_get(j), current_input_batch->element_get(k, j));
2023-01-24 19:00:54 +01:00
}
2023-02-16 18:43:35 +01:00
real_t i_gradient = sum2 / current_output_batch->size();
2023-01-24 19:00:54 +01:00
// Weight/initial updation
_weights->element_set(i, _weights->element_get(i) - learning_rate * w_gradient);
_initial->element_set(i, _initial->element_get(i) - learning_rate * i_gradient);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
2023-02-16 18:43:35 +01:00
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradient
2023-04-16 16:05:50 +02:00
//real_t sum = 0;
//for (int j = 0; j < current_output_batch->size(); j++) {
// sum += (y_hat->element_get(j) - current_output_batch->element_get(j));
2023-04-16 16:05:50 +02:00
//}
2023-02-10 22:23:10 +01:00
2023-02-12 10:52:46 +01:00
//real_t b_gradient = sum / output_mini_batches[i].size();
2023-02-16 18:43:35 +01:00
y_hat = evaluatem(current_input_batch);
2023-01-24 19:00:54 +01:00
2023-02-12 10:52:46 +01:00
if (ui) {
2023-02-16 18:43:35 +01:00
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, current_output_batch));
MLPPUtilities::print_ui_vb(_weights, _bias);
2023-01-24 19:00:54 +01:00
}
}
2023-02-12 10:52:46 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
2023-02-12 10:52:46 +01:00
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
2023-02-12 10:52:46 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
}
2023-01-27 13:01:16 +01:00
real_t MLPPExpReg::score() {
2023-02-10 22:23:10 +01:00
MLPPUtilities util;
2023-02-12 10:52:46 +01:00
2023-02-16 18:43:35 +01:00
return util.performance_vec(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
}
2023-02-16 18:43:35 +01:00
void MLPPExpReg::save(const String &file_name) {
2023-02-10 22:23:10 +01:00
MLPPUtilities util;
2023-02-12 10:52:46 +01:00
2023-02-16 18:43:35 +01:00
//util.saveParameters(file_name, _weights, _initial, _bias);
2023-01-24 19:00:54 +01:00
}
2023-02-16 18:43:35 +01:00
MLPPExpReg::MLPPExpReg(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-12 10:52:46 +01:00
_input_set = p_input_set;
_output_set = p_output_set;
2023-02-16 18:43:35 +01:00
_n = p_input_set->size().y;
_k = p_input_set->size().x;
2023-02-12 10:52:46 +01:00
_reg = p_reg;
_lambda = p_lambda;
_alpha = p_alpha;
2023-02-16 18:43:35 +01:00
_y_hat.instance();
_y_hat->resize(_n);
MLPPUtilities util;
_weights.instance();
_weights->resize(_k);
util.weight_initializationv(_weights);
_initial.instance();
_initial->resize(_k);
util.weight_initializationv(_initial);
_bias = util.bias_initializationr();
2023-02-12 10:52:46 +01:00
}
MLPPExpReg::MLPPExpReg() {
}
MLPPExpReg::~MLPPExpReg() {
}
2023-02-16 18:43:35 +01:00
real_t MLPPExpReg::cost(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-12 10:52:46 +01:00
MLPPCost mlpp_cost;
2023-02-16 18:43:35 +01:00
return mlpp_cost.msev(y_hat, y) + regularization.reg_termv(_weights, _lambda, _alpha, _reg);
2023-02-12 10:52:46 +01:00
}
2023-02-16 18:43:35 +01:00
real_t MLPPExpReg::evaluatev(const Ref<MLPPVector> &x) {
2023-02-12 10:52:46 +01:00
real_t y_hat = 0;
2023-02-16 18:43:35 +01:00
for (int i = 0; i < x->size(); i++) {
y_hat += _initial->element_get(i) * Math::pow(_weights->element_get(i), x->element_get(i));
2023-02-12 10:52:46 +01:00
}
return y_hat + _bias;
2023-01-24 19:00:54 +01:00
}
2023-02-16 18:43:35 +01:00
Ref<MLPPVector> MLPPExpReg::evaluatem(const Ref<MLPPMatrix> &X) {
Ref<MLPPVector> y_hat;
y_hat.instance();
y_hat->resize(X->size().y);
for (int i = 0; i < X->size().y; i++) {
2023-04-16 16:05:50 +02:00
real_t y = 0;
2023-02-12 10:52:46 +01:00
2023-02-16 18:43:35 +01:00
for (int j = 0; j < X->size().x; j++) {
y += _initial->element_get(j) * Math::pow(_weights->element_get(j), X->element_get(i, j));
2023-01-24 19:00:54 +01:00
}
2023-02-16 18:43:35 +01:00
y += _bias;
y_hat->element_set(i, y);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
return y_hat;
2023-01-24 19:00:54 +01:00
}
// a * w^x + b
2023-02-12 10:52:46 +01:00
void MLPPExpReg::forward_pass() {
_y_hat = evaluatem(_input_set);
2023-01-24 19:00:54 +01:00
}
void MLPPExpReg::_bind_methods() {
}