2023-01-23 21:13:26 +01:00
|
|
|
//
|
|
|
|
// SoftmaxReg.cpp
|
|
|
|
//
|
|
|
|
// Created by Marc Melikyan on 10/2/20.
|
|
|
|
//
|
|
|
|
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "softmax_reg.h"
|
2023-01-24 19:00:54 +01:00
|
|
|
#include "../activation/activation.h"
|
|
|
|
#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:20:18 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
MLPPSoftmaxReg::MLPPSoftmaxReg(std::vector<std::vector<real_t>> inputSet, std::vector<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()), n_class(outputSet[0].size()), reg(reg), lambda(lambda), alpha(alpha) {
|
|
|
|
y_hat.resize(n);
|
2023-01-25 01:09:37 +01:00
|
|
|
weights = MLPPUtilities::weightInitialization(k, n_class);
|
|
|
|
bias = MLPPUtilities::biasInitialization(n_class);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> MLPPSoftmaxReg::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
|
|
|
std::vector<std::vector<real_t>> MLPPSoftmaxReg::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
|
|
|
void MLPPSoftmaxReg::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<std::vector<real_t>> error = alg.subtraction(y_hat, outputSet);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
//Calculating the weight gradients
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<std::vector<real_t>> w_gradient = alg.matmult(alg.transpose(inputSet), error);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
//Weight updation
|
|
|
|
weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate, w_gradient));
|
|
|
|
weights = regularization.regWeights(weights, lambda, alpha, reg);
|
|
|
|
|
|
|
|
// Calculating the bias gradients
|
2023-01-27 13:01:16 +01:00
|
|
|
//real_t b_gradient = alg.sum_elements(error);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Bias Updation
|
|
|
|
bias = alg.subtractMatrixRows(bias, alg.scalarMultiply(learning_rate, error));
|
|
|
|
|
|
|
|
forwardPass();
|
|
|
|
|
|
|
|
// UI PORTION
|
|
|
|
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-27 13:01:16 +01:00
|
|
|
void MLPPSoftmaxReg::SGD(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;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
std::random_device rd;
|
|
|
|
std::default_random_engine generator(rd());
|
|
|
|
std::uniform_int_distribution<int> distribution(0, int(n - 1));
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t outputIndex = distribution(generator);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> y_hat = Evaluate(inputSet[outputIndex]);
|
2023-01-24 19:00:54 +01:00
|
|
|
cost_prev = Cost({ y_hat }, { outputSet[outputIndex] });
|
|
|
|
|
|
|
|
// Calculating the weight gradients
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<std::vector<real_t>> w_gradient = alg.outerProduct(inputSet[outputIndex], alg.subtraction(y_hat, outputSet[outputIndex]));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Weight Updation
|
|
|
|
weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate, w_gradient));
|
|
|
|
weights = regularization.regWeights(weights, lambda, alpha, reg);
|
|
|
|
|
|
|
|
// Calculating the bias gradients
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> b_gradient = alg.subtraction(y_hat, outputSet[outputIndex]);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Bias updation
|
|
|
|
bias = alg.subtraction(bias, alg.scalarMultiply(learning_rate, b_gradient));
|
|
|
|
|
|
|
|
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-27 13:01:16 +01:00
|
|
|
void MLPPSoftmaxReg::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;
|
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++) {
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<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<std::vector<real_t>> error = alg.subtraction(y_hat, outputMiniBatches[i]);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Calculating the weight gradients
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<std::vector<real_t>> w_gradient = alg.matmult(alg.transpose(inputMiniBatches[i]), error);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
//Weight updation
|
|
|
|
weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate, w_gradient));
|
|
|
|
weights = regularization.regWeights(weights, lambda, alpha, reg);
|
|
|
|
|
|
|
|
// Calculating the bias gradients
|
|
|
|
bias = alg.subtractMatrixRows(bias, alg.scalarMultiply(learning_rate, error));
|
|
|
|
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-27 13:01:16 +01:00
|
|
|
real_t MLPPSoftmaxReg::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-25 01:09:37 +01:00
|
|
|
void MLPPSoftmaxReg::save(std::string fileName) {
|
|
|
|
MLPPUtilities util;
|
2023-01-24 19:00:54 +01:00
|
|
|
util.saveParameters(fileName, weights, bias);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPSoftmaxReg::Cost(std::vector<std::vector<real_t>> y_hat, std::vector<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.CrossEntropy(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> MLPPSoftmaxReg::Evaluate(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;
|
2023-01-24 19:00:54 +01:00
|
|
|
return avn.softmax(alg.addition(bias, alg.mat_vec_mult(alg.transpose(weights), x)));
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<std::vector<real_t>> MLPPSoftmaxReg::Evaluate(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;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
return avn.softmax(alg.mat_vec_add(alg.matmult(X, weights), bias));
|
|
|
|
}
|
|
|
|
|
|
|
|
// softmax ( wTx + b )
|
2023-01-25 01:09:37 +01:00
|
|
|
void MLPPSoftmaxReg::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
|
|
|
|
|
|
|
y_hat = avn.softmax(alg.mat_vec_add(alg.matmult(inputSet, weights), bias));
|
|
|
|
}
|