From 4c65bb36d7e23733e3b319b24795f79443649cdf Mon Sep 17 00:00:00 2001 From: novak_99 Date: Fri, 28 May 2021 19:26:10 -0700 Subject: [PATCH] "Vectorized" the implementation of SGD for Log & Lin Reg --- MLPP/LinReg/LinReg.cpp | 20 ++++++-------------- MLPP/LogReg/LogReg.cpp | 20 ++++++-------------- main.cpp | 20 ++++++++++---------- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/MLPP/LinReg/LinReg.cpp b/MLPP/LinReg/LinReg.cpp index 5bd4b03..d59c87e 100644 --- a/MLPP/LinReg/LinReg.cpp +++ b/MLPP/LinReg/LinReg.cpp @@ -65,6 +65,7 @@ namespace MLPP{ } void LinReg::SGD(double learning_rate, int max_epoch, bool UI){ + LinAlg alg; Reg regularization; Utilities util; double cost_prev = 0; @@ -79,24 +80,15 @@ namespace MLPP{ double y_hat = Evaluate(inputSet[outputIndex]); cost_prev = Cost({y_hat}, {outputSet[outputIndex]}); + double error = y_hat - outputSet[outputIndex]; - for(int i = 0; i < k; i++){ - - // Calculating the weight gradients - - double w_gradient = (y_hat - outputSet[outputIndex]) * inputSet[outputIndex][i]; - - - // Weight updation - weights[i] -= learning_rate * w_gradient; - } + // Weight updation + weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate * error, inputSet[outputIndex])); weights = regularization.regWeights(weights, lambda, alpha, reg); - // Calculating the bias gradients - double b_gradient = (y_hat - outputSet[outputIndex]); - // Bias updation - bias -= learning_rate * b_gradient; + bias -= learning_rate * error; + y_hat = Evaluate({inputSet[outputIndex]}); if(UI) { diff --git a/MLPP/LogReg/LogReg.cpp b/MLPP/LogReg/LogReg.cpp index 9c7d8dd..80a67e0 100644 --- a/MLPP/LogReg/LogReg.cpp +++ b/MLPP/LogReg/LogReg.cpp @@ -92,6 +92,7 @@ namespace MLPP{ } void LogReg::SGD(double learning_rate, int max_epoch, bool UI){ + LinAlg alg; Reg regularization; Utilities util; double cost_prev = 0; @@ -106,24 +107,15 @@ namespace MLPP{ double y_hat = Evaluate(inputSet[outputIndex]); cost_prev = Cost({y_hat}, {outputSet[outputIndex]}); + double error = y_hat - outputSet[outputIndex]; - for(int i = 0; i < k; i++){ - - // Calculating the weight gradients - - double w_gradient = (y_hat - outputSet[outputIndex]) * inputSet[outputIndex][i]; - - - // Weight updation - weights[i] -= learning_rate * w_gradient; - } + // Weight updation + weights = alg.subtraction(weights, alg.scalarMultiply(learning_rate * error, inputSet[outputIndex])); weights = regularization.regWeights(weights, lambda, alpha, reg); - // Calculating the bias gradients - double b_gradient = (y_hat - outputSet[outputIndex]); - // Bias updation - bias -= learning_rate * b_gradient; + bias -= learning_rate * error; + y_hat = Evaluate({inputSet[outputIndex]}); if(UI) { diff --git a/main.cpp b/main.cpp index bd1a68a..6198bd6 100644 --- a/main.cpp +++ b/main.cpp @@ -131,26 +131,26 @@ int main() { // UniLinReg model(inputSet, outputSet); // alg.printVector(model.modelSetTest(inputSet)); - // // MULIVARIATE LINEAR REGRESSION + // MULIVARIATE LINEAR REGRESSION // std::vector> inputSet = {{1,2,3,4,5,6,7,8,9,10}, {3,5,9,12,15,18,21,24,27,30}}; // std::vector outputSet = {2,4,6,8,10,12,14,16,18,20}; // LinReg model(alg.transpose(inputSet), outputSet); // Can use Lasso, Ridge, ElasticNet Reg - // model.normalEquation(); + //model.normalEquation(); // model.gradientDescent(0.001, 30000, 1); // model.SGD(0.001, 30000, 1); // model.MBGD(0.001, 10000, 2, 1); // alg.printVector(model.modelSetTest((alg.transpose(inputSet)))); // std::cout << "ACCURACY: " << 100 * model.score() << "%" << std::endl; - // // LOGISTIC REGRESSION - // std::vector> inputSet; - // std::vector outputSet; - // data.setData(30, "/Users/marcmelikyan/Desktop/Data/BreastCancer.csv", inputSet, outputSet); - // LogReg model(inputSet, outputSet); - // //model.SGD(0.1, 50000, 0); + // LOGISTIC REGRESSION + std::vector> inputSet; + std::vector outputSet; + data.setData(30, "/Users/marcmelikyan/Desktop/Data/BreastCancer.csv", inputSet, outputSet); + LogReg model(inputSet, outputSet); + model.SGD(0.001, 100000, 0); // model.MLE(0.1, 10000, 0); - // alg.printVector(model.modelSetTest(inputSet)); - // std::cout << "ACCURACY: " << 100 * model.score() << "%" << std::endl; + alg.printVector(model.modelSetTest(inputSet)); + std::cout << "ACCURACY: " << 100 * model.score() << "%" << std::endl; // // PROBIT REGRESSION // std::vector> inputSet;