2023-01-23 21:13:26 +01:00
|
|
|
//
|
|
|
|
// 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"
|
2023-01-23 21:13:26 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <random>
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
MLPPCLogLogReg::MLPPCLogLogReg(std::vector<std::vector<double>> inputSet, std::vector<double> outputSet, std::string reg, double lambda, double alpha) :
|
2023-01-24 19:00:54 +01:00
|
|
|
inputSet(inputSet), outputSet(outputSet), n(inputSet.size()), k(inputSet[0].size()), reg(reg), lambda(lambda), alpha(alpha) {
|
|
|
|
y_hat.resize(n);
|
2023-01-25 01:09:37 +01:00
|
|
|
weights = MLPPUtilities::weightInitialization(k);
|
|
|
|
bias = MLPPUtilities::biasInitialization();
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
std::vector<double> MLPPCLogLogReg::modelSetTest(std::vector<std::vector<double>> X) {
|
2023-01-24 19:00:54 +01:00
|
|
|
return Evaluate(X);
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
double MLPPCLogLogReg::modelTest(std::vector<double> x) {
|
2023-01-24 19:00:54 +01:00
|
|
|
return Evaluate(x);
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
void MLPPCLogLogReg::gradientDescent(double 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-24 19:00:54 +01:00
|
|
|
double cost_prev = 0;
|
|
|
|
int epoch = 1;
|
|
|
|
forwardPass();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
cost_prev = Cost(y_hat, outputSet);
|
|
|
|
|
|
|
|
std::vector<double> error = alg.subtraction(y_hat, outputSet);
|
|
|
|
|
|
|
|
// Calculating the weight gradients
|
|
|
|
weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate / n, alg.mat_vec_mult(alg.transpose(inputSet), alg.hadamard_product(error, avn.cloglog(z, 1)))));
|
|
|
|
weights = regularization.regWeights(weights, lambda, alpha, reg);
|
|
|
|
|
|
|
|
// Calculating the bias gradients
|
|
|
|
bias -= learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / n;
|
|
|
|
|
|
|
|
forwardPass();
|
|
|
|
|
|
|
|
if (UI) {
|
2023-01-25 01:09:37 +01:00
|
|
|
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputSet));
|
|
|
|
MLPPUtilities::UI(weights, bias);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
epoch++;
|
|
|
|
|
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
void MLPPCLogLogReg::MLE(double 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-24 19:00:54 +01:00
|
|
|
double cost_prev = 0;
|
|
|
|
int epoch = 1;
|
|
|
|
forwardPass();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
cost_prev = Cost(y_hat, outputSet);
|
|
|
|
|
|
|
|
std::vector<double> error = alg.subtraction(y_hat, outputSet);
|
|
|
|
|
|
|
|
weights = alg.addition(weights, alg.scalarMultiply(learning_rate / n, alg.mat_vec_mult(alg.transpose(inputSet), alg.hadamard_product(error, avn.cloglog(z, 1)))));
|
|
|
|
weights = regularization.regWeights(weights, lambda, alpha, reg);
|
|
|
|
|
|
|
|
// Calculating the bias gradients
|
|
|
|
bias += learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / n;
|
|
|
|
forwardPass();
|
|
|
|
|
|
|
|
if (UI) {
|
2023-01-25 01:09:37 +01:00
|
|
|
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputSet));
|
|
|
|
MLPPUtilities::UI(weights, bias);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
epoch++;
|
|
|
|
|
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
void MLPPCLogLogReg::SGD(double 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-01-24 19:00:54 +01:00
|
|
|
double cost_prev = 0;
|
|
|
|
int epoch = 1;
|
|
|
|
forwardPass();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
std::random_device rd;
|
|
|
|
std::default_random_engine generator(rd());
|
|
|
|
std::uniform_int_distribution<int> distribution(0, int(n - 1));
|
|
|
|
int outputIndex = distribution(generator);
|
|
|
|
|
|
|
|
double y_hat = Evaluate(inputSet[outputIndex]);
|
|
|
|
double z = propagate(inputSet[outputIndex]);
|
|
|
|
cost_prev = Cost({ y_hat }, { outputSet[outputIndex] });
|
|
|
|
|
|
|
|
double error = y_hat - outputSet[outputIndex];
|
|
|
|
|
|
|
|
// Weight Updation
|
|
|
|
weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate * error * exp(z - exp(z)), inputSet[outputIndex]));
|
|
|
|
weights = regularization.regWeights(weights, lambda, alpha, reg);
|
|
|
|
|
|
|
|
// Bias updation
|
|
|
|
bias -= learning_rate * error * exp(z - exp(z));
|
|
|
|
|
|
|
|
y_hat = Evaluate({ inputSet[outputIndex] });
|
|
|
|
|
|
|
|
if (UI) {
|
2023-01-25 01:09:37 +01:00
|
|
|
MLPPUtilities::CostInfo(epoch, cost_prev, Cost({ y_hat }, { outputSet[outputIndex] }));
|
|
|
|
MLPPUtilities::UI(weights, bias);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
epoch++;
|
|
|
|
|
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
forwardPass();
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
void MLPPCLogLogReg::MBGD(double learning_rate, int max_epoch, int mini_batch_size, 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-24 19:00:54 +01:00
|
|
|
double cost_prev = 0;
|
|
|
|
int epoch = 1;
|
|
|
|
|
|
|
|
// Creating the mini-batches
|
|
|
|
int n_mini_batch = n / mini_batch_size;
|
2023-01-25 01:09:37 +01:00
|
|
|
auto [inputMiniBatches, outputMiniBatches] = MLPPUtilities::createMiniBatches(inputSet, outputSet, n_mini_batch);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
for (int i = 0; i < n_mini_batch; i++) {
|
|
|
|
std::vector<double> y_hat = Evaluate(inputMiniBatches[i]);
|
|
|
|
std::vector<double> z = propagate(inputMiniBatches[i]);
|
|
|
|
cost_prev = Cost(y_hat, outputMiniBatches[i]);
|
|
|
|
|
|
|
|
std::vector<double> error = alg.subtraction(y_hat, outputMiniBatches[i]);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Calculating the bias gradients
|
|
|
|
bias -= learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / n;
|
|
|
|
|
|
|
|
forwardPass();
|
|
|
|
|
|
|
|
y_hat = Evaluate(inputMiniBatches[i]);
|
|
|
|
|
|
|
|
if (UI) {
|
2023-01-25 01:09:37 +01:00
|
|
|
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputMiniBatches[i]));
|
|
|
|
MLPPUtilities::UI(weights, bias);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
epoch++;
|
|
|
|
if (epoch > max_epoch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
forwardPass();
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
double MLPPCLogLogReg::score() {
|
2023-01-25 01:09:37 +01:00
|
|
|
MLPPUtilities util;
|
2023-01-24 19:00:54 +01:00
|
|
|
return util.performance(y_hat, outputSet);
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
double MLPPCLogLogReg::Cost(std::vector<double> y_hat, std::vector<double> y) {
|
2023-01-25 00:54:50 +01:00
|
|
|
MLPPReg regularization;
|
2023-01-24 19:37:08 +01:00
|
|
|
class MLPPCost cost;
|
2023-01-24 19:00:54 +01:00
|
|
|
return cost.MSE(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg);
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
std::vector<double> MLPPCLogLogReg::Evaluate(std::vector<std::vector<double>> X) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-01-24 19:00:54 +01:00
|
|
|
return avn.cloglog(alg.scalarAdd(bias, alg.mat_vec_mult(X, weights)));
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
std::vector<double> MLPPCLogLogReg::propagate(std::vector<std::vector<double>> X) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:00:54 +01:00
|
|
|
return alg.scalarAdd(bias, alg.mat_vec_mult(X, weights));
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
double MLPPCLogLogReg::Evaluate(std::vector<double> x) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-01-24 19:00:54 +01:00
|
|
|
return avn.cloglog(alg.dot(weights, x) + bias);
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
double MLPPCLogLogReg::propagate(std::vector<double> x) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:00:54 +01:00
|
|
|
return alg.dot(weights, x) + bias;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cloglog ( wTx + b )
|
2023-01-24 19:29:29 +01:00
|
|
|
void MLPPCLogLogReg::forwardPass() {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:23:30 +01:00
|
|
|
MLPPActivation avn;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
z = propagate(inputSet);
|
|
|
|
y_hat = avn.cloglog(z);
|
|
|
|
}
|