mirror of
https://github.com/Relintai/pmlpp.git
synced 2024-12-22 15:06:47 +01:00
Cleaned up SVC.
This commit is contained in:
parent
6465280167
commit
da6324830d
320
mlpp/svc/svc.cpp
320
mlpp/svc/svc.cpp
@ -5,48 +5,84 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "svc.h"
|
#include "svc.h"
|
||||||
|
|
||||||
#include "../activation/activation.h"
|
#include "../activation/activation.h"
|
||||||
#include "../cost/cost.h"
|
#include "../cost/cost.h"
|
||||||
#include "../lin_alg/lin_alg.h"
|
#include "../lin_alg/lin_alg.h"
|
||||||
#include "../regularization/reg.h"
|
#include "../regularization/reg.h"
|
||||||
#include "../utilities/utilities.h"
|
#include "../utilities/utilities.h"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
std::vector<real_t> MLPPSVC::modelSetTest(std::vector<std::vector<real_t>> X) {
|
Ref<MLPPMatrix> MLPPSVC::get_input_set() {
|
||||||
return Evaluate(X);
|
return _input_set;
|
||||||
|
}
|
||||||
|
void MLPPSVC::set_input_set(const Ref<MLPPMatrix> &val) {
|
||||||
|
_input_set = val;
|
||||||
|
|
||||||
|
_initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPSVC::modelTest(std::vector<real_t> x) {
|
Ref<MLPPVector> MLPPSVC::get_output_set() {
|
||||||
return Evaluate(x);
|
return _output_set;
|
||||||
|
}
|
||||||
|
void MLPPSVC::set_output_set(const Ref<MLPPMatrix> &val) {
|
||||||
|
_output_set = val;
|
||||||
|
|
||||||
|
_initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPSVC::gradientDescent(real_t learning_rate, int max_epoch, bool UI) {
|
real_t MLPPSVC::get_c() {
|
||||||
class MLPPCost cost;
|
return _c;
|
||||||
|
}
|
||||||
|
void MLPPSVC::set_c(const real_t val) {
|
||||||
|
_c = val;
|
||||||
|
|
||||||
|
_initialized = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ref<MLPPVector> MLPPSVC::model_set_test(const Ref<MLPPMatrix> &X) {
|
||||||
|
ERR_FAIL_COND_V(!_initialized, Ref<MLPPVector>());
|
||||||
|
|
||||||
|
return evaluatem(X);
|
||||||
|
}
|
||||||
|
|
||||||
|
real_t MLPPSVC::model_test(const Ref<MLPPVector> &x) {
|
||||||
|
ERR_FAIL_COND_V(!_initialized, 0);
|
||||||
|
|
||||||
|
return evaluatev(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MLPPSVC::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
|
||||||
|
ERR_FAIL_COND(!_initialized);
|
||||||
|
|
||||||
|
MLPPCost mlpp_cost;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPReg regularization;
|
MLPPReg regularization;
|
||||||
|
|
||||||
real_t cost_prev = 0;
|
real_t cost_prev = 0;
|
||||||
int epoch = 1;
|
int epoch = 1;
|
||||||
forwardPass();
|
|
||||||
|
forward_pass();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
cost_prev = Cost(y_hat, outputSet, weights, C);
|
cost_prev = cost(_y_hat, _output_set, _weights, _c);
|
||||||
|
|
||||||
weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate / n, alg.mat_vec_mult(alg.transpose(inputSet), cost.HingeLossDeriv(z, outputSet, C))));
|
_weights = alg.subtractionnv(_weights, alg.scalar_multiplynv(learning_rate / _n, alg.mat_vec_multv(alg.transposem(_input_set), mlpp_cost.hinge_loss_derivwv(_z, _output_set, _c))));
|
||||||
weights = regularization.regWeights(weights, learning_rate / n, 0, "Ridge");
|
_weights = regularization.reg_weightsv(_weights, learning_rate / _n, 0, MLPPReg::REGULARIZATION_TYPE_RIDGE);
|
||||||
|
|
||||||
// Calculating the bias gradients
|
// Calculating the bias gradients
|
||||||
bias += learning_rate * alg.sum_elements(cost.HingeLossDeriv(y_hat, outputSet, C)) / n;
|
_bias += learning_rate * alg.sum_elementsv(mlpp_cost.hinge_loss_derivwv(_y_hat, _output_set, _c)) / _n;
|
||||||
|
|
||||||
forwardPass();
|
forward_pass();
|
||||||
|
|
||||||
// UI PORTION
|
// UI PORTION
|
||||||
if (UI) {
|
if (ui) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputSet, weights, C));
|
MLPPUtilities::cost_info(epoch, cost_prev, cost(_y_hat, _output_set, _weights, _c));
|
||||||
MLPPUtilities::UI(weights, bias);
|
MLPPUtilities::print_ui_vb(_weights, _bias);
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
|
|
||||||
if (epoch > max_epoch) {
|
if (epoch > max_epoch) {
|
||||||
@ -55,39 +91,66 @@ void MLPPSVC::gradientDescent(real_t learning_rate, int max_epoch, bool UI) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPSVC::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
void MLPPSVC::sgd(real_t learning_rate, int max_epoch, bool ui) {
|
||||||
class MLPPCost cost;
|
ERR_FAIL_COND(!_initialized);
|
||||||
|
|
||||||
|
MLPPCost mlpp_cost;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPReg regularization;
|
MLPPReg regularization;
|
||||||
|
|
||||||
|
std::random_device rd;
|
||||||
|
std::default_random_engine generator(rd());
|
||||||
|
std::uniform_int_distribution<int> distribution(0, int(_n - 1));
|
||||||
|
|
||||||
|
Ref<MLPPVector> input_set_row_tmp;
|
||||||
|
input_set_row_tmp.instance();
|
||||||
|
input_set_row_tmp->resize(_input_set->size().x);
|
||||||
|
|
||||||
|
Ref<MLPPVector> output_set_row_tmp;
|
||||||
|
output_set_row_tmp.instance();
|
||||||
|
output_set_row_tmp->resize(1);
|
||||||
|
|
||||||
|
Ref<MLPPVector> z_row_tmp;
|
||||||
|
z_row_tmp.instance();
|
||||||
|
z_row_tmp->resize(1);
|
||||||
|
|
||||||
real_t cost_prev = 0;
|
real_t cost_prev = 0;
|
||||||
int epoch = 1;
|
int epoch = 1;
|
||||||
|
|
||||||
|
forward_pass();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
std::random_device rd;
|
int output_index = distribution(generator);
|
||||||
std::default_random_engine generator(rd());
|
|
||||||
std::uniform_int_distribution<int> distribution(0, int(n - 1));
|
|
||||||
int outputIndex = distribution(generator);
|
|
||||||
|
|
||||||
//real_t y_hat = Evaluate(inputSet[outputIndex]);
|
_input_set->get_row_into_mlpp_vector(output_index, input_set_row_tmp);
|
||||||
real_t z = propagate(inputSet[outputIndex]);
|
|
||||||
cost_prev = Cost({ z }, { outputSet[outputIndex] }, weights, C);
|
|
||||||
|
|
||||||
real_t costDeriv = cost.HingeLossDeriv(std::vector<real_t>({ z }), std::vector<real_t>({ outputSet[outputIndex] }), C)[0]; // Explicit conversion to avoid ambiguity with overloaded function. Error occured on Ubuntu.
|
real_t output_set_indx = _output_set->get_element(output_index);
|
||||||
|
output_set_row_tmp->set_element(0, output_set_indx);
|
||||||
|
|
||||||
|
//real_t y_hat = Evaluate(input_set_row_tmp);
|
||||||
|
real_t z = propagatev(input_set_row_tmp);
|
||||||
|
|
||||||
|
z_row_tmp->set_element(0, z);
|
||||||
|
|
||||||
|
cost_prev = cost(z_row_tmp, output_set_row_tmp, _weights, _c);
|
||||||
|
|
||||||
|
Ref<MLPPVector> cost_deriv_vec = mlpp_cost.hinge_loss_derivwv(z_row_tmp, output_set_row_tmp, _c);
|
||||||
|
|
||||||
|
real_t cost_deriv = cost_deriv_vec->get_element(0);
|
||||||
|
|
||||||
// Weight Updation
|
// Weight Updation
|
||||||
weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate * costDeriv, inputSet[outputIndex]));
|
_weights = alg.subtractionnv(_weights, alg.scalar_multiplynv(learning_rate * cost_deriv, input_set_row_tmp));
|
||||||
weights = regularization.regWeights(weights, learning_rate, 0, "Ridge");
|
_weights = regularization.reg_weightsv(_weights, learning_rate, 0, MLPPReg::REGULARIZATION_TYPE_RIDGE);
|
||||||
|
|
||||||
// Bias updation
|
// Bias updation
|
||||||
bias -= learning_rate * costDeriv;
|
_bias -= learning_rate * cost_deriv;
|
||||||
|
|
||||||
//y_hat = Evaluate({ inputSet[outputIndex] });
|
//y_hat = Evaluate({ _input_set[output_index] });
|
||||||
|
|
||||||
if (UI) {
|
if (ui) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost({ z }, { outputSet[outputIndex] }, weights, C));
|
MLPPUtilities::cost_info(epoch, cost_prev, cost(z_row_tmp, output_set_row_tmp, _weights, _c));
|
||||||
MLPPUtilities::UI(weights, bias);
|
MLPPUtilities::print_ui_vb(_weights, _bias);
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
@ -96,108 +159,207 @@ void MLPPSVC::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
forwardPass();
|
|
||||||
|
forward_pass();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPSVC::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) {
|
void MLPPSVC::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui) {
|
||||||
class MLPPCost cost;
|
ERR_FAIL_COND(!_initialized);
|
||||||
|
|
||||||
|
MLPPCost mlpp_cost;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPReg regularization;
|
MLPPReg regularization;
|
||||||
|
|
||||||
real_t cost_prev = 0;
|
real_t cost_prev = 0;
|
||||||
int epoch = 1;
|
int epoch = 1;
|
||||||
|
|
||||||
// Creating the mini-batches
|
// Creating the mini-batches
|
||||||
int n_mini_batch = n / mini_batch_size;
|
int n_mini_batch = _n / mini_batch_size;
|
||||||
auto batches = MLPPUtilities::createMiniBatches(inputSet, outputSet, n_mini_batch);
|
MLPPUtilities::CreateMiniBatchMVBatch batches = MLPPUtilities::create_mini_batchesmv(_input_set, _output_set, n_mini_batch);
|
||||||
auto inputMiniBatches = std::get<0>(batches);
|
|
||||||
auto outputMiniBatches = std::get<1>(batches);
|
forward_pass();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
for (int i = 0; i < n_mini_batch; i++) {
|
for (int i = 0; i < n_mini_batch; i++) {
|
||||||
std::vector<real_t> y_hat = Evaluate(inputMiniBatches[i]);
|
Ref<MLPPMatrix> current_input_batch_entry = batches.input_sets[i];
|
||||||
std::vector<real_t> z = propagate(inputMiniBatches[i]);
|
Ref<MLPPVector> current_output_batch_entry = batches.output_sets[i];
|
||||||
cost_prev = Cost(z, outputMiniBatches[i], weights, C);
|
|
||||||
|
Ref<MLPPVector> y_hat = evaluatem(current_input_batch_entry);
|
||||||
|
Ref<MLPPVector> z = propagatem(current_input_batch_entry);
|
||||||
|
cost_prev = cost(z, current_output_batch_entry, _weights, _c);
|
||||||
|
|
||||||
// Calculating the weight gradients
|
// 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 = alg.subtractionnv(_weights, alg.scalar_multiplynv(learning_rate / _n, alg.mat_vec_multv(alg.transposem(current_input_batch_entry), mlpp_cost.hinge_loss_derivwv(z, current_output_batch_entry, _c))));
|
||||||
weights = regularization.regWeights(weights, learning_rate / n, 0, "Ridge");
|
_weights = regularization.reg_weightsv(_weights, learning_rate / _n, 0, MLPPReg::REGULARIZATION_TYPE_RIDGE);
|
||||||
|
|
||||||
// Calculating the bias gradients
|
// Calculating the bias gradients
|
||||||
bias -= learning_rate * alg.sum_elements(cost.HingeLossDeriv(y_hat, outputMiniBatches[i], C)) / n;
|
_bias -= learning_rate * alg.sum_elementsv(mlpp_cost.hinge_loss_derivwv(y_hat, current_output_batch_entry, _c)) / _n;
|
||||||
|
|
||||||
forwardPass();
|
forward_pass();
|
||||||
|
|
||||||
y_hat = Evaluate(inputMiniBatches[i]);
|
y_hat = evaluatem(current_input_batch_entry);
|
||||||
|
|
||||||
if (UI) {
|
if (ui) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(z, outputMiniBatches[i], weights, C));
|
MLPPUtilities::cost_info(epoch, cost_prev, cost(z, current_output_batch_entry, _weights, _c));
|
||||||
MLPPUtilities::UI(weights, bias);
|
MLPPUtilities::print_ui_vb(_weights, _bias);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
|
|
||||||
if (epoch > max_epoch) {
|
if (epoch > max_epoch) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
forwardPass();
|
|
||||||
|
forward_pass();
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPSVC::score() {
|
real_t MLPPSVC::score() {
|
||||||
|
ERR_FAIL_COND_V(!_initialized, 0);
|
||||||
|
|
||||||
MLPPUtilities util;
|
MLPPUtilities util;
|
||||||
return util.performance(y_hat, outputSet);
|
return util.performance_vec(_y_hat, _output_set);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPSVC::save(std::string fileName) {
|
void MLPPSVC::save(const String &file_name) {
|
||||||
|
ERR_FAIL_COND(!_initialized);
|
||||||
|
|
||||||
MLPPUtilities util;
|
MLPPUtilities util;
|
||||||
util.saveParameters(fileName, weights, bias);
|
|
||||||
|
//util.saveParameters(_file_name, _weights, _bias);
|
||||||
}
|
}
|
||||||
|
|
||||||
MLPPSVC::MLPPSVC(std::vector<std::vector<real_t>> p_inputSet, std::vector<real_t> p_outputSet, real_t p_C) {
|
bool MLPPSVC::is_initialized() {
|
||||||
inputSet = p_inputSet;
|
return _initialized;
|
||||||
outputSet = p_outputSet;
|
}
|
||||||
n = inputSet.size();
|
void MLPPSVC::initialize() {
|
||||||
k = inputSet[0].size();
|
if (_initialized) {
|
||||||
C = p_C;
|
return;
|
||||||
|
|
||||||
y_hat.resize(n);
|
|
||||||
weights = MLPPUtilities::weightInitialization(k);
|
|
||||||
bias = MLPPUtilities::biasInitialization();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPSVC::Cost(std::vector<real_t> z, std::vector<real_t> y, std::vector<real_t> weights, real_t C) {
|
ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
|
||||||
class MLPPCost cost;
|
|
||||||
return cost.HingeLoss(z, y, weights, C);
|
_n = _input_set->size().y;
|
||||||
|
_k = _input_set->size().x;
|
||||||
|
|
||||||
|
if (!_y_hat.is_valid()) {
|
||||||
|
_y_hat.instance();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<real_t> MLPPSVC::Evaluate(std::vector<std::vector<real_t>> X) {
|
_y_hat->resize(_n);
|
||||||
|
|
||||||
|
MLPPUtilities util;
|
||||||
|
|
||||||
|
if (!_weights.is_valid()) {
|
||||||
|
_weights.instance();
|
||||||
|
}
|
||||||
|
|
||||||
|
_weights->resize(_k);
|
||||||
|
|
||||||
|
util.weight_initializationv(_weights);
|
||||||
|
_bias = util.bias_initializationr();
|
||||||
|
|
||||||
|
_initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
MLPPSVC::MLPPSVC(const Ref<MLPPMatrix> &input_set, const Ref<MLPPVector> &output_set, real_t c) {
|
||||||
|
_input_set = input_set;
|
||||||
|
_output_set = output_set;
|
||||||
|
|
||||||
|
_n = _input_set->size().y;
|
||||||
|
_k = _input_set->size().x;
|
||||||
|
_c = c;
|
||||||
|
|
||||||
|
_y_hat.instance();
|
||||||
|
|
||||||
|
_y_hat->resize(_n);
|
||||||
|
|
||||||
|
MLPPUtilities util;
|
||||||
|
|
||||||
|
_weights.instance();
|
||||||
|
_weights->resize(_k);
|
||||||
|
util.weight_initializationv(_weights);
|
||||||
|
_bias = util.bias_initializationr();
|
||||||
|
|
||||||
|
_initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
MLPPSVC::MLPPSVC() {
|
||||||
|
_y_hat.instance();
|
||||||
|
_weights.instance();
|
||||||
|
|
||||||
|
_c = 0;
|
||||||
|
_n = 0;
|
||||||
|
_k = 0;
|
||||||
|
|
||||||
|
_initialized = false;
|
||||||
|
}
|
||||||
|
MLPPSVC::~MLPPSVC() {
|
||||||
|
}
|
||||||
|
|
||||||
|
real_t MLPPSVC::cost(const Ref<MLPPVector> &z, const Ref<MLPPVector> &y, const Ref<MLPPVector> &weights, real_t c) {
|
||||||
|
MLPPCost mlpp_cost;
|
||||||
|
return mlpp_cost.hinge_losswv(z, y, weights, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ref<MLPPVector> MLPPSVC::evaluatem(const Ref<MLPPMatrix> &X) {
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
return avn.sign(alg.scalarAdd(bias, alg.mat_vec_mult(X, weights)));
|
return avn.sign_normv(alg.scalar_addnv(_bias, alg.mat_vec_multv(X, _weights)));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<real_t> MLPPSVC::propagate(std::vector<std::vector<real_t>> X) {
|
Ref<MLPPVector> MLPPSVC::propagatem(const Ref<MLPPMatrix> &X) {
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
return alg.scalarAdd(bias, alg.mat_vec_mult(X, weights));
|
return alg.scalar_addnv(_bias, alg.mat_vec_multv(X, _weights));
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPSVC::Evaluate(std::vector<real_t> x) {
|
real_t MLPPSVC::evaluatev(const Ref<MLPPVector> &x) {
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
return avn.sign(alg.dot(weights, x) + bias);
|
return avn.sign_normr(alg.dotv(_weights, x) + _bias);
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPSVC::propagate(std::vector<real_t> x) {
|
real_t MLPPSVC::propagatev(const Ref<MLPPVector> &x) {
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
return alg.dot(weights, x) + bias;
|
return alg.dotv(_weights, x) + _bias;
|
||||||
}
|
}
|
||||||
|
|
||||||
// sign ( wTx + b )
|
// sign ( wTx + b )
|
||||||
void MLPPSVC::forwardPass() {
|
void MLPPSVC::forward_pass() {
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
|
|
||||||
z = propagate(inputSet);
|
_z = propagatem(_input_set);
|
||||||
y_hat = avn.sign(z);
|
_y_hat = avn.sign_normv(_z);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MLPPSVC::_bind_methods() {
|
||||||
|
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPSVC::get_input_set);
|
||||||
|
ClassDB::bind_method(D_METHOD("set_input_set", "val"), &MLPPSVC::set_input_set);
|
||||||
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "input_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_input_set", "get_input_set");
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("get_output_set"), &MLPPSVC::get_output_set);
|
||||||
|
ClassDB::bind_method(D_METHOD("set_output_set", "val"), &MLPPSVC::set_output_set);
|
||||||
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "output_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "set_output_set", "get_output_set");
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("get_c"), &MLPPSVC::get_c);
|
||||||
|
ClassDB::bind_method(D_METHOD("set_c", "val"), &MLPPSVC::set_c);
|
||||||
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "c"), "set_c", "get_c");
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("model_set_test", "X"), &MLPPSVC::model_set_test);
|
||||||
|
ClassDB::bind_method(D_METHOD("model_test", "x"), &MLPPSVC::model_test);
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("gradient_descent", "learning_rate", "max_epoch", "ui"), &MLPPSVC::gradient_descent, false);
|
||||||
|
ClassDB::bind_method(D_METHOD("sgd", "learning_rate", "max_epoch", "ui"), &MLPPSVC::sgd, false);
|
||||||
|
ClassDB::bind_method(D_METHOD("mbgd", "learning_rate", "max_epoch", "mini_batch_size", "ui"), &MLPPSVC::mbgd, false);
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("score"), &MLPPSVC::score);
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("save", "file_name"), &MLPPSVC::save);
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("is_initialized"), &MLPPSVC::is_initialized);
|
||||||
|
ClassDB::bind_method(D_METHOD("initialize"), &MLPPSVC::initialize);
|
||||||
}
|
}
|
||||||
|
@ -13,43 +13,71 @@
|
|||||||
|
|
||||||
#include "core/math/math_defs.h"
|
#include "core/math/math_defs.h"
|
||||||
|
|
||||||
#include <string>
|
#include "core/object/reference.h"
|
||||||
#include <vector>
|
|
||||||
|
#include "../lin_alg/mlpp_matrix.h"
|
||||||
|
#include "../lin_alg/mlpp_vector.h"
|
||||||
|
|
||||||
|
#include "../regularization/reg.h"
|
||||||
|
|
||||||
|
class MLPPSVC : public Reference {
|
||||||
|
GDCLASS(MLPPSVC, Reference);
|
||||||
|
|
||||||
class MLPPSVC {
|
|
||||||
public:
|
public:
|
||||||
std::vector<real_t> modelSetTest(std::vector<std::vector<real_t>> X);
|
Ref<MLPPMatrix> get_input_set();
|
||||||
real_t modelTest(std::vector<real_t> x);
|
void set_input_set(const Ref<MLPPMatrix> &val);
|
||||||
void gradientDescent(real_t learning_rate, int max_epoch, bool UI = false);
|
|
||||||
void SGD(real_t learning_rate, int max_epoch, bool UI = false);
|
Ref<MLPPVector> get_output_set();
|
||||||
void MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI = false);
|
void set_output_set(const Ref<MLPPMatrix> &val);
|
||||||
|
|
||||||
|
real_t get_c();
|
||||||
|
void set_c(const real_t val);
|
||||||
|
|
||||||
|
Ref<MLPPVector> model_set_test(const Ref<MLPPMatrix> &X);
|
||||||
|
real_t model_test(const Ref<MLPPVector> &x);
|
||||||
|
|
||||||
|
void gradient_descent(real_t learning_rate, int max_epoch, bool ui = false);
|
||||||
|
void sgd(real_t learning_rate, int max_epoch, bool ui = false);
|
||||||
|
void mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui = false);
|
||||||
|
|
||||||
real_t score();
|
real_t score();
|
||||||
void save(std::string fileName);
|
|
||||||
|
|
||||||
MLPPSVC(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, real_t C);
|
void save(const String &file_name);
|
||||||
|
|
||||||
private:
|
bool is_initialized();
|
||||||
real_t Cost(std::vector<real_t> y_hat, std::vector<real_t> y, std::vector<real_t> weights, real_t C);
|
void initialize();
|
||||||
|
|
||||||
std::vector<real_t> Evaluate(std::vector<std::vector<real_t>> X);
|
MLPPSVC(const Ref<MLPPMatrix> &input_set, const Ref<MLPPVector> &output_set, real_t c);
|
||||||
std::vector<real_t> propagate(std::vector<std::vector<real_t>> X);
|
|
||||||
real_t Evaluate(std::vector<real_t> x);
|
|
||||||
real_t propagate(std::vector<real_t> x);
|
|
||||||
void forwardPass();
|
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> inputSet;
|
MLPPSVC();
|
||||||
std::vector<real_t> outputSet;
|
~MLPPSVC();
|
||||||
std::vector<real_t> z;
|
|
||||||
std::vector<real_t> y_hat;
|
|
||||||
std::vector<real_t> weights;
|
|
||||||
real_t bias;
|
|
||||||
|
|
||||||
real_t C;
|
protected:
|
||||||
int n;
|
real_t cost(const Ref<MLPPVector> &z, const Ref<MLPPVector> &y, const Ref<MLPPVector> &weights, real_t c);
|
||||||
int k;
|
|
||||||
|
|
||||||
// UI Portion
|
Ref<MLPPVector> evaluatem(const Ref<MLPPMatrix> &X);
|
||||||
void UI(int epoch, real_t cost_prev);
|
Ref<MLPPVector> propagatem(const Ref<MLPPMatrix> &X);
|
||||||
|
|
||||||
|
real_t evaluatev(const Ref<MLPPVector> &x);
|
||||||
|
real_t propagatev(const Ref<MLPPVector> &x);
|
||||||
|
|
||||||
|
void forward_pass();
|
||||||
|
|
||||||
|
static void _bind_methods();
|
||||||
|
|
||||||
|
Ref<MLPPMatrix> _input_set;
|
||||||
|
Ref<MLPPVector> _output_set;
|
||||||
|
|
||||||
|
Ref<MLPPVector> _z;
|
||||||
|
Ref<MLPPVector> _y_hat;
|
||||||
|
Ref<MLPPVector> _weights;
|
||||||
|
real_t _bias;
|
||||||
|
|
||||||
|
real_t _c;
|
||||||
|
int _n;
|
||||||
|
int _k;
|
||||||
|
|
||||||
|
bool _initialized;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SVC_hpp */
|
#endif /* SVC_hpp */
|
||||||
|
@ -43,6 +43,7 @@ SOFTWARE.
|
|||||||
#include "mlpp/uni_lin_reg/uni_lin_reg.h"
|
#include "mlpp/uni_lin_reg/uni_lin_reg.h"
|
||||||
#include "mlpp/wgan/wgan.h"
|
#include "mlpp/wgan/wgan.h"
|
||||||
#include "mlpp/probit_reg/probit_reg.h"
|
#include "mlpp/probit_reg/probit_reg.h"
|
||||||
|
#include "mlpp/svc/svc.h"
|
||||||
|
|
||||||
#include "mlpp/mlp/mlp.h"
|
#include "mlpp/mlp/mlp.h"
|
||||||
|
|
||||||
@ -71,6 +72,7 @@ void register_pmlpp_types(ModuleRegistrationLevel p_level) {
|
|||||||
ClassDB::register_class<MLPPUniLinReg>();
|
ClassDB::register_class<MLPPUniLinReg>();
|
||||||
ClassDB::register_class<MLPPOutlierFinder>();
|
ClassDB::register_class<MLPPOutlierFinder>();
|
||||||
ClassDB::register_class<MLPPProbitReg>();
|
ClassDB::register_class<MLPPProbitReg>();
|
||||||
|
ClassDB::register_class<MLPPSVC>();
|
||||||
|
|
||||||
ClassDB::register_class<MLPPDataESimple>();
|
ClassDB::register_class<MLPPDataESimple>();
|
||||||
ClassDB::register_class<MLPPDataSimple>();
|
ClassDB::register_class<MLPPDataSimple>();
|
||||||
|
@ -51,9 +51,9 @@
|
|||||||
#include "../mlpp/outlier_finder/outlier_finder_old.h"
|
#include "../mlpp/outlier_finder/outlier_finder_old.h"
|
||||||
#include "../mlpp/pca/pca_old.h"
|
#include "../mlpp/pca/pca_old.h"
|
||||||
#include "../mlpp/probit_reg/probit_reg_old.h"
|
#include "../mlpp/probit_reg/probit_reg_old.h"
|
||||||
|
#include "../mlpp/svc/svc_old.h"
|
||||||
#include "../mlpp/uni_lin_reg/uni_lin_reg_old.h"
|
#include "../mlpp/uni_lin_reg/uni_lin_reg_old.h"
|
||||||
#include "../mlpp/wgan/wgan_old.h"
|
#include "../mlpp/wgan/wgan_old.h"
|
||||||
#include "../mlpp/svc/svc_old.h"
|
|
||||||
|
|
||||||
Vector<real_t> dstd_vec_to_vec(const std::vector<real_t> &in) {
|
Vector<real_t> dstd_vec_to_vec(const std::vector<real_t> &in) {
|
||||||
Vector<real_t> r;
|
Vector<real_t> r;
|
||||||
@ -414,10 +414,16 @@ void MLPPTests::test_support_vector_classification(bool ui) {
|
|||||||
|
|
||||||
// SUPPORT VECTOR CLASSIFICATION
|
// SUPPORT VECTOR CLASSIFICATION
|
||||||
Ref<MLPPDataSimple> dt = data.load_breast_cancer_svc(_breast_cancer_svm_data_path);
|
Ref<MLPPDataSimple> dt = data.load_breast_cancer_svc(_breast_cancer_svm_data_path);
|
||||||
|
|
||||||
MLPPSVCOld model_old(dt->get_input()->to_std_vector(), dt->get_output()->to_std_vector(), ui);
|
MLPPSVCOld model_old(dt->get_input()->to_std_vector(), dt->get_output()->to_std_vector(), ui);
|
||||||
model_old.SGD(0.00001, 100000, ui);
|
model_old.SGD(0.00001, 100000, ui);
|
||||||
alg.printVector(model_old.modelSetTest(dt->get_input()->to_std_vector()));
|
alg.printVector(model_old.modelSetTest(dt->get_input()->to_std_vector()));
|
||||||
std::cout << "ACCURACY: " << 100 * model_old.score() << "%" << std::endl;
|
std::cout << "ACCURACY (old): " << 100 * model_old.score() << "%" << std::endl;
|
||||||
|
|
||||||
|
MLPPSVC model(dt->get_input(), dt->get_output(), ui);
|
||||||
|
model.sgd(0.00001, 100000, ui);
|
||||||
|
PLOG_MSG((model.model_set_test(dt->get_input())->to_string()));
|
||||||
|
PLOG_MSG("ACCURACY: " + String::num(100 * model.score()) + "%");
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPTests::test_mlp(bool ui) {
|
void MLPPTests::test_mlp(bool ui) {
|
||||||
|
Loading…
Reference in New Issue
Block a user