pmlpp/mlpp/exp_reg/exp_reg.cpp

276 lines
6.8 KiB
C++
Raw Normal View History

//
// ExpReg.cpp
//
// Created by Marc Melikyan on 10/2/20.
//
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 "../lin_alg/lin_alg.h"
#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-12 10:52:46 +01:00
std::vector<real_t> MLPPExpReg::model_set_test(std::vector<std::vector<real_t>> X) {
return evaluatem(X);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
real_t MLPPExpReg::model_test(std::vector<real_t> x) {
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:29:02 +01:00
MLPPLinAlg alg;
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-02-12 10:52:46 +01:00
std::vector<real_t> error = alg.subtraction(_y_hat, _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[j] * _input_set[j][i] * std::pow(_weights[i], _input_set[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[j] * std::pow(_weights[i], _input_set[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
2023-02-12 10:52:46 +01:00
_weights[i] -= learning_rate * w_gradient;
_initial[i] -= learning_rate * i_gradient;
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
_weights = regularization.regWeights(_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[j] - _output_set[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) {
MLPPUtilities::CostInfo(epoch, cost_prev, cost(_y_hat, _output_set));
MLPPUtilities::UI(_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-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-02-12 10:52:46 +01:00
real_t y_hat = evaluatev(_input_set[output_index]);
cost_prev = cost({ y_hat }, { _output_set[output_index] });
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
2023-02-12 10:52:46 +01:00
real_t w_gradient = (y_hat - _output_set[output_index]) * _input_set[output_index][i] * std::pow(_weights[i], _input_set[output_index][i] - 1);
real_t i_gradient = (y_hat - _output_set[output_index]) * std::pow(_weights[i], _input_set[output_index][i]);
2023-01-24 19:00:54 +01:00
// Weight/initial updation
2023-02-12 10:52:46 +01:00
_weights[i] -= learning_rate * w_gradient;
_initial[i] -= learning_rate * i_gradient;
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
_weights = regularization.regWeights(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradients
2023-02-12 10:52:46 +01:00
real_t b_gradient = (y_hat - _output_set[output_index]);
2023-01-24 19:00:54 +01:00
// Bias updation
2023-02-12 10:52:46 +01:00
_bias -= learning_rate * b_gradient;
y_hat = evaluatev(_input_set[output_index]);
2023-01-24 19:00:54 +01:00
2023-02-12 10:52:46 +01:00
if (ui) {
MLPPUtilities::CostInfo(epoch, cost_prev, cost({ y_hat }, { _output_set[output_index] }));
MLPPUtilities::UI(_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:29:02 +01:00
MLPPLinAlg alg;
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;
auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch);
auto input_mini_batches = std::get<0>(batches);
auto output_mini_batches = std::get<1>(batches);
2023-01-24 19:00:54 +01:00
while (true) {
for (int i = 0; i < n_mini_batch; i++) {
2023-02-12 10:52:46 +01:00
std::vector<real_t> y_hat = evaluatem(input_mini_batches[i]);
cost_prev = cost(y_hat, output_mini_batches[i]);
std::vector<real_t> error = alg.subtraction(y_hat, output_mini_batches[i]);
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-12 10:52:46 +01:00
for (uint32_t k = 0; k < output_mini_batches[i].size(); k++) {
sum += error[k] * input_mini_batches[i][k][j] * std::pow(_weights[j], input_mini_batches[i][k][j] - 1);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
real_t w_gradient = sum / output_mini_batches[i].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-12 10:52:46 +01:00
for (uint32_t k = 0; k < output_mini_batches[i].size(); k++) {
sum2 += error[k] * std::pow(_weights[j], input_mini_batches[i][k][j]);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
real_t i_gradient = sum2 / output_mini_batches[i].size();
2023-01-24 19:00:54 +01:00
// Weight/initial updation
2023-02-12 10:52:46 +01:00
_weights[j] -= learning_rate * w_gradient;
_initial[j] -= learning_rate * i_gradient;
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
_weights = regularization.regWeights(_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 (uint32_t j = 0; j < output_mini_batches[i].size(); j++) {
sum += (y_hat[j] - output_mini_batches[i][j]);
2023-01-24 19:00:54 +01: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();
y_hat = evaluatem(input_mini_batches[i]);
2023-01-24 19:00:54 +01:00
2023-02-12 10:52:46 +01:00
if (ui) {
MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, output_mini_batches[i]));
MLPPUtilities::UI(_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
return util.performance(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
void MLPPExpReg::save(std::string file_name) {
2023-02-10 22:23:10 +01:00
MLPPUtilities util;
2023-02-12 10:52:46 +01:00
util.saveParameters(file_name, _weights, _initial, _bias);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
MLPPExpReg::MLPPExpReg(std::vector<std::vector<real_t>> p_input_set, std::vector<real_t> p_output_set, std::string p_reg, real_t p_lambda, real_t p_alpha) {
_input_set = p_input_set;
_output_set = p_output_set;
_n = p_input_set.size();
_k = p_input_set[0].size();
_reg = p_reg;
_lambda = p_lambda;
_alpha = p_alpha;
_y_hat.resize(_n);
_weights = MLPPUtilities::weightInitialization(_k);
_initial = MLPPUtilities::weightInitialization(_k);
_bias = MLPPUtilities::biasInitialization();
}
MLPPExpReg::MLPPExpReg() {
}
MLPPExpReg::~MLPPExpReg() {
}
2023-02-12 10:52:46 +01:00
real_t MLPPExpReg::cost(std::vector<real_t> y_hat, std::vector<real_t> y) {
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-02-12 10:52:46 +01:00
MLPPCost mlpp_cost;
return mlpp_cost.MSE(y_hat, y) + regularization.regTerm(_weights, _lambda, _alpha, _reg);
}
real_t MLPPExpReg::evaluatev(std::vector<real_t> x) {
real_t y_hat = 0;
for (uint32_t i = 0; i < x.size(); i++) {
y_hat += _initial[i] * std::pow(_weights[i], x[i]);
}
return y_hat + _bias;
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
std::vector<real_t> MLPPExpReg::evaluatem(std::vector<std::vector<real_t>> X) {
2023-01-27 13:01:16 +01:00
std::vector<real_t> y_hat;
2023-01-24 19:00:54 +01:00
y_hat.resize(X.size());
2023-02-12 10:52:46 +01:00
2023-02-10 22:23:10 +01:00
for (uint32_t i = 0; i < X.size(); i++) {
2023-01-24 19:00:54 +01:00
y_hat[i] = 0;
2023-02-10 22:23:10 +01:00
for (uint32_t j = 0; j < X[i].size(); j++) {
2023-02-12 10:52:46 +01:00
y_hat[i] += _initial[j] * std::pow(_weights[j], X[i][j]);
2023-01-24 19:00:54 +01:00
}
2023-02-12 10:52:46 +01:00
y_hat[i] += _bias;
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() {
}