mirror of
https://github.com/Relintai/pmlpp.git
synced 2025-01-18 15:07:16 +01:00
Initial cleanup pass on MLPPCLogLogReg.
This commit is contained in:
parent
cdb0e47d16
commit
8d9651b65a
@ -14,31 +14,26 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
MLPPCLogLogReg::MLPPCLogLogReg(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, std::string reg, real_t lambda, real_t alpha) :
|
std::vector<real_t> MLPPCLogLogReg::model_set_test(std::vector<std::vector<real_t>> X) {
|
||||||
inputSet(inputSet), outputSet(outputSet), n(inputSet.size()), k(inputSet[0].size()), reg(reg), lambda(lambda), alpha(alpha) {
|
return evaluatem(X);
|
||||||
y_hat.resize(n);
|
|
||||||
weights = MLPPUtilities::weightInitialization(k);
|
|
||||||
bias = MLPPUtilities::biasInitialization();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<real_t> MLPPCLogLogReg::modelSetTest(std::vector<std::vector<real_t>> X) {
|
real_t MLPPCLogLogReg::model_test(std::vector<real_t> x) {
|
||||||
return Evaluate(X);
|
return evaluatev(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPCLogLogReg::modelTest(std::vector<real_t> x) {
|
void MLPPCLogLogReg::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
|
||||||
return Evaluate(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MLPPCLogLogReg::gradientDescent(real_t learning_rate, int max_epoch, bool UI) {
|
|
||||||
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);
|
cost_prev = cost(y_hat, outputSet);
|
||||||
|
|
||||||
std::vector<real_t> error = alg.subtraction(y_hat, outputSet);
|
std::vector<real_t> error = alg.subtraction(y_hat, outputSet);
|
||||||
|
|
||||||
@ -49,12 +44,13 @@ void MLPPCLogLogReg::gradientDescent(real_t learning_rate, int max_epoch, bool U
|
|||||||
// Calculating the bias gradients
|
// Calculating the bias gradients
|
||||||
bias -= learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / n;
|
bias -= learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / n;
|
||||||
|
|
||||||
forwardPass();
|
forward_pass();
|
||||||
|
|
||||||
if (UI) {
|
if (ui) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputSet));
|
MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, outputSet));
|
||||||
MLPPUtilities::UI(weights, bias);
|
MLPPUtilities::UI(weights, bias);
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
|
|
||||||
if (epoch > max_epoch) {
|
if (epoch > max_epoch) {
|
||||||
@ -63,16 +59,18 @@ void MLPPCLogLogReg::gradientDescent(real_t learning_rate, int max_epoch, bool U
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPCLogLogReg::MLE(real_t learning_rate, int max_epoch, bool UI) {
|
void MLPPCLogLogReg::mle(real_t learning_rate, int max_epoch, bool ui) {
|
||||||
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);
|
cost_prev = cost(y_hat, outputSet);
|
||||||
|
|
||||||
std::vector<real_t> error = alg.subtraction(y_hat, outputSet);
|
std::vector<real_t> error = alg.subtraction(y_hat, outputSet);
|
||||||
|
|
||||||
@ -81,12 +79,14 @@ void MLPPCLogLogReg::MLE(real_t learning_rate, int max_epoch, bool UI) {
|
|||||||
|
|
||||||
// Calculating the bias gradients
|
// Calculating the bias gradients
|
||||||
bias += learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / n;
|
bias += learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / n;
|
||||||
forwardPass();
|
|
||||||
|
|
||||||
if (UI) {
|
forward_pass();
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputSet));
|
|
||||||
|
if (ui) {
|
||||||
|
MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, outputSet));
|
||||||
MLPPUtilities::UI(weights, bias);
|
MLPPUtilities::UI(weights, bias);
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
|
|
||||||
if (epoch > max_epoch) {
|
if (epoch > max_epoch) {
|
||||||
@ -95,12 +95,14 @@ void MLPPCLogLogReg::MLE(real_t learning_rate, int max_epoch, bool UI) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPCLogLogReg::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
void MLPPCLogLogReg::sgd(real_t learning_rate, int max_epoch, bool p_) {
|
||||||
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) {
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
@ -108,9 +110,9 @@ void MLPPCLogLogReg::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
|||||||
std::uniform_int_distribution<int> distribution(0, int(n - 1));
|
std::uniform_int_distribution<int> distribution(0, int(n - 1));
|
||||||
int outputIndex = distribution(generator);
|
int outputIndex = distribution(generator);
|
||||||
|
|
||||||
real_t y_hat = Evaluate(inputSet[outputIndex]);
|
real_t y_hat = evaluatev(inputSet[outputIndex]);
|
||||||
real_t z = propagate(inputSet[outputIndex]);
|
real_t z = propagatev(inputSet[outputIndex]);
|
||||||
cost_prev = Cost({ y_hat }, { outputSet[outputIndex] });
|
cost_prev = cost({ y_hat }, { outputSet[outputIndex] });
|
||||||
|
|
||||||
real_t error = y_hat - outputSet[outputIndex];
|
real_t error = y_hat - outputSet[outputIndex];
|
||||||
|
|
||||||
@ -121,22 +123,24 @@ void MLPPCLogLogReg::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
|||||||
// Bias updation
|
// Bias updation
|
||||||
bias -= learning_rate * error * exp(z - exp(z));
|
bias -= learning_rate * error * exp(z - exp(z));
|
||||||
|
|
||||||
y_hat = Evaluate({ inputSet[outputIndex] });
|
y_hat = evaluatev(inputSet[outputIndex]);
|
||||||
|
|
||||||
if (UI) {
|
if (p_) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost({ y_hat }, { outputSet[outputIndex] }));
|
MLPPUtilities::CostInfo(epoch, cost_prev, cost({ y_hat }, { outputSet[outputIndex] }));
|
||||||
MLPPUtilities::UI(weights, bias);
|
MLPPUtilities::UI(weights, bias);
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
|
|
||||||
if (epoch > max_epoch) {
|
if (epoch > max_epoch) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
forwardPass();
|
|
||||||
|
forward_pass();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPCLogLogReg::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) {
|
void MLPPCLogLogReg::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool p_) {
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPReg regularization;
|
MLPPReg regularization;
|
||||||
@ -151,9 +155,9 @@ void MLPPCLogLogReg::MBGD(real_t learning_rate, int max_epoch, int mini_batch_si
|
|||||||
|
|
||||||
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]);
|
std::vector<real_t> y_hat = evaluatem(inputMiniBatches[i]);
|
||||||
std::vector<real_t> z = propagate(inputMiniBatches[i]);
|
std::vector<real_t> z = propagatem(inputMiniBatches[i]);
|
||||||
cost_prev = Cost(y_hat, outputMiniBatches[i]);
|
cost_prev = cost(y_hat, outputMiniBatches[i]);
|
||||||
|
|
||||||
std::vector<real_t> error = alg.subtraction(y_hat, outputMiniBatches[i]);
|
std::vector<real_t> error = alg.subtraction(y_hat, outputMiniBatches[i]);
|
||||||
|
|
||||||
@ -164,21 +168,24 @@ void MLPPCLogLogReg::MBGD(real_t learning_rate, int max_epoch, int mini_batch_si
|
|||||||
// Calculating the bias gradients
|
// Calculating the bias gradients
|
||||||
bias -= learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / n;
|
bias -= learning_rate * alg.sum_elements(alg.hadamard_product(error, avn.cloglog(z, 1))) / n;
|
||||||
|
|
||||||
forwardPass();
|
forward_pass();
|
||||||
|
|
||||||
y_hat = Evaluate(inputMiniBatches[i]);
|
y_hat = evaluatem(inputMiniBatches[i]);
|
||||||
|
|
||||||
if (UI) {
|
if (p_) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputMiniBatches[i]));
|
MLPPUtilities::CostInfo(epoch, cost_prev, cost(y_hat, outputMiniBatches[i]));
|
||||||
MLPPUtilities::UI(weights, bias);
|
MLPPUtilities::UI(weights, bias);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
|
|
||||||
if (epoch > max_epoch) {
|
if (epoch > max_epoch) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
forwardPass();
|
|
||||||
|
forward_pass();
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPCLogLogReg::score() {
|
real_t MLPPCLogLogReg::score() {
|
||||||
@ -186,38 +193,58 @@ real_t MLPPCLogLogReg::score() {
|
|||||||
return util.performance(y_hat, outputSet);
|
return util.performance(y_hat, outputSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPCLogLogReg::Cost(std::vector<real_t> y_hat, std::vector<real_t> y) {
|
MLPPCLogLogReg::MLPPCLogLogReg(std::vector<std::vector<real_t>> pinputSet, std::vector<real_t> poutputSet, std::string p_reg, real_t p_lambda, real_t p_alpha) {
|
||||||
|
inputSet = pinputSet;
|
||||||
|
outputSet = poutputSet;
|
||||||
|
n = inputSet.size();
|
||||||
|
k = inputSet[0].size();
|
||||||
|
reg = p_reg;
|
||||||
|
lambda = p_lambda;
|
||||||
|
alpha = p_alpha;
|
||||||
|
|
||||||
|
y_hat.resize(n);
|
||||||
|
|
||||||
|
weights = MLPPUtilities::weightInitialization(k);
|
||||||
|
bias = MLPPUtilities::biasInitialization();
|
||||||
|
}
|
||||||
|
|
||||||
|
MLPPCLogLogReg::MLPPCLogLogReg() {
|
||||||
|
}
|
||||||
|
MLPPCLogLogReg::~MLPPCLogLogReg() {
|
||||||
|
}
|
||||||
|
|
||||||
|
real_t MLPPCLogLogReg::cost(std::vector<real_t> y_hat, std::vector<real_t> y) {
|
||||||
MLPPReg regularization;
|
MLPPReg regularization;
|
||||||
class MLPPCost cost;
|
class MLPPCost cost;
|
||||||
return cost.MSE(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg);
|
return cost.MSE(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<real_t> MLPPCLogLogReg::Evaluate(std::vector<std::vector<real_t>> X) {
|
real_t MLPPCLogLogReg::evaluatev(std::vector<real_t> x) {
|
||||||
MLPPLinAlg alg;
|
|
||||||
MLPPActivation avn;
|
|
||||||
return avn.cloglog(alg.scalarAdd(bias, alg.mat_vec_mult(X, weights)));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<real_t> MLPPCLogLogReg::propagate(std::vector<std::vector<real_t>> X) {
|
|
||||||
MLPPLinAlg alg;
|
|
||||||
return alg.scalarAdd(bias, alg.mat_vec_mult(X, weights));
|
|
||||||
}
|
|
||||||
|
|
||||||
real_t MLPPCLogLogReg::Evaluate(std::vector<real_t> x) {
|
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
return avn.cloglog(alg.dot(weights, x) + bias);
|
return avn.cloglog(alg.dot(weights, x) + bias);
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPCLogLogReg::propagate(std::vector<real_t> x) {
|
real_t MLPPCLogLogReg::propagatev(std::vector<real_t> x) {
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
return alg.dot(weights, x) + bias;
|
return alg.dot(weights, x) + bias;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<real_t> MLPPCLogLogReg::evaluatem(std::vector<std::vector<real_t>> X) {
|
||||||
|
MLPPLinAlg alg;
|
||||||
|
MLPPActivation avn;
|
||||||
|
return avn.cloglog(alg.scalarAdd(bias, alg.mat_vec_mult(X, weights)));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<real_t> MLPPCLogLogReg::propagatem(std::vector<std::vector<real_t>> X) {
|
||||||
|
MLPPLinAlg alg;
|
||||||
|
return alg.scalarAdd(bias, alg.mat_vec_mult(X, weights));
|
||||||
|
}
|
||||||
|
|
||||||
// cloglog ( wTx + b )
|
// cloglog ( wTx + b )
|
||||||
void MLPPCLogLogReg::forwardPass() {
|
void MLPPCLogLogReg::forward_pass() {
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
|
|
||||||
z = propagate(inputSet);
|
z = propagatem(inputSet);
|
||||||
y_hat = avn.cloglog(z);
|
y_hat = avn.cloglog(z);
|
||||||
}
|
}
|
||||||
|
@ -15,25 +15,34 @@
|
|||||||
|
|
||||||
class MLPPCLogLogReg {
|
class MLPPCLogLogReg {
|
||||||
public:
|
public:
|
||||||
MLPPCLogLogReg(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, std::string reg = "None", real_t lambda = 0.5, real_t alpha = 0.5);
|
std::vector<real_t> model_set_test(std::vector<std::vector<real_t>> X);
|
||||||
std::vector<real_t> modelSetTest(std::vector<std::vector<real_t>> X);
|
real_t model_test(std::vector<real_t> x);
|
||||||
real_t modelTest(std::vector<real_t> x);
|
|
||||||
void gradientDescent(real_t learning_rate, int max_epoch, bool UI = false);
|
void gradient_descent(real_t learning_rate, int max_epoch, bool ui = false);
|
||||||
void MLE(real_t learning_rate, int max_epoch, bool UI = false);
|
void mle(real_t learning_rate, int max_epoch, bool ui = false);
|
||||||
void SGD(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);
|
void mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui = false);
|
||||||
|
|
||||||
real_t score();
|
real_t score();
|
||||||
|
|
||||||
private:
|
MLPPCLogLogReg(std::vector<std::vector<real_t>> pinputSet, std::vector<real_t> poutputSet, std::string p_reg = "None", real_t p_lambda = 0.5, real_t p_alpha = 0.5);
|
||||||
void weightInitialization(int k);
|
|
||||||
void biasInitialization();
|
|
||||||
real_t Cost(std::vector<real_t> y_hat, std::vector<real_t> y);
|
|
||||||
|
|
||||||
std::vector<real_t> Evaluate(std::vector<std::vector<real_t>> X);
|
MLPPCLogLogReg();
|
||||||
std::vector<real_t> propagate(std::vector<std::vector<real_t>> X);
|
~MLPPCLogLogReg();
|
||||||
real_t Evaluate(std::vector<real_t> x);
|
|
||||||
real_t propagate(std::vector<real_t> x);
|
private:
|
||||||
void forwardPass();
|
void weight_initialization(int k);
|
||||||
|
void bias_initialization();
|
||||||
|
|
||||||
|
real_t cost(std::vector<real_t> y_hat, std::vector<real_t> y);
|
||||||
|
|
||||||
|
real_t evaluatev(std::vector<real_t> x);
|
||||||
|
real_t propagatev(std::vector<real_t> x);
|
||||||
|
|
||||||
|
std::vector<real_t> evaluatem(std::vector<std::vector<real_t>> X);
|
||||||
|
std::vector<real_t> propagatem(std::vector<std::vector<real_t>> X);
|
||||||
|
|
||||||
|
void forward_pass();
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> inputSet;
|
std::vector<std::vector<real_t>> inputSet;
|
||||||
std::vector<real_t> outputSet;
|
std::vector<real_t> outputSet;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
#ifndef MLPP_C_LOG_LOG_REG_H
|
#ifndef MLPP_C_LOG_LOG_REG_OLD_H
|
||||||
#define MLPP_C_LOG_LOG_REG_H
|
#define MLPP_C_LOG_LOG_REG_OLD_H
|
||||||
|
|
||||||
//
|
//
|
||||||
// CLogLogReg.hpp
|
// CLogLogReg.hpp
|
||||||
|
@ -423,9 +423,15 @@ void MLPPTests::test_c_log_log_regression(bool ui) {
|
|||||||
// CLOGLOG REGRESSION
|
// CLOGLOG REGRESSION
|
||||||
std::vector<std::vector<real_t>> inputSet = { { 1, 2, 3, 4, 5, 6, 7, 8 }, { 0, 0, 0, 0, 1, 1, 1, 1 } };
|
std::vector<std::vector<real_t>> inputSet = { { 1, 2, 3, 4, 5, 6, 7, 8 }, { 0, 0, 0, 0, 1, 1, 1, 1 } };
|
||||||
std::vector<real_t> outputSet = { 0, 0, 0, 0, 1, 1, 1, 1 };
|
std::vector<real_t> outputSet = { 0, 0, 0, 0, 1, 1, 1, 1 };
|
||||||
|
|
||||||
|
MLPPCLogLogRegOld model_old(alg.transpose(inputSet), outputSet);
|
||||||
|
model_old.SGD(0.1, 10000, ui);
|
||||||
|
alg.printVector(model_old.modelSetTest(alg.transpose(inputSet)));
|
||||||
|
std::cout << "ACCURACY: " << 100 * model_old.score() << "%" << std::endl;
|
||||||
|
|
||||||
MLPPCLogLogReg model(alg.transpose(inputSet), outputSet);
|
MLPPCLogLogReg model(alg.transpose(inputSet), outputSet);
|
||||||
model.SGD(0.1, 10000, ui);
|
model.sgd(0.1, 10000, ui);
|
||||||
alg.printVector(model.modelSetTest(alg.transpose(inputSet)));
|
alg.printVector(model.model_set_test(alg.transpose(inputSet)));
|
||||||
std::cout << "ACCURACY: " << 100 * model.score() << "%" << std::endl;
|
std::cout << "ACCURACY: " << 100 * model.score() << "%" << std::endl;
|
||||||
}
|
}
|
||||||
void MLPPTests::test_exp_reg_regression(bool ui) {
|
void MLPPTests::test_exp_reg_regression(bool ui) {
|
||||||
|
Loading…
Reference in New Issue
Block a user