2023-12-30 00:41:59 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* c_log_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-23 21:13:26 +01:00
|
|
|
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "c_log_log_reg.h"
|
|
|
|
#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"
|
2023-01-23 21:13:26 +01:00
|
|
|
|
|
|
|
#include <random>
|
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
Ref<MLPPVector> MLPPCLogLogReg::model_set_test(const Ref<MLPPMatrix> &X) {
|
2023-02-12 13:04:33 +01:00
|
|
|
return evaluatem(X);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
real_t MLPPCLogLogReg::model_test(const Ref<MLPPVector> &x) {
|
2023-02-12 13:04:33 +01:00
|
|
|
return evaluatev(x);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
void MLPPCLogLogReg::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:54:50 +01:00
|
|
|
MLPPReg regularization;
|
2023-02-12 13:04:33 +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 13:04:33 +01:00
|
|
|
|
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
while (true) {
|
2023-02-12 13:30:47 +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
|
|
|
|
|
|
|
// Calculating the weight gradients
|
2023-04-30 18:46:53 +02:00
|
|
|
_weights->sub(_input_set->transposen()->mult_vec(error->hadamard_productn(avn.cloglog_derivv(_z)))->scalar_multiplyn(learning_rate / _n));
|
2023-02-16 19:56:18 +01:00
|
|
|
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Calculating the bias gradients
|
2023-04-30 18:46:53 +02:00
|
|
|
|
|
|
|
bias -= learning_rate * error->hadamard_productn(avn.cloglog_derivv(_z))->sum_elements() / _n;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
if (ui) {
|
2023-02-16 19:56:18 +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 13:04:33 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
epoch++;
|
|
|
|
|
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
void MLPPCLogLogReg::mle(real_t learning_rate, int max_epoch, bool ui) {
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-01-25 00:54:50 +01:00
|
|
|
MLPPReg regularization;
|
2023-02-12 13:04:33 +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 13:04:33 +01:00
|
|
|
|
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
while (true) {
|
2023-02-12 13:30:47 +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-04-30 18:46:53 +02:00
|
|
|
_weights->add(_input_set->transposen()->mult_vec(error->hadamard_productn(avn.cloglog_derivv(_z)))->scalar_multiplyn(learning_rate / _n));
|
2023-02-16 19:56:18 +01:00
|
|
|
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Calculating the bias gradients
|
2023-04-30 18:46:53 +02:00
|
|
|
bias += learning_rate * error->hadamard_productn(avn.cloglog_derivv(_z))->sum_elements() / _n;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
forward_pass();
|
|
|
|
|
|
|
|
if (ui) {
|
2023-02-16 19:56:18 +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 13:04:33 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
epoch++;
|
|
|
|
|
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
void MLPPCLogLogReg::sgd(real_t learning_rate, int max_epoch, bool p_) {
|
2023-01-25 00:54:50 +01:00
|
|
|
MLPPReg regularization;
|
2023-02-12 13:04:33 +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 13:04:33 +01:00
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
std::random_device rd;
|
|
|
|
std::default_random_engine generator(rd());
|
|
|
|
std::uniform_int_distribution<int> distribution(0, int(_n - 1));
|
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 19:56:18 +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-16 19:56:18 +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);
|
2023-04-29 13:44:18 +02:00
|
|
|
real_t output_element_set = _output_set->element_get(output_index);
|
|
|
|
output_set_row_tmp->element_set(0, output_element_set);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
real_t y_hat = evaluatev(input_set_row_tmp);
|
2023-04-29 13:44:18 +02:00
|
|
|
y_hat_row_tmp->element_set(0, y_hat);
|
2023-02-16 19:56:18 +01:00
|
|
|
|
|
|
|
real_t z = propagatev(input_set_row_tmp);
|
|
|
|
|
|
|
|
cost_prev = cost(y_hat_row_tmp, output_set_row_tmp);
|
|
|
|
|
2023-04-29 13:44:18 +02:00
|
|
|
real_t error = y_hat - output_element_set;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Weight Updation
|
2023-04-30 18:46:53 +02:00
|
|
|
|
|
|
|
_weights->sub(input_set_row_tmp->scalar_multiplyn(learning_rate * error * Math::exp(z - Math::exp(z))));
|
2023-02-16 19:56:18 +01:00
|
|
|
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Bias updation
|
|
|
|
bias -= learning_rate * error * exp(z - exp(z));
|
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
y_hat = evaluatev(input_set_row_tmp);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
if (p_) {
|
2023-02-16 19:56:18 +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 13:04:33 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
epoch++;
|
|
|
|
|
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 13:04:33 +01:00
|
|
|
|
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
void MLPPCLogLogReg::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool p_) {
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
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-12 13:30:47 +01:00
|
|
|
int n_mini_batch = _n / mini_batch_size;
|
2023-02-16 19:56:18 +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 19:56:18 +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);
|
|
|
|
Ref<MLPPVector> z = propagatem(current_input_batch);
|
|
|
|
cost_prev = cost(y_hat, current_output_batch);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
// Calculating the weight gradients
|
2023-04-30 18:46:53 +02:00
|
|
|
_weights->sub(current_input_batch->transposen()->mult_vec(error->hadamard_productn(avn.cloglog_derivv(z)))->scalar_multiplyn(learning_rate / _n));
|
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
_weights = regularization.reg_weightsv(_weights, _lambda, _alpha, _reg);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Calculating the bias gradients
|
2023-04-30 18:46:53 +02:00
|
|
|
bias -= learning_rate * error->hadamard_productn(avn.cloglog_derivv(z))->sum_elements() / _n;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
y_hat = evaluatem(current_input_batch);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 13:04:33 +01:00
|
|
|
if (p_) {
|
2023-02-16 19:56:18 +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 13:04:33 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
epoch++;
|
2023-02-12 13:04:33 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 13:04:33 +01:00
|
|
|
|
|
|
|
forward_pass();
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPCLogLogReg::score() {
|
2023-02-10 22:40:20 +01:00
|
|
|
MLPPUtilities util;
|
2023-02-16 19:56:18 +01:00
|
|
|
|
|
|
|
return util.performance_vec(_y_hat, _output_set);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
MLPPCLogLogReg::MLPPCLogLogReg(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 13:30:47 +01:00
|
|
|
_input_set = p_input_set;
|
|
|
|
_output_set = p_output_set;
|
2023-02-16 19:56:18 +01:00
|
|
|
_n = _input_set->size().y;
|
|
|
|
_k = _input_set->size().x;
|
2023-02-12 13:30:47 +01:00
|
|
|
_reg = p_reg;
|
|
|
|
_lambda = p_lambda;
|
|
|
|
_alpha = p_alpha;
|
2023-02-12 13:04:33 +01:00
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
_y_hat.instance();
|
|
|
|
_y_hat->resize(_n);
|
|
|
|
|
|
|
|
MLPPUtilities utilities;
|
2023-02-12 13:04:33 +01:00
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
_weights.instance();
|
|
|
|
_weights->resize(_k);
|
|
|
|
utilities.weight_initializationv(_weights);
|
|
|
|
|
|
|
|
bias = utilities.bias_initializationr();
|
2023-02-12 13:04:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MLPPCLogLogReg::MLPPCLogLogReg() {
|
|
|
|
}
|
|
|
|
MLPPCLogLogReg::~MLPPCLogLogReg() {
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
real_t MLPPCLogLogReg::cost(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
|
2023-01-25 00:54:50 +01:00
|
|
|
MLPPReg regularization;
|
2023-02-16 19:56:18 +01:00
|
|
|
MLPPCost mlpp_cost;
|
|
|
|
|
|
|
|
return mlpp_cost.msev(y_hat, y) + regularization.reg_termv(_weights, _lambda, _alpha, _reg);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
real_t MLPPCLogLogReg::evaluatev(const Ref<MLPPVector> &x) {
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-02-16 19:56:18 +01:00
|
|
|
|
2023-04-30 18:46:53 +02:00
|
|
|
return avn.cloglog_normr(_weights->dot(x) + bias);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
real_t MLPPCLogLogReg::propagatev(const Ref<MLPPVector> &x) {
|
2023-04-30 18:46:53 +02:00
|
|
|
return _weights->dot(x) + bias;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
Ref<MLPPVector> MLPPCLogLogReg::evaluatem(const Ref<MLPPMatrix> &X) {
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-02-16 19:56:18 +01:00
|
|
|
|
2023-04-30 18:46:53 +02:00
|
|
|
return avn.cloglog_normv(X->mult_vec(_weights)->scalar_addn(bias));
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:18 +01:00
|
|
|
Ref<MLPPVector> MLPPCLogLogReg::propagatem(const Ref<MLPPMatrix> &X) {
|
2023-04-30 18:46:53 +02:00
|
|
|
return X->mult_vec(_weights)->scalar_addn(bias);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// cloglog ( wTx + b )
|
2023-02-12 13:04:33 +01:00
|
|
|
void MLPPCLogLogReg::forward_pass() {
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 13:30:47 +01:00
|
|
|
_z = propagatem(_input_set);
|
2023-02-16 19:56:18 +01:00
|
|
|
_y_hat = avn.cloglog_normv(_z);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
2023-02-12 18:23:33 +01:00
|
|
|
|
|
|
|
void MLPPCLogLogReg::_bind_methods() {
|
|
|
|
}
|