mirror of
https://github.com/Relintai/pmlpp.git
synced 2024-12-22 15:06:47 +01:00
Cleaned up SoftmaxReg.
This commit is contained in:
parent
022a330ff6
commit
17d3f486ae
@ -5,61 +5,108 @@
|
||||
//
|
||||
|
||||
#include "softmax_reg.h"
|
||||
|
||||
#include "../activation/activation.h"
|
||||
#include "../cost/cost.h"
|
||||
#include "../lin_alg/lin_alg.h"
|
||||
#include "../regularization/reg.h"
|
||||
#include "../utilities/utilities.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
|
||||
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) :
|
||||
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);
|
||||
weights = MLPPUtilities::weightInitialization(k, n_class);
|
||||
bias = MLPPUtilities::biasInitialization(n_class);
|
||||
Ref<MLPPMatrix> MLPPSoftmaxReg::get_input_set() {
|
||||
return _input_set;
|
||||
}
|
||||
void MLPPSoftmaxReg::set_input_set(const Ref<MLPPMatrix> &val) {
|
||||
_input_set = val;
|
||||
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
std::vector<real_t> MLPPSoftmaxReg::modelTest(std::vector<real_t> x) {
|
||||
return Evaluate(x);
|
||||
Ref<MLPPMatrix> MLPPSoftmaxReg::get_output_set() {
|
||||
return _output_set;
|
||||
}
|
||||
void MLPPSoftmaxReg::set_output_set(const Ref<MLPPMatrix> &val) {
|
||||
_output_set = val;
|
||||
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
std::vector<std::vector<real_t>> MLPPSoftmaxReg::modelSetTest(std::vector<std::vector<real_t>> X) {
|
||||
return Evaluate(X);
|
||||
MLPPReg::RegularizationType MLPPSoftmaxReg::get_reg() {
|
||||
return _reg;
|
||||
}
|
||||
void MLPPSoftmaxReg::set_reg(const MLPPReg::RegularizationType val) {
|
||||
_reg = val;
|
||||
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
void MLPPSoftmaxReg::gradientDescent(real_t learning_rate, int max_epoch, bool UI) {
|
||||
real_t MLPPSoftmaxReg::get_lambda() {
|
||||
return _lambda;
|
||||
}
|
||||
void MLPPSoftmaxReg::set_lambda(const real_t val) {
|
||||
_lambda = val;
|
||||
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
real_t MLPPSoftmaxReg::get_alpha() {
|
||||
return _alpha;
|
||||
}
|
||||
void MLPPSoftmaxReg::set_alpha(const real_t val) {
|
||||
_alpha = val;
|
||||
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
Ref<MLPPVector> MLPPSoftmaxReg::model_test(const Ref<MLPPVector> &x) {
|
||||
ERR_FAIL_COND_V(!_initialized, Ref<MLPPVector>());
|
||||
|
||||
return evaluatev(x);
|
||||
}
|
||||
|
||||
Ref<MLPPMatrix> MLPPSoftmaxReg::model_set_test(const Ref<MLPPMatrix> &X) {
|
||||
ERR_FAIL_COND_V(!_initialized, Ref<MLPPMatrix>());
|
||||
|
||||
return evaluatem(X);
|
||||
}
|
||||
|
||||
void MLPPSoftmaxReg::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
|
||||
ERR_FAIL_COND(!_initialized);
|
||||
|
||||
MLPPLinAlg alg;
|
||||
MLPPReg regularization;
|
||||
real_t cost_prev = 0;
|
||||
int epoch = 1;
|
||||
forwardPass();
|
||||
|
||||
forward_pass();
|
||||
|
||||
while (true) {
|
||||
cost_prev = Cost(y_hat, outputSet);
|
||||
std::vector<std::vector<real_t>> error = alg.subtraction(y_hat, outputSet);
|
||||
cost_prev = cost(_y_hat, _output_set);
|
||||
|
||||
Ref<MLPPMatrix> error = alg.subtractionm(_y_hat, _output_set);
|
||||
|
||||
//Calculating the weight gradients
|
||||
std::vector<std::vector<real_t>> w_gradient = alg.matmult(alg.transpose(inputSet), error);
|
||||
Ref<MLPPMatrix> w_gradient = alg.matmultm(alg.transposem(_input_set), error);
|
||||
|
||||
//Weight updation
|
||||
weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate, w_gradient));
|
||||
weights = regularization.regWeights(weights, lambda, alpha, reg);
|
||||
_weights = alg.subtractionm(_weights, alg.scalar_multiplym(learning_rate, w_gradient));
|
||||
_weights = regularization.reg_weightsm(_weights, _lambda, _alpha, _reg);
|
||||
|
||||
// Calculating the bias gradients
|
||||
//real_t b_gradient = alg.sum_elements(error);
|
||||
|
||||
// Bias Updation
|
||||
bias = alg.subtractMatrixRows(bias, alg.scalarMultiply(learning_rate, error));
|
||||
_bias = alg.subtract_matrix_rows(_bias, alg.scalar_multiplym(learning_rate, error));
|
||||
|
||||
forwardPass();
|
||||
forward_pass();
|
||||
|
||||
// UI PORTION
|
||||
if (UI) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputSet));
|
||||
MLPPUtilities::UI(weights, bias);
|
||||
if (ui) {
|
||||
MLPPUtilities::cost_info(epoch, cost_prev, cost(_y_hat, _output_set));
|
||||
MLPPUtilities::print_ui_mb(_weights, _bias);
|
||||
}
|
||||
|
||||
epoch++;
|
||||
|
||||
if (epoch > max_epoch) {
|
||||
@ -68,125 +115,286 @@ void MLPPSoftmaxReg::gradientDescent(real_t learning_rate, int max_epoch, bool U
|
||||
}
|
||||
}
|
||||
|
||||
void MLPPSoftmaxReg::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
||||
void MLPPSoftmaxReg::sgd(real_t learning_rate, int max_epoch, bool ui) {
|
||||
ERR_FAIL_COND(!_initialized);
|
||||
|
||||
MLPPLinAlg alg;
|
||||
MLPPReg regularization;
|
||||
|
||||
real_t cost_prev = 0;
|
||||
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));
|
||||
real_t outputIndex = distribution(generator);
|
||||
std::random_device rd;
|
||||
std::default_random_engine generator(rd());
|
||||
std::uniform_int_distribution<int> distribution(0, int(_n - 1));
|
||||
|
||||
std::vector<real_t> y_hat = Evaluate(inputSet[outputIndex]);
|
||||
cost_prev = Cost({ y_hat }, { outputSet[outputIndex] });
|
||||
Ref<MLPPVector> input_set_row_tmp;
|
||||
input_set_row_tmp.instance();
|
||||
input_set_row_tmp->resize(_input_set->size().x);
|
||||
|
||||
Ref<MLPPMatrix> y_hat_matrix_tmp;
|
||||
y_hat_matrix_tmp.instance();
|
||||
//y_hat_matrix_tmp->resize(Size2i(_input_set->size().y, 1));
|
||||
|
||||
Ref<MLPPVector> output_set_row_tmp;
|
||||
output_set_row_tmp.instance();
|
||||
output_set_row_tmp->resize(_output_set->size().x);
|
||||
|
||||
Ref<MLPPMatrix> output_set_row_matrix_tmp;
|
||||
output_set_row_matrix_tmp.instance();
|
||||
output_set_row_matrix_tmp->resize(Size2i(_output_set->size().x, 1));
|
||||
|
||||
while (true) {
|
||||
real_t output_index = distribution(generator);
|
||||
|
||||
_input_set->get_row_into_mlpp_vector(output_index, input_set_row_tmp);
|
||||
|
||||
Ref<MLPPVector> y_hat = evaluatev(input_set_row_tmp);
|
||||
y_hat_matrix_tmp->resize(Size2i(y_hat->size(), 1));
|
||||
y_hat_matrix_tmp->set_row_mlpp_vector(0, y_hat);
|
||||
|
||||
_output_set->get_row_into_mlpp_vector(output_index, output_set_row_tmp);
|
||||
output_set_row_matrix_tmp->set_row_mlpp_vector(0, output_set_row_tmp);
|
||||
|
||||
cost_prev = cost(y_hat_matrix_tmp, output_set_row_matrix_tmp);
|
||||
|
||||
// Calculating the weight gradients
|
||||
std::vector<std::vector<real_t>> w_gradient = alg.outerProduct(inputSet[outputIndex], alg.subtraction(y_hat, outputSet[outputIndex]));
|
||||
Ref<MLPPMatrix> w_gradient = alg.outer_product(input_set_row_tmp, alg.subtractionnv(y_hat, output_set_row_tmp));
|
||||
|
||||
// Weight Updation
|
||||
weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate, w_gradient));
|
||||
weights = regularization.regWeights(weights, lambda, alpha, reg);
|
||||
_weights = alg.subtractionm(_weights, alg.scalar_multiplym(learning_rate, w_gradient));
|
||||
_weights = regularization.reg_weightsm(_weights, _lambda, _alpha, _reg);
|
||||
|
||||
// Calculating the bias gradients
|
||||
std::vector<real_t> b_gradient = alg.subtraction(y_hat, outputSet[outputIndex]);
|
||||
Ref<MLPPVector> b_gradient = alg.subtractionnv(y_hat, output_set_row_tmp);
|
||||
|
||||
// Bias updation
|
||||
bias = alg.subtraction(bias, alg.scalarMultiply(learning_rate, b_gradient));
|
||||
_bias = alg.subtractionnv(_bias, alg.scalar_multiplynv(learning_rate, b_gradient));
|
||||
|
||||
y_hat = Evaluate({ inputSet[outputIndex] });
|
||||
y_hat = evaluatev(output_set_row_tmp);
|
||||
|
||||
if (UI) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost({ y_hat }, { outputSet[outputIndex] }));
|
||||
MLPPUtilities::UI(weights, bias);
|
||||
if (ui) {
|
||||
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat_matrix_tmp, output_set_row_matrix_tmp));
|
||||
MLPPUtilities::print_ui_mb(_weights, _bias);
|
||||
}
|
||||
|
||||
epoch++;
|
||||
|
||||
if (epoch > max_epoch) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
forwardPass();
|
||||
|
||||
forward_pass();
|
||||
}
|
||||
|
||||
void MLPPSoftmaxReg::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) {
|
||||
void MLPPSoftmaxReg::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui) {
|
||||
ERR_FAIL_COND(!_initialized);
|
||||
|
||||
MLPPLinAlg alg;
|
||||
MLPPReg regularization;
|
||||
real_t cost_prev = 0;
|
||||
int epoch = 1;
|
||||
|
||||
// Creating the mini-batches
|
||||
int n_mini_batch = n / mini_batch_size;
|
||||
auto batches = MLPPUtilities::createMiniBatches(inputSet, outputSet, n_mini_batch);
|
||||
auto inputMiniBatches = std::get<0>(batches);
|
||||
auto outputMiniBatches = std::get<1>(batches);
|
||||
int n_mini_batch = _n / mini_batch_size;
|
||||
MLPPUtilities::CreateMiniBatchMMBatch batches = MLPPUtilities::create_mini_batchesmm(_input_set, _output_set, n_mini_batch);
|
||||
|
||||
while (true) {
|
||||
for (int i = 0; i < n_mini_batch; i++) {
|
||||
std::vector<std::vector<real_t>> y_hat = Evaluate(inputMiniBatches[i]);
|
||||
cost_prev = Cost(y_hat, outputMiniBatches[i]);
|
||||
Ref<MLPPMatrix> current_inputs = batches.input_sets[i];
|
||||
Ref<MLPPMatrix> current_outputs = batches.output_sets[i];
|
||||
|
||||
std::vector<std::vector<real_t>> error = alg.subtraction(y_hat, outputMiniBatches[i]);
|
||||
Ref<MLPPMatrix> y_hat = evaluatem(current_inputs);
|
||||
cost_prev = cost(y_hat, current_outputs);
|
||||
|
||||
Ref<MLPPMatrix> error = alg.subtractionm(y_hat, current_outputs);
|
||||
|
||||
// Calculating the weight gradients
|
||||
std::vector<std::vector<real_t>> w_gradient = alg.matmult(alg.transpose(inputMiniBatches[i]), error);
|
||||
Ref<MLPPMatrix> w_gradient = alg.matmultm(alg.transposem(current_inputs), error);
|
||||
|
||||
//Weight updation
|
||||
weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate, w_gradient));
|
||||
weights = regularization.regWeights(weights, lambda, alpha, reg);
|
||||
_weights = alg.subtractionm(_weights, alg.scalar_multiplym(learning_rate, w_gradient));
|
||||
_weights = regularization.reg_weightsm(_weights, _lambda, _alpha, _reg);
|
||||
|
||||
// Calculating the bias gradients
|
||||
bias = alg.subtractMatrixRows(bias, alg.scalarMultiply(learning_rate, error));
|
||||
y_hat = Evaluate(inputMiniBatches[i]);
|
||||
_bias = alg.subtract_matrix_rows(_bias, alg.scalar_multiplym(learning_rate, error));
|
||||
y_hat = evaluatem(current_inputs);
|
||||
|
||||
if (UI) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputMiniBatches[i]));
|
||||
MLPPUtilities::UI(weights, bias);
|
||||
if (ui) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, current_outputs));
|
||||
MLPPUtilities::print_ui_mb(_weights, _bias);
|
||||
}
|
||||
}
|
||||
|
||||
epoch++;
|
||||
|
||||
if (epoch > max_epoch) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
forwardPass();
|
||||
|
||||
forward_pass();
|
||||
}
|
||||
|
||||
real_t MLPPSoftmaxReg::score() {
|
||||
ERR_FAIL_COND_V(!_initialized, 0);
|
||||
|
||||
MLPPUtilities util;
|
||||
return util.performance(y_hat, outputSet);
|
||||
|
||||
return util.performance_mat(_y_hat, _output_set);
|
||||
}
|
||||
|
||||
void MLPPSoftmaxReg::save(std::string fileName) {
|
||||
void MLPPSoftmaxReg::save(const String &file_name) {
|
||||
ERR_FAIL_COND(!_initialized);
|
||||
|
||||
MLPPUtilities util;
|
||||
util.saveParameters(fileName, weights, bias);
|
||||
|
||||
//util.saveParameters(file_name, _weights, _bias);
|
||||
}
|
||||
|
||||
real_t MLPPSoftmaxReg::Cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y) {
|
||||
bool MLPPSoftmaxReg::is_initialized() {
|
||||
return _initialized;
|
||||
}
|
||||
void MLPPSoftmaxReg::initialize() {
|
||||
if (_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
|
||||
|
||||
_n = _input_set->size().y;
|
||||
_k = _input_set->size().x;
|
||||
_n_class = _output_set->size().x;
|
||||
|
||||
_y_hat.instance();
|
||||
_y_hat->resize(Size2i(_n, 0));
|
||||
|
||||
MLPPUtilities util;
|
||||
|
||||
_weights.instance();
|
||||
_weights->resize(Size2i(_n_class, _k));
|
||||
|
||||
_bias.instance();
|
||||
_bias->resize(_n_class);
|
||||
|
||||
util.weight_initializationm(_weights);
|
||||
util.bias_initializationv(_bias);
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
MLPPSoftmaxReg::MLPPSoftmaxReg(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPMatrix> &p_output_set, MLPPReg::RegularizationType p_reg, real_t p_lambda, real_t p_alpha) {
|
||||
_input_set = p_input_set;
|
||||
_output_set = p_output_set;
|
||||
|
||||
_n = _input_set->size().y;
|
||||
_k = _input_set->size().x;
|
||||
_n_class = _output_set->size().x;
|
||||
|
||||
_reg = p_reg;
|
||||
_lambda = p_lambda;
|
||||
_alpha = p_alpha;
|
||||
|
||||
if (!_y_hat.is_valid()) {
|
||||
_y_hat.instance();
|
||||
}
|
||||
_y_hat->resize(Size2i(_n, 0));
|
||||
|
||||
MLPPUtilities util;
|
||||
|
||||
if (!_weights.is_valid()) {
|
||||
_weights.instance();
|
||||
}
|
||||
_weights->resize(Size2i(_n_class, _k));
|
||||
|
||||
if (!_bias.is_valid()) {
|
||||
_bias.instance();
|
||||
}
|
||||
_bias->resize(_n_class);
|
||||
|
||||
util.weight_initializationm(_weights);
|
||||
util.bias_initializationv(_bias);
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
MLPPSoftmaxReg::MLPPSoftmaxReg() {
|
||||
_n = 0;
|
||||
_k = 0;
|
||||
_n_class = 0;
|
||||
|
||||
// Regularization Params
|
||||
_reg = MLPPReg::REGULARIZATION_TYPE_NONE;
|
||||
_lambda = 0.5;
|
||||
_alpha = 0.5; /* This is the controlling param for Elastic Net*/
|
||||
|
||||
_initialized = false;
|
||||
}
|
||||
MLPPSoftmaxReg::~MLPPSoftmaxReg() {
|
||||
}
|
||||
|
||||
real_t MLPPSoftmaxReg::cost(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
|
||||
MLPPReg regularization;
|
||||
class MLPPCost cost;
|
||||
return cost.CrossEntropy(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg);
|
||||
|
||||
return cost.cross_entropym(y_hat, y) + regularization.reg_termm(_weights, _lambda, _alpha, _reg);
|
||||
}
|
||||
|
||||
std::vector<real_t> MLPPSoftmaxReg::Evaluate(std::vector<real_t> x) {
|
||||
MLPPLinAlg alg;
|
||||
MLPPActivation avn;
|
||||
return avn.softmax(alg.addition(bias, alg.mat_vec_mult(alg.transpose(weights), x)));
|
||||
}
|
||||
|
||||
std::vector<std::vector<real_t>> MLPPSoftmaxReg::Evaluate(std::vector<std::vector<real_t>> X) {
|
||||
Ref<MLPPVector> MLPPSoftmaxReg::evaluatev(const Ref<MLPPVector> &x) {
|
||||
MLPPLinAlg alg;
|
||||
MLPPActivation avn;
|
||||
|
||||
return avn.softmax(alg.mat_vec_add(alg.matmult(X, weights), bias));
|
||||
return avn.softmax_normv(alg.additionnv(_bias, alg.mat_vec_multv(alg.transposem(_weights), x)));
|
||||
}
|
||||
|
||||
Ref<MLPPMatrix> MLPPSoftmaxReg::evaluatem(const Ref<MLPPMatrix> &X) {
|
||||
MLPPLinAlg alg;
|
||||
MLPPActivation avn;
|
||||
|
||||
return avn.softmax_normm(alg.mat_vec_addv(alg.matmultm(X, _weights), _bias));
|
||||
}
|
||||
|
||||
// softmax ( wTx + b )
|
||||
void MLPPSoftmaxReg::forwardPass() {
|
||||
void MLPPSoftmaxReg::forward_pass() {
|
||||
MLPPLinAlg alg;
|
||||
MLPPActivation avn;
|
||||
|
||||
y_hat = avn.softmax(alg.mat_vec_add(alg.matmult(inputSet, weights), bias));
|
||||
_y_hat = avn.softmax_normm(alg.mat_vec_addv(alg.matmultm(_input_set, _weights), _bias));
|
||||
}
|
||||
|
||||
void MLPPSoftmaxReg::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPSoftmaxReg::get_input_set);
|
||||
ClassDB::bind_method(D_METHOD("set_input_set", "val"), &MLPPSoftmaxReg::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"), &MLPPSoftmaxReg::get_output_set);
|
||||
ClassDB::bind_method(D_METHOD("set_output_set", "val"), &MLPPSoftmaxReg::set_output_set);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "output_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_output_set", "get_output_set");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_reg"), &MLPPSoftmaxReg::get_reg);
|
||||
ClassDB::bind_method(D_METHOD("set_reg", "val"), &MLPPSoftmaxReg::set_reg);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "reg"), "set_reg", "get_reg");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_lambda"), &MLPPSoftmaxReg::get_lambda);
|
||||
ClassDB::bind_method(D_METHOD("set_lambda", "val"), &MLPPSoftmaxReg::set_lambda);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "lambda"), "set_lambda", "get_lambda");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_alpha"), &MLPPSoftmaxReg::get_alpha);
|
||||
ClassDB::bind_method(D_METHOD("set_alpha", "val"), &MLPPSoftmaxReg::set_alpha);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "alpha"), "set_alpha", "get_alpha");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("model_test", "x"), &MLPPSoftmaxReg::model_test);
|
||||
ClassDB::bind_method(D_METHOD("model_set_test", "X"), &MLPPSoftmaxReg::model_set_test);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("gradient_descent", "learning_rate", "max_epoch", "ui"), &MLPPSoftmaxReg::gradient_descent, false);
|
||||
ClassDB::bind_method(D_METHOD("sgd", "learning_rate", "max_epoch", "ui"), &MLPPSoftmaxReg::sgd, false);
|
||||
ClassDB::bind_method(D_METHOD("mbgd", "learning_rate", "max_epoch", "mini_batch_size", "ui"), &MLPPSoftmaxReg::mbgd, false);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("score"), &MLPPSoftmaxReg::score);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("save", "file_name"), &MLPPSoftmaxReg::save);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("is_initialized"), &MLPPSoftmaxReg::is_initialized);
|
||||
ClassDB::bind_method(D_METHOD("initialize"), &MLPPSoftmaxReg::initialize);
|
||||
}
|
||||
|
@ -10,41 +10,77 @@
|
||||
|
||||
#include "core/math/math_defs.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "core/object/reference.h"
|
||||
|
||||
#include "../lin_alg/mlpp_matrix.h"
|
||||
#include "../lin_alg/mlpp_vector.h"
|
||||
|
||||
#include "../regularization/reg.h"
|
||||
|
||||
class MLPPSoftmaxReg : public Reference {
|
||||
GDCLASS(MLPPSoftmaxReg, Reference);
|
||||
|
||||
class MLPPSoftmaxReg {
|
||||
public:
|
||||
MLPPSoftmaxReg(std::vector<std::vector<real_t>> inputSet, std::vector<std::vector<real_t>> outputSet, std::string reg = "None", real_t lambda = 0.5, real_t alpha = 0.5);
|
||||
std::vector<real_t> modelTest(std::vector<real_t> x);
|
||||
std::vector<std::vector<real_t>> modelSetTest(std::vector<std::vector<real_t>> X);
|
||||
void gradientDescent(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);
|
||||
Ref<MLPPMatrix> get_input_set();
|
||||
void set_input_set(const Ref<MLPPMatrix> &val);
|
||||
|
||||
Ref<MLPPMatrix> get_output_set();
|
||||
void set_output_set(const Ref<MLPPMatrix> &val);
|
||||
|
||||
MLPPReg::RegularizationType get_reg();
|
||||
void set_reg(const MLPPReg::RegularizationType val);
|
||||
|
||||
real_t get_lambda();
|
||||
void set_lambda(const real_t val);
|
||||
|
||||
real_t get_alpha();
|
||||
void set_alpha(const real_t val);
|
||||
|
||||
Ref<MLPPVector> model_test(const Ref<MLPPVector> &x);
|
||||
Ref<MLPPMatrix> model_set_test(const Ref<MLPPMatrix> &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();
|
||||
void save(std::string fileName);
|
||||
|
||||
private:
|
||||
real_t Cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y);
|
||||
void save(const String &file_name);
|
||||
|
||||
std::vector<std::vector<real_t>> Evaluate(std::vector<std::vector<real_t>> X);
|
||||
std::vector<real_t> Evaluate(std::vector<real_t> x);
|
||||
void forwardPass();
|
||||
bool is_initialized();
|
||||
void initialize();
|
||||
|
||||
std::vector<std::vector<real_t>> inputSet;
|
||||
std::vector<std::vector<real_t>> outputSet;
|
||||
std::vector<std::vector<real_t>> y_hat;
|
||||
std::vector<std::vector<real_t>> weights;
|
||||
std::vector<real_t> bias;
|
||||
MLPPSoftmaxReg(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPMatrix> &p_output_set, MLPPReg::RegularizationType p_reg = MLPPReg::REGULARIZATION_TYPE_NONE, real_t p_lambda = 0.5, real_t p_alpha = 0.5);
|
||||
|
||||
int n;
|
||||
int k;
|
||||
int n_class;
|
||||
MLPPSoftmaxReg();
|
||||
~MLPPSoftmaxReg();
|
||||
|
||||
protected:
|
||||
real_t cost(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
|
||||
|
||||
Ref<MLPPVector> evaluatev(const Ref<MLPPVector> &x);
|
||||
Ref<MLPPMatrix> evaluatem(const Ref<MLPPMatrix> &X);
|
||||
|
||||
void forward_pass();
|
||||
|
||||
static void _bind_methods();
|
||||
|
||||
Ref<MLPPMatrix> _input_set;
|
||||
Ref<MLPPMatrix> _output_set;
|
||||
Ref<MLPPMatrix> _y_hat;
|
||||
Ref<MLPPMatrix> _weights;
|
||||
Ref<MLPPVector> _bias;
|
||||
|
||||
int _n;
|
||||
int _k;
|
||||
int _n_class;
|
||||
|
||||
// Regularization Params
|
||||
std::string reg;
|
||||
real_t lambda;
|
||||
real_t alpha; /* This is the controlling param for Elastic Net*/
|
||||
MLPPReg::RegularizationType _reg;
|
||||
real_t _lambda;
|
||||
real_t _alpha; /* This is the controlling param for Elastic Net*/
|
||||
|
||||
bool _initialized;
|
||||
};
|
||||
|
||||
#endif /* SoftmaxReg_hpp */
|
||||
|
@ -96,7 +96,8 @@ void MLPPSoftmaxRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
||||
// Bias updation
|
||||
bias = alg.subtraction(bias, alg.scalarMultiply(learning_rate, b_gradient));
|
||||
|
||||
y_hat = Evaluate({ inputSet[outputIndex] });
|
||||
//y_hat = Evaluate({ inputSet[outputIndex] });
|
||||
y_hat = Evaluate(inputSet[outputIndex]);
|
||||
|
||||
if (UI) {
|
||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost({ y_hat }, { outputSet[outputIndex] }));
|
||||
|
@ -44,6 +44,7 @@ SOFTWARE.
|
||||
#include "mlpp/wgan/wgan.h"
|
||||
#include "mlpp/probit_reg/probit_reg.h"
|
||||
#include "mlpp/svc/svc.h"
|
||||
#include "mlpp/softmax_reg/softmax_reg.h"
|
||||
|
||||
#include "mlpp/mlp/mlp.h"
|
||||
|
||||
@ -73,6 +74,7 @@ void register_pmlpp_types(ModuleRegistrationLevel p_level) {
|
||||
ClassDB::register_class<MLPPOutlierFinder>();
|
||||
ClassDB::register_class<MLPPProbitReg>();
|
||||
ClassDB::register_class<MLPPSVC>();
|
||||
ClassDB::register_class<MLPPSoftmaxReg>();
|
||||
|
||||
ClassDB::register_class<MLPPDataESimple>();
|
||||
ClassDB::register_class<MLPPDataSimple>();
|
||||
|
@ -398,12 +398,19 @@ void MLPPTests::test_softmax_regression(bool ui) {
|
||||
MLPPLinAlg alg;
|
||||
MLPPData data;
|
||||
|
||||
// SOFTMAX REGRESSION
|
||||
Ref<MLPPDataComplex> dt = data.load_iris(_iris_data_path);
|
||||
|
||||
// SOFTMAX REGRESSION
|
||||
|
||||
MLPPSoftmaxRegOld model_old(dt->get_input()->to_std_vector(), dt->get_output()->to_std_vector());
|
||||
model_old.SGD(0.1, 10000, ui);
|
||||
alg.printMatrix(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;
|
||||
|
||||
MLPPSoftmaxReg model(dt->get_input(), dt->get_output());
|
||||
model.sgd(0.1, 10000, ui);
|
||||
PLOG_MSG(model.model_set_test(dt->get_input())->to_string());
|
||||
PLOG_MSG("ACCURACY: " + String::num(100 * model.score()) + "%");
|
||||
}
|
||||
void MLPPTests::test_support_vector_classification(bool ui) {
|
||||
//MLPPStat stat;
|
||||
|
Loading…
Reference in New Issue
Block a user