From 121bd722a26e0cba84df6cc790f04500a27a21be Mon Sep 17 00:00:00 2001 From: ayesdie Date: Wed, 20 Mar 2019 18:36:28 +0530 Subject: [PATCH] updated groundTruth issue for SoftmaxRegressionFunction and fixed build --- .../methods/linear_svm/linear_svm_function_impl.hpp | 10 +++++----- .../softmax_regression/softmax_regression_function.cpp | 3 +++ src/mlpack/tests/linear_svm_test.cpp | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/mlpack/methods/linear_svm/linear_svm_function_impl.hpp b/src/mlpack/methods/linear_svm/linear_svm_function_impl.hpp index be586e6bcf..3e8c3217bc 100644 --- a/src/mlpack/methods/linear_svm/linear_svm_function_impl.hpp +++ b/src/mlpack/methods/linear_svm/linear_svm_function_impl.hpp @@ -221,7 +221,7 @@ double LinearSVMFunction::Evaluate( } arma::mat margin = scores - (arma::repmat(arma::ones(numClasses).t() - * (scores % groundTruth.cols(firstId,lastId)), numClasses, 1)) + * (scores % groundTruth.cols(firstId, lastId)), numClasses, 1)) + delta - (delta * groundTruth.cols(firstId, lastId)); // The Hinge Loss Function @@ -326,7 +326,7 @@ void LinearSVMFunction::Gradient( } arma::mat margin = scores - (arma::repmat(arma::ones(numClasses).t() - * (scores % groundTruth.cols(firstId,lastId)), numClasses, 1)) + * (scores % groundTruth.cols(firstId, lastId)), numClasses, 1)) + delta - (delta * groundTruth.cols(firstId, lastId)); // For each sample, find the total number of classes where @@ -334,7 +334,7 @@ void LinearSVMFunction::Gradient( arma::mat mask = margin.for_each([](arma::mat::elem_type& val) { val = (val > 0) ? 1: 0; }); - arma::mat difference = groundTruth.cols(firstId,lastId) + arma::mat difference = groundTruth.cols(firstId, lastId) % (-arma::repmat(arma::sum(mask), numClasses, 1)) + mask; // Check intercept condition @@ -454,7 +454,7 @@ double LinearSVMFunction::EvaluateWithGradient( } arma::mat margin = scores - (arma::repmat(arma::ones(numClasses).t() - * (scores % groundTruth.cols(firstId,lastId)), numClasses, 1)) + * (scores % groundTruth.cols(firstId, lastId)), numClasses, 1)) + delta - (delta * groundTruth.cols(firstId, lastId)); // For each sample, find the total number of classes where @@ -462,7 +462,7 @@ double LinearSVMFunction::EvaluateWithGradient( arma::mat mask = margin.for_each([](arma::mat::elem_type& val) { val = (val > 0) ? 1: 0; }); - arma::mat difference = groundTruth.cols(firstId,lastId) + arma::mat difference = groundTruth.cols(firstId, lastId) % (-arma::repmat(arma::sum(mask), numClasses, 1)) + mask; // Check intercept condition diff --git a/src/mlpack/methods/softmax_regression/softmax_regression_function.cpp b/src/mlpack/methods/softmax_regression/softmax_regression_function.cpp index 0878957f29..4752e6107d 100644 --- a/src/mlpack/methods/softmax_regression/softmax_regression_function.cpp +++ b/src/mlpack/methods/softmax_regression/softmax_regression_function.cpp @@ -123,6 +123,9 @@ void SoftmaxRegressionFunction::GetGroundTruthMatrix( arma::uvec rowPointers(labels.n_elem); arma::uvec colPointers(labels.n_elem + 1); + // colPointers[0] needs to be set to 0. + colPointers[0] = 0; + // Row pointers are the labels of the examples, and column pointers are the // number of cumulative entries made uptil that column. for (size_t i = 0; i < labels.n_elem; i++) diff --git a/src/mlpack/tests/linear_svm_test.cpp b/src/mlpack/tests/linear_svm_test.cpp index 1e0bf27a6a..41045d235c 100644 --- a/src/mlpack/tests/linear_svm_test.cpp +++ b/src/mlpack/tests/linear_svm_test.cpp @@ -754,7 +754,7 @@ BOOST_AUTO_TEST_CASE(LinearSVMParallelSGDTwoClasses) // Compare training accuracy to 1. const double acc = lsvm.ComputeAccuracy(data, labels); - BOOST_REQUIRE_CLOSE(acc, 1.0, 1.0); + BOOST_REQUIRE_CLOSE(acc, 1.0, 2.0); // Create test dataset. for (size_t i = 0; i < points / 2; i++) @@ -770,7 +770,7 @@ BOOST_AUTO_TEST_CASE(LinearSVMParallelSGDTwoClasses) // Compare test accuracy to 1. const double testAcc = lsvm.ComputeAccuracy(data, labels); - BOOST_REQUIRE_CLOSE(testAcc, 1.0, 1.0); + BOOST_REQUIRE_CLOSE(testAcc, 1.0, 2.0); } #endif