pmlpp/mlpp/dual_svc/dual_svc.cpp

247 lines
6.9 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-10 22:33:32 +01:00
MLPPDualSVC::MLPPDualSVC(std::vector<std::vector<real_t>> p_inputSet, std::vector<real_t> p_outputSet, real_t p_C, std::string p_kernel) {
inputSet = p_inputSet;
outputSet = p_outputSet;
n = p_inputSet.size();
k = p_inputSet[0].size();
C = p_C;
kernel = p_kernel;
2023-01-24 19:20:18 +01:00
2023-01-24 19:00:54 +01:00
y_hat.resize(n);
bias = MLPPUtilities::biasInitialization();
alpha = MLPPUtilities::weightInitialization(n); // One alpha for all training examples, as per the lagrangian multipliers.
2023-01-24 19:00:54 +01:00
K = kernelFunction(inputSet, inputSet, kernel); // For now this is unused. When non-linear kernels are added, the K will be manipulated.
}
2023-01-27 13:01:16 +01:00
std::vector<real_t> MLPPDualSVC::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 MLPPDualSVC::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 MLPPDualSVC::gradientDescent(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-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(alpha, inputSet, outputSet);
alpha = alg.subtraction(alpha, alg.scalarMultiply(learning_rate, cost.dualFormSVMDeriv(alpha, inputSet, outputSet)));
alphaProjection();
// Calculating the bias
2023-01-27 13:01:16 +01:00
real_t biasGradient = 0;
2023-02-10 22:33:32 +01:00
for (uint32_t i = 0; i < alpha.size(); i++) {
2023-01-27 13:01:16 +01:00
real_t sum = 0;
2023-01-24 19:00:54 +01:00
if (alpha[i] < C && alpha[i] > 0) {
2023-02-10 22:33:32 +01:00
for (uint32_t j = 0; j < alpha.size(); j++) {
2023-01-24 19:00:54 +01:00
if (alpha[j] > 0) {
sum += alpha[j] * outputSet[j] * alg.dot(inputSet[j], inputSet[i]); // TO DO: DON'T forget to add non-linear kernelizations.
}
}
}
biasGradient = (1 - outputSet[i] * sum) / outputSet[i];
break;
}
bias -= biasGradient * learning_rate;
forwardPass();
// UI PORTION
if (UI) {
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(alpha, inputSet, outputSet));
MLPPUtilities::UI(alpha, bias);
2023-01-24 19:00:54 +01:00
std::cout << score() << std::endl; // TO DO: DELETE THIS.
}
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);
// cost_prev = Cost(alpha, inputSet[outputIndex], outputSet[outputIndex]);
// // Bias updation
// bias -= learning_rate * costDeriv;
// y_hat = Evaluate({inputSet[outputIndex]});
// 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;
// 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]);
// 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-01-24 19:00:54 +01:00
return util.performance(y_hat, outputSet);
}
2023-01-25 00:21:31 +01:00
void MLPPDualSVC::save(std::string fileName) {
2023-02-10 22:33:32 +01:00
MLPPUtilities util;
2023-01-24 19:00:54 +01:00
util.saveParameters(fileName, alpha, bias);
}
2023-01-27 13:01:16 +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-01-27 13:01:16 +01:00
std::vector<real_t> MLPPDualSVC::Evaluate(std::vector<std::vector<real_t>> X) {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-24 19:00:54 +01:00
return avn.sign(propagate(X));
}
2023-01-27 13:01:16 +01:00
std::vector<real_t> MLPPDualSVC::propagate(std::vector<std::vector<real_t>> X) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-27 13:01:16 +01:00
std::vector<real_t> z;
2023-02-10 22:33:32 +01:00
for (uint32_t i = 0; i < X.size(); i++) {
2023-01-27 13:01:16 +01:00
real_t sum = 0;
2023-02-10 22:33:32 +01:00
for (uint32_t j = 0; j < alpha.size(); j++) {
2023-01-24 19:00:54 +01:00
if (alpha[j] != 0) {
sum += alpha[j] * outputSet[j] * alg.dot(inputSet[j], X[i]); // TO DO: DON'T forget to add non-linear kernelizations.
}
}
sum += bias;
z.push_back(sum);
}
return z;
}
2023-01-27 13:01:16 +01:00
real_t MLPPDualSVC::Evaluate(std::vector<real_t> x) {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-24 19:00:54 +01:00
return avn.sign(propagate(x));
}
2023-01-27 13:01:16 +01:00
real_t MLPPDualSVC::propagate(std::vector<real_t> x) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-27 13:01:16 +01:00
real_t z = 0;
2023-02-10 22:33:32 +01:00
for (uint32_t j = 0; j < alpha.size(); j++) {
2023-01-24 19:00:54 +01:00
if (alpha[j] != 0) {
z += alpha[j] * outputSet[j] * alg.dot(inputSet[j], x); // TO DO: DON'T forget to add non-linear kernelizations.
}
}
z += bias;
return z;
}
2023-01-25 00:21:31 +01:00
void MLPPDualSVC::forwardPass() {
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-24 19:00:54 +01:00
z = propagate(inputSet);
y_hat = avn.sign(z);
}
2023-01-25 00:21:31 +01:00
void MLPPDualSVC::alphaProjection() {
2023-02-10 22:33:32 +01:00
for (uint32_t i = 0; i < alpha.size(); i++) {
2023-01-24 19:00:54 +01:00
if (alpha[i] > C) {
alpha[i] = C;
} else if (alpha[i] < 0) {
alpha[i] = 0;
}
}
}
2023-01-27 13:01:16 +01:00
real_t MLPPDualSVC::kernelFunction(std::vector<real_t> u, std::vector<real_t> v, std::string kernel) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
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-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> MLPPDualSVC::kernelFunction(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") {
return alg.matmult(inputSet, alg.transpose(inputSet));
2023-02-10 22:33:32 +01:00
}
return std::vector<std::vector<real_t>>();
2023-01-24 19:00:54 +01:00
}