pmlpp/mlpp/exp_reg/exp_reg.cpp

238 lines
6.4 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-01-24 19:20:18 +01:00
2023-01-27 13:01:16 +01:00
MLPPExpReg::MLPPExpReg(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, std::string reg, real_t lambda, real_t 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);
weights = MLPPUtilities::weightInitialization(k);
initial = MLPPUtilities::weightInitialization(k);
bias = MLPPUtilities::biasInitialization();
2023-01-24 19:00:54 +01:00
}
2023-01-27 13:01:16 +01:00
std::vector<real_t> MLPPExpReg::modelSetTest(std::vector<std::vector<real_t>> X) {
2023-01-24 19:00:54 +01:00
return Evaluate(X);
}
2023-01-27 13:01:16 +01:00
real_t MLPPExpReg::modelTest(std::vector<real_t> x) {
2023-01-24 19:00:54 +01:00
return Evaluate(x);
}
2023-01-27 13:01:16 +01:00
void MLPPExpReg::gradientDescent(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-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
forwardPass();
while (true) {
cost_prev = Cost(y_hat, outputSet);
2023-01-27 13:01:16 +01:00
std::vector<real_t> error = alg.subtraction(y_hat, outputSet);
2023-01-24 19:00:54 +01:00
for (int i = 0; i < k; i++) {
// Calculating the weight gradient
2023-01-27 13:01:16 +01:00
real_t sum = 0;
2023-01-24 19:00:54 +01:00
for (int j = 0; j < n; j++) {
sum += error[j] * inputSet[j][i] * std::pow(weights[i], inputSet[j][i] - 1);
}
2023-01-27 13:01:16 +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-01-24 19:00:54 +01:00
for (int j = 0; j < n; j++) {
sum2 += error[j] * std::pow(weights[i], inputSet[j][i]);
}
2023-01-27 13:01:16 +01:00
real_t i_gradient = sum2 / n;
2023-01-24 19:00:54 +01:00
// Weight/initial updation
weights[i] -= learning_rate * w_gradient;
initial[i] -= learning_rate * i_gradient;
}
weights = regularization.regWeights(weights, lambda, alpha, reg);
// Calculating the bias gradient
2023-01-27 13:01:16 +01:00
real_t sum = 0;
2023-01-24 19:00:54 +01:00
for (int j = 0; j < n; j++) {
sum += (y_hat[j] - outputSet[j]);
}
2023-01-27 13:01:16 +01:00
real_t b_gradient = sum / n;
2023-01-24 19:00:54 +01:00
// bias updation
bias -= learning_rate * b_gradient;
forwardPass();
if (UI) {
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-27 13:01:16 +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-01-27 13:01:16 +01:00
real_t cost_prev = 0;
2023-01-24 19:00:54 +01:00
int epoch = 1;
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);
2023-01-27 13:01:16 +01:00
real_t y_hat = Evaluate(inputSet[outputIndex]);
2023-01-24 19:00:54 +01:00
cost_prev = Cost({ y_hat }, { outputSet[outputIndex] });
for (int i = 0; i < k; i++) {
// Calculating the weight gradients
2023-01-27 13:01:16 +01:00
real_t w_gradient = (y_hat - outputSet[outputIndex]) * inputSet[outputIndex][i] * std::pow(weights[i], inputSet[outputIndex][i] - 1);
real_t i_gradient = (y_hat - outputSet[outputIndex]) * std::pow(weights[i], inputSet[outputIndex][i]);
2023-01-24 19:00:54 +01:00
// Weight/initial updation
weights[i] -= learning_rate * w_gradient;
initial[i] -= learning_rate * i_gradient;
}
weights = regularization.regWeights(weights, lambda, alpha, reg);
// Calculating the bias gradients
2023-01-27 13:01:16 +01:00
real_t b_gradient = (y_hat - outputSet[outputIndex]);
2023-01-24 19:00:54 +01:00
// Bias updation
bias -= learning_rate * b_gradient;
y_hat = Evaluate({ inputSet[outputIndex] });
if (UI) {
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-27 13:01:16 +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-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 [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++) {
2023-01-27 13:01:16 +01:00
std::vector<real_t> y_hat = Evaluate(inputMiniBatches[i]);
2023-01-24 19:00:54 +01:00
cost_prev = Cost(y_hat, outputMiniBatches[i]);
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
for (int j = 0; j < k; j++) {
// Calculating the weight gradient
2023-01-27 13:01:16 +01:00
real_t sum = 0;
2023-01-24 19:00:54 +01:00
for (int k = 0; k < outputMiniBatches[i].size(); k++) {
sum += error[k] * inputMiniBatches[i][k][j] * std::pow(weights[j], inputMiniBatches[i][k][j] - 1);
}
2023-01-27 13:01:16 +01:00
real_t w_gradient = sum / outputMiniBatches[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-01-24 19:00:54 +01:00
for (int k = 0; k < outputMiniBatches[i].size(); k++) {
sum2 += error[k] * std::pow(weights[j], inputMiniBatches[i][k][j]);
}
2023-01-27 13:01:16 +01:00
real_t i_gradient = sum2 / outputMiniBatches[i].size();
2023-01-24 19:00:54 +01:00
// Weight/initial updation
weights[j] -= learning_rate * w_gradient;
initial[j] -= learning_rate * i_gradient;
}
weights = regularization.regWeights(weights, lambda, alpha, reg);
// Calculating the bias gradient
2023-01-27 13:01:16 +01:00
real_t sum = 0;
2023-01-24 19:00:54 +01:00
for (int j = 0; j < outputMiniBatches[i].size(); j++) {
sum += (y_hat[j] - outputMiniBatches[i][j]);
}
2023-01-27 13:01:16 +01:00
real_t b_gradient = sum / outputMiniBatches[i].size();
2023-01-24 19:00:54 +01:00
y_hat = Evaluate(inputMiniBatches[i]);
if (UI) {
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-27 13:01:16 +01:00
real_t MLPPExpReg::score() {
MLPPUtilities util;
2023-01-24 19:00:54 +01:00
return util.performance(y_hat, outputSet);
}
2023-01-25 00:21:31 +01:00
void MLPPExpReg::save(std::string fileName) {
MLPPUtilities util;
2023-01-24 19:00:54 +01:00
util.saveParameters(fileName, weights, initial, bias);
}
2023-01-27 13:01:16 +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-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-27 13:01:16 +01:00
std::vector<real_t> MLPPExpReg::Evaluate(std::vector<std::vector<real_t>> X) {
std::vector<real_t> y_hat;
2023-01-24 19:00:54 +01:00
y_hat.resize(X.size());
for (int i = 0; i < X.size(); i++) {
y_hat[i] = 0;
for (int j = 0; j < X[i].size(); j++) {
y_hat[i] += initial[j] * std::pow(weights[j], X[i][j]);
}
y_hat[i] += bias;
}
return y_hat;
}
2023-01-27 13:01:16 +01:00
real_t MLPPExpReg::Evaluate(std::vector<real_t> x) {
real_t y_hat = 0;
2023-01-24 19:00:54 +01:00
for (int i = 0; i < x.size(); i++) {
y_hat += initial[i] * std::pow(weights[i], x[i]);
}
return y_hat + bias;
}
// a * w^x + b
2023-01-25 00:21:31 +01:00
void MLPPExpReg::forwardPass() {
2023-01-24 19:00:54 +01:00
y_hat = Evaluate(inputSet);
}