pmlpp/mlpp/c_log_log_reg/c_log_log_reg.cpp

251 lines
6.5 KiB
C++
Raw Normal View History

//
// CLogLogReg.cpp
//
// Created by Marc Melikyan on 10/2/20.
//
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 "../lin_alg/lin_alg.h"
#include "../regularization/reg.h"
#include "../utilities/utilities.h"
#include <iostream>
#include <random>
std::vector<real_t> MLPPCLogLogReg::model_set_test(std::vector<std::vector<real_t>> X) {
return evaluatem(X);
2023-01-24 19:00:54 +01:00
}
real_t MLPPCLogLogReg::model_test(std::vector<real_t> x) {
return evaluatev(x);
2023-01-24 19:00:54 +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:29:02 +01:00
MLPPLinAlg alg;
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;
forward_pass();
2023-01-24 19:00:54 +01:00
while (true) {
cost_prev = cost(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
std::vector<real_t> error = alg.subtraction(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
// Calculating the weight gradients
_weights = alg.subtraction(_weights, alg.scalarMultiply(learning_rate / _n, alg.mat_vec_mult(alg.transpose(_input_set), alg.hadamard_product(error, avn.cloglog(_z, true)))));
_weights = regularization.regWeights(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradients
bias -= learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(_z, true))) / _n;
2023-01-24 19:00:54 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
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-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
}
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:29:02 +01:00
MLPPLinAlg alg;
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;
forward_pass();
2023-01-24 19:00:54 +01:00
while (true) {
cost_prev = cost(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
std::vector<real_t> error = alg.subtraction(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
_weights = alg.addition(_weights, alg.scalarMultiply(learning_rate / _n, alg.mat_vec_mult(alg.transpose(_input_set), alg.hadamard_product(error, avn.cloglog(_z, true)))));
_weights = regularization.regWeights(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradients
bias += learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(_z, true))) / _n;
2023-01-24 19:00:54 +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-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
}
void MLPPCLogLogReg::sgd(real_t learning_rate, int max_epoch, bool p_) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
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;
forward_pass();
2023-01-24 19:00:54 +01:00
while (true) {
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
int outputIndex = distribution(generator);
real_t y_hat = evaluatev(_input_set[outputIndex]);
real_t z = propagatev(_input_set[outputIndex]);
cost_prev = cost({ y_hat }, { _output_set[outputIndex] });
2023-01-24 19:00:54 +01:00
real_t error = y_hat - _output_set[outputIndex];
2023-01-24 19:00:54 +01:00
// Weight Updation
_weights = alg.subtraction(_weights, alg.scalarMultiply(learning_rate * error * exp(z - exp(z)), _input_set[outputIndex]));
_weights = regularization.regWeights(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Bias updation
bias -= learning_rate * error * exp(z - exp(z));
y_hat = evaluatev(_input_set[outputIndex]);
2023-01-24 19:00:54 +01:00
if (p_) {
MLPPUtilities::CostInfo(epoch, cost_prev, cost({ y_hat }, { _output_set[outputIndex] }));
MLPPUtilities::UI(_weights, bias);
2023-01-24 19:00:54 +01:00
}
2023-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
forward_pass();
2023-01-24 19:00:54 +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:29:02 +01:00
MLPPLinAlg alg;
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
int n_mini_batch = _n / mini_batch_size;
auto batches = MLPPUtilities::createMiniBatches(_input_set, _output_set, n_mini_batch);
2023-02-10 22:40:20 +01:00
auto inputMiniBatches = std::get<0>(batches);
auto outputMiniBatches = std::get<1>(batches);
2023-01-24 19:00:54 +01:00
while (true) {
for (int i = 0; i < n_mini_batch; i++) {
std::vector<real_t> y_hat = evaluatem(inputMiniBatches[i]);
std::vector<real_t> z = propagatem(inputMiniBatches[i]);
cost_prev = cost(y_hat, outputMiniBatches[i]);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<real_t> error = alg.subtraction(y_hat, outputMiniBatches[i]);
2023-01-24 19:00:54 +01:00
// Calculating the weight gradients
_weights = alg.subtraction(_weights, alg.scalarMultiply(learning_rate / _n, alg.mat_vec_mult(alg.transpose(inputMiniBatches[i]), alg.hadamard_product(error, avn.cloglog(z, 1)))));
_weights = regularization.regWeights(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
// Calculating the bias gradients
bias -= learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / _n;
2023-01-24 19:00:54 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
y_hat = evaluatem(inputMiniBatches[i]);
2023-01-24 19:00:54 +01:00
if (p_) {
MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, outputMiniBatches[i]));
MLPPUtilities::UI(_weights, bias);
2023-01-24 19:00:54 +01:00
}
}
2023-01-24 19:00:54 +01:00
epoch++;
2023-01-24 19:00:54 +01:00
if (epoch > max_epoch) {
break;
}
}
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;
return util.performance(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
}
MLPPCLogLogReg::MLPPCLogLogReg(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 = _input_set.size();
_k = _input_set[0].size();
_reg = p_reg;
_lambda = p_lambda;
_alpha = p_alpha;
_y_hat.resize(_n);
_weights = MLPPUtilities::weightInitialization(_k);
bias = MLPPUtilities::biasInitialization();
}
MLPPCLogLogReg::MLPPCLogLogReg() {
}
MLPPCLogLogReg::~MLPPCLogLogReg() {
}
real_t MLPPCLogLogReg::cost(std::vector<real_t> y_hat, std::vector<real_t> y) {
2023-01-25 00:54:50 +01:00
MLPPReg regularization;
2023-01-24 19:37:08 +01:00
class MLPPCost cost;
return cost.MSE(y_hat, y) + regularization.regTerm(_weights, _lambda, _alpha, _reg);
2023-01-24 19:00:54 +01:00
}
real_t MLPPCLogLogReg::evaluatev(std::vector<real_t> x) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
return avn.cloglog(alg.dot(_weights, x) + bias);
2023-01-24 19:00:54 +01:00
}
real_t MLPPCLogLogReg::propagatev(std::vector<real_t> x) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
return alg.dot(_weights, x) + bias;
2023-01-24 19:00:54 +01:00
}
std::vector<real_t> MLPPCLogLogReg::evaluatem(std::vector<std::vector<real_t>> X) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
return avn.cloglog(alg.scalarAdd(bias, alg.mat_vec_mult(X, _weights)));
2023-01-24 19:00:54 +01:00
}
std::vector<real_t> MLPPCLogLogReg::propagatem(std::vector<std::vector<real_t>> X) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
return alg.scalarAdd(bias, alg.mat_vec_mult(X, _weights));
2023-01-24 19:00:54 +01:00
}
// cloglog ( wTx + b )
void MLPPCLogLogReg::forward_pass() {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-24 19:00:54 +01:00
_z = propagatem(_input_set);
_y_hat = avn.cloglog(_z);
2023-01-24 19:00:54 +01:00
}