pmlpp/mlpp/dual_svc/dual_svc.cpp

257 lines
7.1 KiB
C++
Raw Normal View History

//
// DualSVC.cpp
//
// Created by Marc Melikyan on 10/2/20.
//
2023-01-24 18:12:23 +01:00
#include "dual_svc.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>
2023-02-12 12:36:52 +01:00
std::vector<real_t> MLPPDualSVC::model_set_test(std::vector<std::vector<real_t>> X) {
return evaluatem(X);
2023-01-24 19:00:54 +01:00
}
2023-02-12 12:36:52 +01:00
real_t MLPPDualSVC::model_test(std::vector<real_t> x) {
return evaluatev(x);
2023-01-24 19:00:54 +01:00
}
2023-02-12 12:36:52 +01:00
void MLPPDualSVC::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
MLPPCost mlpp_cost;
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;
2023-02-12 12:36:52 +01:00
forward_pass();
2023-01-24 19:00:54 +01:00
while (true) {
2023-02-12 12:36:52 +01:00
cost_prev = cost(_alpha, _input_set, _output_set);
2023-01-24 19:00:54 +01:00
2023-02-12 12:36:52 +01:00
_alpha = alg.subtraction(_alpha, alg.scalarMultiply(learning_rate, mlpp_cost.dualFormSVMDeriv(_alpha, _input_set, _output_set)));
2023-01-24 19:00:54 +01:00
2023-02-12 12:36:52 +01:00
alpha_projection();
2023-01-24 19:00:54 +01:00
// Calculating the bias
2023-01-27 13:01:16 +01:00
real_t biasGradient = 0;
2023-02-12 12:36:52 +01:00
for (uint32_t i = 0; i < _alpha.size(); i++) {
2023-01-27 13:01:16 +01:00
real_t sum = 0;
2023-02-12 12:36:52 +01:00
if (_alpha[i] < _C && _alpha[i] > 0) {
for (uint32_t j = 0; j < _alpha.size(); j++) {
if (_alpha[j] > 0) {
sum += _alpha[j] * _output_set[j] * alg.dot(_input_set[j], _input_set[i]); // TO DO: DON'T forget to add non-linear kernelizations.
2023-01-24 19:00:54 +01:00
}
}
}
2023-02-12 12:36:52 +01:00
biasGradient = (1 - _output_set[i] * sum) / _output_set[i];
2023-01-24 19:00:54 +01:00
break;
}
2023-02-12 12:36:52 +01:00
_bias -= biasGradient * learning_rate;
forward_pass();
2023-01-24 19:00:54 +01:00
// UI PORTION
2023-02-12 12:36:52 +01:00
if (ui) {
MLPPUtilities::CostInfo(epoch, cost_prev, cost(_alpha, _input_set, _output_set));
MLPPUtilities::UI(_alpha, _bias);
2023-01-24 19:00:54 +01:00
std::cout << score() << std::endl; // TO DO: DELETE THIS.
}
2023-02-12 12:36:52 +01:00
2023-01-24 19:00:54 +01:00
epoch++;
if (epoch > max_epoch) {
break;
}
}
}
2023-01-27 13:01:16 +01:00
// void MLPPDualSVC::SGD(real_t learning_rate, int max_epoch, bool UI){
2023-01-24 19:37:08 +01:00
// class MLPPCost cost;
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
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-02-12 12:36:52 +01:00
// cost_prev = Cost(alpha, _input_set[outputIndex], _output_set[outputIndex]);
2023-01-24 19:00:54 +01:00
// // Bias updation
// bias -= learning_rate * costDeriv;
2023-02-12 12:36:52 +01:00
// y_hat = Evaluate({_input_set[outputIndex]});
2023-01-24 19:00:54 +01:00
// if(UI) {
// MLPPUtilities::CostInfo(epoch, cost_prev, Cost(alpha));
// 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 MLPPDualSVC::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI){
2023-01-24 19:37:08 +01:00
// class MLPPCost cost;
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;
2023-02-12 12:36:52 +01:00
// auto [inputMiniBatches, outputMiniBatches] = MLPPUtilities::createMiniBatches(_input_set, _output_set, 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]);
// std::vector<real_t> z = propagate(inputMiniBatches[i]);
2023-01-24 19:00:54 +01:00
// cost_prev = Cost(z, outputMiniBatches[i], weights, C);
// // Calculating the weight gradients
// weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate/n, alg.mat_vec_mult(alg.transpose(inputMiniBatches[i]), cost.HingeLossDeriv(z, outputMiniBatches[i], C))));
// weights = regularization.regWeights(weights, learning_rate/n, 0, "Ridge");
// // Calculating the bias gradients
// bias -= learning_rate * alg.sum_elements(cost.HingeLossDeriv(y_hat, outputMiniBatches[i], C)) / n;
// forwardPass();
// y_hat = Evaluate(inputMiniBatches[i]);
// if(UI) {
// MLPPUtilities::CostInfo(epoch, cost_prev, Cost(z, outputMiniBatches[i], weights, C));
// 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 MLPPDualSVC::score() {
2023-02-10 22:33:32 +01:00
MLPPUtilities util;
2023-02-12 12:36:52 +01:00
return util.performance(_y_hat, _output_set);
2023-01-24 19:00:54 +01:00
}
2023-02-12 12:36:52 +01:00
MLPPDualSVC::MLPPDualSVC(std::vector<std::vector<real_t>> p_input_set, std::vector<real_t> p_output_set, real_t p_C, std::string p_kernel) {
_input_set = p_input_set;
_output_set = p_output_set;
_n = p_input_set.size();
_k = p_input_set[0].size();
_C = p_C;
_kernel = p_kernel;
_y_hat.resize(_n);
_bias = MLPPUtilities::biasInitialization();
_alpha = MLPPUtilities::weightInitialization(_n); // One alpha for all training examples, as per the lagrangian multipliers.
_K = kernel_functionm(_input_set, _input_set, _kernel); // For now this is unused. When non-linear kernels are added, the K will be manipulated.
}
MLPPDualSVC::MLPPDualSVC() {
}
MLPPDualSVC::~MLPPDualSVC() {
}
void MLPPDualSVC::save(std::string file_name) {
2023-02-10 22:33:32 +01:00
MLPPUtilities util;
2023-02-12 12:36:52 +01:00
util.saveParameters(file_name, _alpha, _bias);
2023-01-24 19:00:54 +01:00
}
2023-02-12 12:36:52 +01:00
real_t MLPPDualSVC::cost(std::vector<real_t> alpha, std::vector<std::vector<real_t>> X, std::vector<real_t> y) {
2023-01-24 19:37:08 +01:00
class MLPPCost cost;
2023-01-24 19:00:54 +01:00
return cost.dualFormSVM(alpha, X, y);
}
2023-02-12 12:36:52 +01:00
real_t MLPPDualSVC::evaluatev(std::vector<real_t> x) {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-02-12 12:36:52 +01:00
return avn.sign(propagatev(x));
2023-01-24 19:00:54 +01:00
}
2023-02-12 12:36:52 +01:00
real_t MLPPDualSVC::propagatev(std::vector<real_t> x) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-02-12 12:36:52 +01:00
real_t z = 0;
for (uint32_t j = 0; j < _alpha.size(); j++) {
if (_alpha[j] != 0) {
z += _alpha[j] * _output_set[j] * alg.dot(_input_set[j], x); // TO DO: DON'T forget to add non-linear kernelizations.
2023-01-24 19:00:54 +01:00
}
}
2023-02-12 12:36:52 +01:00
z += _bias;
2023-01-24 19:00:54 +01:00
return z;
}
2023-02-12 12:36:52 +01:00
std::vector<real_t> MLPPDualSVC::evaluatem(std::vector<std::vector<real_t>> X) {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-02-12 12:36:52 +01:00
return avn.sign(propagatem(X));
2023-01-24 19:00:54 +01:00
}
2023-02-12 12:36:52 +01:00
std::vector<real_t> MLPPDualSVC::propagatem(std::vector<std::vector<real_t>> X) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-02-12 12:36:52 +01:00
std::vector<real_t> z;
for (uint32_t i = 0; i < X.size(); i++) {
real_t sum = 0;
for (uint32_t j = 0; j < _alpha.size(); j++) {
if (_alpha[j] != 0) {
sum += _alpha[j] * _output_set[j] * alg.dot(_input_set[j], X[i]); // TO DO: DON'T forget to add non-linear kernelizations.
}
2023-01-24 19:00:54 +01:00
}
2023-02-12 12:36:52 +01:00
sum += _bias;
z.push_back(sum);
2023-01-24 19:00:54 +01:00
}
return z;
}
2023-02-12 12:36:52 +01:00
void MLPPDualSVC::forward_pass() {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-24 19:00:54 +01:00
2023-02-12 12:36:52 +01:00
_z = propagatem(_input_set);
_y_hat = avn.sign(_z);
2023-01-24 19:00:54 +01:00
}
2023-02-12 12:36:52 +01:00
void MLPPDualSVC::alpha_projection() {
for (uint32_t i = 0; i < _alpha.size(); i++) {
if (_alpha[i] > _C) {
_alpha[i] = _C;
} else if (_alpha[i] < 0) {
_alpha[i] = 0;
2023-01-24 19:00:54 +01:00
}
}
}
2023-02-12 12:36:52 +01:00
real_t MLPPDualSVC::kernel_functionv(std::vector<real_t> u, std::vector<real_t> v, std::string kernel) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-02-12 12:36:52 +01:00
2023-01-24 19:00:54 +01:00
if (kernel == "Linear") {
return alg.dot(u, v);
2023-02-10 22:33:32 +01:00
}
return 0;
2023-01-24 19:00:54 +01:00
}
2023-02-12 12:36:52 +01:00
std::vector<std::vector<real_t>> MLPPDualSVC::kernel_functionm(std::vector<std::vector<real_t>> A, std::vector<std::vector<real_t>> B, std::string kernel) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-24 19:00:54 +01:00
if (kernel == "Linear") {
2023-02-12 12:36:52 +01:00
return alg.matmult(_input_set, alg.transpose(_input_set));
2023-02-10 22:33:32 +01:00
}
return std::vector<std::vector<real_t>>();
2023-01-24 19:00:54 +01:00
}