From 3f430510fe1460ee10b300dc0ab2ec1a08f8f464 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Wed, 17 Jan 2018 01:44:33 +0530 Subject: [PATCH 01/34] Updating CMakeLists.txt --- src/mlpack/tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index 4bd5af0035..0522f5dc74 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -130,6 +130,7 @@ add_executable(mlpack_test main_tests/preprocess_binarize_test.cpp main_tests/preprocess_imputer_test.cpp main_tests/preprocess_split_test.cpp + main_tests/perceptron_test.cpp ) # Link dependencies of test executable. From 368547487282408f4b7de8a4ea27b09eed021230 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Wed, 17 Jan 2018 02:02:59 +0530 Subject: [PATCH 02/34] adding binding tests for perceptron_main.cpp --- .../tests/main_tests/perceptron_test.cpp | 322 ++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 src/mlpack/tests/main_tests/perceptron_test.cpp diff --git a/src/mlpack/tests/main_tests/perceptron_test.cpp b/src/mlpack/tests/main_tests/perceptron_test.cpp new file mode 100644 index 0000000000..c761b293a8 --- /dev/null +++ b/src/mlpack/tests/main_tests/perceptron_test.cpp @@ -0,0 +1,322 @@ +/** + * @file perceptron_test.cpp + * @author B Kartheek Reddy + * + * Test mlpackMain() of perceptron_main.cpp. + **/ + +#define BINDING_TYPE BINDING_TYPE_TEST + +static const std::string testName = "PerceptronModel"; + + +#include +#include +#include +#include "test_helper.hpp" + +#include + +using namespace mlpack; + +struct PerceptronTestFixture +{ + public: + PerceptronTestFixture() + { + try { + // Cache in the options for this program. + CLI::RestoreSettings(testName); + } catch (std::invalid_argument e) { + Log::Fatal << "Invalid Test Name" << e.what() << std::endl; + } + + } + + ~PerceptronTestFixture() + { + // Clear the settings. + CLI::ClearSettings(); + } + +}; + +void resetSettings() { + CLI::ClearSettings(); + CLI::RestoreSettings(testName); +} + +BOOST_FIXTURE_TEST_SUITE(PerceptronMainTest, PerceptronTestFixture); + +/** + * Checking for dimensionality of the test data set + **/ +BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData) +{ + constexpr int N = 10; + constexpr int D = 4; + constexpr int M = 20; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses + + arma::mat testX = arma::randu(D-1,M); // test data with wrong dimensionality + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("test", std::move(testX)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; + +} + +/** + * Ensuring that re-training of an existing model with different of classes is checked + **/ +BOOST_AUTO_TEST_CASE(PerceptronReTrainWithWrongClasses) +{ + arma::mat trainX1; + if(!data::Load("train_data_3_classes.csv",trainX1)) { + BOOST_FAIL("Could not load the train data train_data_3_classes.csv"); + } + + SetInputParam("training",std::move(trainX1)); //last column of trainX1 contains the class labels + + //training model using first training dataset + mlpackMain(); + + PerceptronModel model = CLI::GetParam("output_model"); + + resetSettings(); + + arma::mat trainX2; + + if(!data::Load("train_data_5_classes.csv",trainX2)) { + BOOST_FAIL("Could not load the train data train_data_5_classes.csv"); + } + + SetInputParam("training",std::move(trainX2)); //last column of trainX2 contains the class labels + SetInputParam("input_model",std::move(model)); + + Log::Fatal.ignoreInput=true; + BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); + Log::Fatal.ignoreInput=false; +} + +/** + * Ensuring that the response size is checked + **/ +BOOST_AUTO_TEST_CASE(PerceptronWrongResponseSizeTest) +{ + constexpr int D = 5; + constexpr int N = 10; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; // response vector with wrong size + + trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << endr; // 8 responses + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Ensuring that absence of responses is checked. + */ +BOOST_AUTO_TEST_CASE(PerceptronNoResponsesTest) +{ + constexpr int N = 10; + constexpr int D = 1; + + arma::mat trainX = arma::randu(D, N); + SetInputParam("training", std::move(trainX)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Ensuring that absence of training data is checked. + */ +BOOST_AUTO_TEST_CASE(PerceptronNoTrainingDataTest) +{ + arma::rowvec trainY; + trainY << 1 << 1 << 0 << 1 << 0 << 0 <(D, N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses + + arma::mat testX = arma::randu(D, M); + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("test", testX); + + //first solution + mlpackMain(); + + PerceptronModel model = CLI::GetParam("output_model"); + const arma::rowvec testY1 = CLI::GetParam("output"); + + resetSettings(); + + SetInputParam("input_model", std::move(model)); + SetInputParam("test", std::move(testX)); + + //second solution + mlpackMain(); + + const arma::rowvec testY2 = CLI::GetParam("output"); + + BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); +} + +/** + * Ensuring that test data dimensionality is checked when model is loaded. + */ +BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData2) +{ + constexpr int N = 10; + constexpr int D = 3; + constexpr int M = 15; + + arma::mat trainX = arma::randu(D, N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + + // training the model + mlpackMain(); + + PerceptronModel model = CLI::GetParam("output_model"); + + resetSettings(); + + arma::mat testX = arma::randu(D - 1, M); // test data with Wrong dimensionality. + SetInputParam("input_model", std::move(model)); + SetInputParam("test", std::move(testX)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Checking two options of specifying responses (extra row in train matrix and + * extra parameter) and ensuring that predictions are the same. + */ +BOOST_AUTO_TEST_CASE(PerceptronResponsesRepresentationTest) +{ + arma::mat trainX1({{1.0, 2.0, 3.0}, {1.0, 4.0, 9.0}, {0,1,1}}); + arma::mat testX({{4.0,5.0},{1.0,6.0}}); + + SetInputParam("training", trainX1); + SetInputParam("test", testX); + + // The first solution. + mlpackMain(); + + const arma::rowvec testY1 = CLI::GetParam("output"); + + resetSettings(); + + arma::mat trainX2({{1.0, 2.0, 3.0},{1.0, 4.0, 9.0}}); + arma::rowvec trainY2({0,1,1}); + + SetInputParam("training", std::move(trainX2)); + SetInputParam("labels", std::move(trainY2)); + SetInputParam("test", std::move(testX)); + + // The second solution. + mlpackMain(); + + const arma::rowvec testY2 = CLI::GetParam("output"); + + BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); +} + +/** + * Checking that that size and dimensionality of prediction is correct. + */ +BOOST_AUTO_TEST_CASE(PerceptronPredictionDimTest) +{ + constexpr int N = 10; + constexpr int D = 3; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses + + arma::mat testX = arma::randu(D,N); + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("test", std::move(testX)); + + mlpackMain(); + + const arma::rowvec testY = CLI::GetParam("output"); + + BOOST_REQUIRE_EQUAL(testY.n_rows,1); + BOOST_REQUIRE_EQUAL(testY.n_cols,N); +} + +/** + * Ensuring that the max_iterations is non negative + **/ +BOOST_AUTO_TEST_CASE(PerceptronNonNegMaxIterationTest) +{ + constexpr int N = 10; + constexpr int D = 3; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses + + arma::testX = arma::randu(D,N); + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("test", std::move(testX)); + SetInputParam("max_iterations", int (-1)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; + +} + +BOOST_AUTO_TEST_SUITE_END(); \ No newline at end of file From f9fdd9aabf46cd1c80e87299160006511c83d672 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Wed, 17 Jan 2018 02:30:15 +0530 Subject: [PATCH 03/34] adding binding tests for perceptron_main.cpp --- src/mlpack/tests/main_tests/perceptron_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/tests/main_tests/perceptron_test.cpp b/src/mlpack/tests/main_tests/perceptron_test.cpp index c761b293a8..0cb5c49644 100644 --- a/src/mlpack/tests/main_tests/perceptron_test.cpp +++ b/src/mlpack/tests/main_tests/perceptron_test.cpp @@ -306,7 +306,7 @@ BOOST_AUTO_TEST_CASE(PerceptronNonNegMaxIterationTest) trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses - arma::testX = arma::randu(D,N); + arma::mat testX = arma::randu(D,N); SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); @@ -316,7 +316,7 @@ BOOST_AUTO_TEST_CASE(PerceptronNonNegMaxIterationTest) Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; - + } BOOST_AUTO_TEST_SUITE_END(); \ No newline at end of file From 74bcafe9998824191cb36edcedf2d958214e6842 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 20 Jan 2018 23:53:57 +0530 Subject: [PATCH 04/34] adding binding tests for logistic regression --- .../main_tests/logistic_regression_test.cpp | 310 ++++++++++++++++++ 1 file changed, 310 insertions(+) create mode 100644 src/mlpack/tests/main_tests/logistic_regression_test.cpp diff --git a/src/mlpack/tests/main_tests/logistic_regression_test.cpp b/src/mlpack/tests/main_tests/logistic_regression_test.cpp new file mode 100644 index 0000000000..5a76ff0e86 --- /dev/null +++ b/src/mlpack/tests/main_tests/logistic_regression_test.cpp @@ -0,0 +1,310 @@ +/** + * @file logistic_regression_test.cpp + * @author B Kartheek Reddy + * + * Test mlpackMain() of logistic_regression_main.cpp. + **/ + +#define BINDING_TYPE BINDING_TYPE_TEST + +static const std::string testName = "LogisticRegression"; + + +#include +#include +#include +#include "test_helper.hpp" + +#include + +using namespace mlpack; + + +struct LogisticRegressionTestFixture +{ + public: + LogisticRegressionTestFixture() + { + try { + // Cache in the options for this program. + CLI::RestoreSettings(testName); + } catch (std::invalid_argument e) { + Log::Fatal << "Invalid Test Name : " << e.what() << std::endl; + } + + } + + ~LogisticRegressionTestFixture() + { + // Clear the settings. + CLI::ClearSettings(); + } + +}; + +BOOST_FIXTURE_TEST_SUITE(LogisticRegressionMainTest, LogisticRegressionTestFixture); + +/** + * Ensuring that absence of training data is checked. + **/ +BOOST_AUTO_TEST_CASE(LRNoTrainingData) +{ + arma::rowvec trainY; + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses + + SetInputParam("training_responses", std::move(trainY)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; + +} + +/** + * Ensuring that absence of responses is checked. + */ +BOOST_AUTO_TEST_CASE(LRNoResponses) +{ + constexpr int N = 10; + constexpr int D = 1; + + arma::mat trainX = arma::randu(D, N); + SetInputParam("training", std::move(trainX)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Checking that that size and dimensionality of prediction is correct. + */ +BOOST_AUTO_TEST_CASE(LRPridictionSizeCheck) +{ + constexpr int N = 10; + constexpr int D = 3; + constexpr int M = 15; + + arma::mat trainX = arma::randu(D, N); + arma::rowvec trainY; + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses + arma::mat testX = arma::randu(D, M); + + SetInputParam("training", std::move(trainX)); + SetInputParam("training_responses", std::move(trainY)); + SetInputParam("test", std::move(testX)); + + mlpackMain(); + + const arma::rowvec testY = CLI::GetParam("output_predictions"); + + BOOST_REQUIRE_EQUAL(testY.n_rows, 1); + BOOST_REQUIRE_EQUAL(testY.n_cols, M); +} + +/** + * Ensuring that the response size is checked + **/ +BOOST_AUTO_TEST_CASE(LRWrongResponseSizeTest) +{ + constexpr int D = 3; + constexpr int N = 10; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; // response vector with wrong size + + trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << arma::endr; // 8 responses - incorrect size + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Checking two options of specifying responses (extra row in train matrix and + * extra parameter) and ensuring that predictions are the same. + */ +BOOST_AUTO_TEST_CASE(LRResponsesRepresentationTest) +{ + arma::mat trainX1({{1.0, 2.0, 3.0}, {1.0, 4.0, 9.0}, {0,1,1}}); + arma::mat testX({{4.0,5.0},{1.0,6.0}}); + + SetInputParam("training",std::move(trainX1)); + SetInputParam("test", testX); + + // The first solution. + mlpackMain(); + + const arma::rowvec testY1 = CLI::GetParam("output"); + + //reset the settings + CLI::ClearSettings(); + CLI::RestoreSettings(testName); + + arma::mat trainX2({{1.0, 2.0, 3.0},{1.0, 4.0, 9.0}}); + arma::rowvec trainY2({0,1,1}); + + SetInputParam("training", std::move(trainX2)); + SetInputParam("labels", std::move(trainY2)); + SetInputParam("test", std::move(testX)); + + // The second solution. + mlpackMain(); + + const arma::rowvec testY2 = CLI::GetParam("output"); + + BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); +} + +/** + * Check that model can saved / loaded and used. Ensuring that results are the + * same. + */ +BOOST_AUTO_TEST_CASE(LRModelReload) +{ + constexpr int N = 10; + constexpr int D = 3; + constexpr int M = 15; + + arma::mat trainX = arma::randu(D, N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses + + arma::mat testX = arma::randu(D, M); + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("test", testX); + + //first solution + mlpackMain(); + + LogisticRegression<> model = CLI::GetParam>("output_model"); + const arma::rowvec testY1 = CLI::GetParam("output"); + + //reset the settings + CLI::ClearSettings(); + CLI::RestoreSettings(testName); + + SetInputParam("input_model", std::move(model)); + SetInputParam("test", std::move(testX)); + + //second solution + mlpackMain(); + + const arma::rowvec testY2 = CLI::GetParam("output"); + + BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); +} + +/** + * Checking for dimensionality of the test data set + **/ +BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData) +{ + constexpr int N = 10; + constexpr int D = 4; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses + + arma::mat testX = arma::randu(D-1,N); // test data with wrong dimensionality + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("test", std::move(testX)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; + +} + +/** + * Ensuring that test data dimensionality is checked when model is loaded. + */ +BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData2) +{ + constexpr int N = 10; + constexpr int D = 3; + constexpr int M = 15; + + arma::mat trainX = arma::randu(D, N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + + // training the model + mlpackMain(); + + LogisticRegression<> model = CLI::GetParam>("output_model"); + + //reset the settings + CLI::ClearSettings(); + CLI::RestoreSettings(testName); + + arma::mat testX = arma::randu(D - 1, M); // test data with Wrong dimensionality. + SetInputParam("input_model", std::move(model)); + SetInputParam("test", std::move(testX)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Ensuring that training responses contain only two classes (0 or 1) + **/ +BOOST_AUTO_TEST_CASE(LRTrainWithMoreThanTwoClasses) +{ + constexpr int N = 8; + constexpr int D = 2; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 2 << 1 << 3 << 1 << arma::endr; // 8 responses containing more than two classes + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Ensuring that max iteration for optimizers is non negative + **/ +BOOST_AUTO_TEST_CASE(LRMaxIterationNonNegativeTest) +{ + constexpr int N = 10; + constexpr int D = 3; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("max_iterations", int(-1)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; + +} + +BOOST_AUTO_TEST_SUITE_END(); + + From 9a694d1e847ba163d7e5b72ebe20f3b3192f73c0 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 20 Jan 2018 23:55:52 +0530 Subject: [PATCH 05/34] Update CMakeLists.txt --- src/mlpack/tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index 0522f5dc74..c5f68eba11 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -130,7 +130,7 @@ add_executable(mlpack_test main_tests/preprocess_binarize_test.cpp main_tests/preprocess_imputer_test.cpp main_tests/preprocess_split_test.cpp - main_tests/perceptron_test.cpp + main_tests/logistic_regression_test.cpp ) # Link dependencies of test executable. From e222c673d6d619c1a6fd002fe87cb0580013d0ac Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Mon, 22 Jan 2018 19:20:15 +0530 Subject: [PATCH 06/34] adding more test cases --- .../main_tests/logistic_regression_test.cpp | 70 ++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/src/mlpack/tests/main_tests/logistic_regression_test.cpp b/src/mlpack/tests/main_tests/logistic_regression_test.cpp index 5a76ff0e86..c0998442b7 100644 --- a/src/mlpack/tests/main_tests/logistic_regression_test.cpp +++ b/src/mlpack/tests/main_tests/logistic_regression_test.cpp @@ -285,7 +285,7 @@ BOOST_AUTO_TEST_CASE(LRTrainWithMoreThanTwoClasses) /** * Ensuring that max iteration for optimizers is non negative **/ -BOOST_AUTO_TEST_CASE(LRMaxIterationNonNegativeTest) +BOOST_AUTO_TEST_CASE(LRNonNegativeMaxIterationTest) { constexpr int N = 10; constexpr int D = 3; @@ -305,6 +305,74 @@ BOOST_AUTO_TEST_CASE(LRMaxIterationNonNegativeTest) } +/** + * Ensuring that max_iterations for optimizers is integer value + **/ +BOOST_AUTO_TEST_CASE(LRIntegerMaxIterationTest) +{ + constexpr int N = 10; + constexpr int D = 3; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("max_iterations", int(0.01)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Ensuring that step size for optimizer is non negative + **/ +BOOST_AUTO_TEST_CASE(LRNonNegativeStepSizeTest) +{ + constexpr int N = 10; + constexpr int D = 2; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; + + trainY << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("optimizer", "sgd"); + SetInputParam("step_size", double (-0.01)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; +} + +/** + * Ensuring that tolerance is non negative + **/ +BOOST_AUTO_TEST_CASE(LRNonNegativeToleranceTest) +{ + constexpr int N = 10; + constexpr int D = 3; + + arma::mat trainX = arma::randu(D,N); + arma::rowvec trainY; + + trainY << 1 << 1 << 0 << 1 << 0 << 0 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("tolerance", double (-0.01)); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; + +} + BOOST_AUTO_TEST_SUITE_END(); From 9be9f0b11f10196b2187a8cc0109e4624074171e Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Mon, 22 Jan 2018 19:41:09 +0530 Subject: [PATCH 07/34] resolving the errors --- src/mlpack/tests/main_tests/logistic_regression_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/tests/main_tests/logistic_regression_test.cpp b/src/mlpack/tests/main_tests/logistic_regression_test.cpp index c0998442b7..22e8208766 100644 --- a/src/mlpack/tests/main_tests/logistic_regression_test.cpp +++ b/src/mlpack/tests/main_tests/logistic_regression_test.cpp @@ -342,7 +342,7 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeStepSizeTest) SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("optimizer", "sgd"); + SetInputParam("optimizer", std::string ("sgd")) ; SetInputParam("step_size", double (-0.01)); Log::Fatal.ignoreInput = true; From ac1722c0f447cac96f949a5e8c5b50f5ce8aaa7f Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Tue, 23 Jan 2018 23:05:51 +0530 Subject: [PATCH 08/34] adding perceptron test to CMakeLists.txt --- src/mlpack/tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index d61618ef99..660eb06869 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -71,6 +71,7 @@ add_executable(mlpack_test main_tests/preprocess_binarize_test.cpp main_tests/preprocess_imputer_test.cpp main_tests/preprocess_split_test.cpp + main_tests/perceptron_test.cpp main_tests/test_helper.hpp math_test.cpp matrix_completion_test.cpp From 877b43a139b0842030593f66c5615663327a9836 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 27 Jan 2018 01:11:49 +0530 Subject: [PATCH 09/34] resolving Errors --- .../tests/main_tests/perceptron_test.cpp | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/mlpack/tests/main_tests/perceptron_test.cpp b/src/mlpack/tests/main_tests/perceptron_test.cpp index 0cb5c49644..961c3940e7 100644 --- a/src/mlpack/tests/main_tests/perceptron_test.cpp +++ b/src/mlpack/tests/main_tests/perceptron_test.cpp @@ -16,6 +16,7 @@ static const std::string testName = "PerceptronModel"; #include "test_helper.hpp" #include +#include "../test_tools.hpp" using namespace mlpack; @@ -58,14 +59,14 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData) constexpr int M = 20; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses - arma::mat testX = arma::randu(D-1,M); // test data with wrong dimensionality + arma::mat testX = arma::randu(D-3,M); // test data with wrong dimensionality SetInputParam("training", std::move(trainX)); - SetInputParam("labels", std::move(trainY)); + // SetInputParam("labels", std::move(trainY)); SetInputParam("test", std::move(testX)); Log::Fatal.ignoreInput = true; @@ -112,13 +113,13 @@ BOOST_AUTO_TEST_CASE(PerceptronReTrainWithWrongClasses) **/ BOOST_AUTO_TEST_CASE(PerceptronWrongResponseSizeTest) { - constexpr int D = 5; + constexpr int D = 2; constexpr int N = 10; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; // response vector with wrong size + arma::Row trainY; // response vector with wrong size - trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << endr; // 8 responses + trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 1 << 0 << endr; // 8 responses SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); @@ -149,7 +150,7 @@ BOOST_AUTO_TEST_CASE(PerceptronNoResponsesTest) */ BOOST_AUTO_TEST_CASE(PerceptronNoTrainingDataTest) { - arma::rowvec trainY; + arma::Row trainY; trainY << 1 << 1 << 0 << 1 << 0 << 0 <(D, N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses @@ -184,7 +185,7 @@ BOOST_AUTO_TEST_CASE(PerceptronModelReload) mlpackMain(); PerceptronModel model = CLI::GetParam("output_model"); - const arma::rowvec testY1 = CLI::GetParam("output"); + const arma::Row testY1 = CLI::GetParam>("output"); resetSettings(); @@ -194,7 +195,7 @@ BOOST_AUTO_TEST_CASE(PerceptronModelReload) //second solution mlpackMain(); - const arma::rowvec testY2 = CLI::GetParam("output"); + const arma::Row testY2 = CLI::GetParam>("output"); BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); } @@ -209,7 +210,7 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData2) constexpr int M = 15; arma::mat trainX = arma::randu(D, N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses @@ -247,12 +248,12 @@ BOOST_AUTO_TEST_CASE(PerceptronResponsesRepresentationTest) // The first solution. mlpackMain(); - const arma::rowvec testY1 = CLI::GetParam("output"); + const arma::Row testY1 = CLI::GetParam>("output"); resetSettings(); arma::mat trainX2({{1.0, 2.0, 3.0},{1.0, 4.0, 9.0}}); - arma::rowvec trainY2({0,1,1}); + arma::Row trainY2({0,1,1}); SetInputParam("training", std::move(trainX2)); SetInputParam("labels", std::move(trainY2)); @@ -261,7 +262,7 @@ BOOST_AUTO_TEST_CASE(PerceptronResponsesRepresentationTest) // The second solution. mlpackMain(); - const arma::rowvec testY2 = CLI::GetParam("output"); + const arma::Row testY2 = CLI::GetParam>("output"); BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); } @@ -275,7 +276,7 @@ BOOST_AUTO_TEST_CASE(PerceptronPredictionDimTest) constexpr int D = 3; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses @@ -287,7 +288,7 @@ BOOST_AUTO_TEST_CASE(PerceptronPredictionDimTest) mlpackMain(); - const arma::rowvec testY = CLI::GetParam("output"); + const arma::Row testY = CLI::GetParam>("output"); BOOST_REQUIRE_EQUAL(testY.n_rows,1); BOOST_REQUIRE_EQUAL(testY.n_cols,N); @@ -302,7 +303,7 @@ BOOST_AUTO_TEST_CASE(PerceptronNonNegMaxIterationTest) constexpr int D = 3; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses From d59ac2ec70e099f4dbbcdc6f117baf1b1b53bcb5 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Tue, 30 Jan 2018 22:00:50 +0530 Subject: [PATCH 10/34] resolving Errors --- .../main_tests/logistic_regression_test.cpp | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/mlpack/tests/main_tests/logistic_regression_test.cpp b/src/mlpack/tests/main_tests/logistic_regression_test.cpp index 22e8208766..597d18bfbf 100644 --- a/src/mlpack/tests/main_tests/logistic_regression_test.cpp +++ b/src/mlpack/tests/main_tests/logistic_regression_test.cpp @@ -49,10 +49,10 @@ BOOST_FIXTURE_TEST_SUITE(LogisticRegressionMainTest, LogisticRegressionTestFixtu **/ BOOST_AUTO_TEST_CASE(LRNoTrainingData) { - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses - SetInputParam("training_responses", std::move(trainY)); + SetInputParam("labels", std::move(trainY)); Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); @@ -86,17 +86,17 @@ BOOST_AUTO_TEST_CASE(LRPridictionSizeCheck) constexpr int M = 15; arma::mat trainX = arma::randu(D, N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses arma::mat testX = arma::randu(D, M); SetInputParam("training", std::move(trainX)); - SetInputParam("training_responses", std::move(trainY)); + SetInputParam("labels", std::move(trainY)); SetInputParam("test", std::move(testX)); mlpackMain(); - const arma::rowvec testY = CLI::GetParam("output_predictions"); + const arma::Row testY = CLI::GetParam>("output"); BOOST_REQUIRE_EQUAL(testY.n_rows, 1); BOOST_REQUIRE_EQUAL(testY.n_cols, M); @@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(LRWrongResponseSizeTest) constexpr int N = 10; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; // response vector with wrong size + arma::Row trainY; // response vector with wrong size trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << arma::endr; // 8 responses - incorrect size @@ -138,14 +138,14 @@ BOOST_AUTO_TEST_CASE(LRResponsesRepresentationTest) // The first solution. mlpackMain(); - const arma::rowvec testY1 = CLI::GetParam("output"); + const arma::Row testY1 = CLI::GetParam>("output"); //reset the settings CLI::ClearSettings(); CLI::RestoreSettings(testName); arma::mat trainX2({{1.0, 2.0, 3.0},{1.0, 4.0, 9.0}}); - arma::rowvec trainY2({0,1,1}); + arma::Row trainY2({0,1,1}); SetInputParam("training", std::move(trainX2)); SetInputParam("labels", std::move(trainY2)); @@ -154,7 +154,7 @@ BOOST_AUTO_TEST_CASE(LRResponsesRepresentationTest) // The second solution. mlpackMain(); - const arma::rowvec testY2 = CLI::GetParam("output"); + const arma::Row testY2 = CLI::GetParam>("output"); BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); } @@ -170,11 +170,11 @@ BOOST_AUTO_TEST_CASE(LRModelReload) constexpr int M = 15; arma::mat trainX = arma::randu(D, N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses - arma::mat testX = arma::randu(D, M); + arma::mat testX = arma::randu(D, M); SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); @@ -184,7 +184,7 @@ BOOST_AUTO_TEST_CASE(LRModelReload) mlpackMain(); LogisticRegression<> model = CLI::GetParam>("output_model"); - const arma::rowvec testY1 = CLI::GetParam("output"); + const arma::Row testY1 = CLI::GetParam>("output"); //reset the settings CLI::ClearSettings(); @@ -196,7 +196,7 @@ BOOST_AUTO_TEST_CASE(LRModelReload) //second solution mlpackMain(); - const arma::rowvec testY2 = CLI::GetParam("output"); + const arma::Row testY2 = CLI::GetParam>("output"); BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); } @@ -210,7 +210,7 @@ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData) constexpr int D = 4; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses @@ -236,7 +236,7 @@ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData2) constexpr int M = 15; arma::mat trainX = arma::randu(D, N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses @@ -270,7 +270,7 @@ BOOST_AUTO_TEST_CASE(LRTrainWithMoreThanTwoClasses) constexpr int D = 2; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 2 << 1 << 3 << 1 << arma::endr; // 8 responses containing more than two classes @@ -291,7 +291,7 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeMaxIterationTest) constexpr int D = 3; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses @@ -314,7 +314,7 @@ BOOST_AUTO_TEST_CASE(LRIntegerMaxIterationTest) constexpr int D = 3; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses @@ -336,7 +336,7 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeStepSizeTest) constexpr int D = 2; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; + arma::Row trainY; trainY << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses @@ -359,7 +359,7 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeToleranceTest) constexpr int D = 3; arma::mat trainX = arma::randu(D,N); - arma::rowvec trainY; + arma::Row trainY; trainY << 1 << 1 << 0 << 1 << 0 << 0 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses From ed97d7882ae61ef9af1b998cf6fef3bca7743b75 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Tue, 30 Jan 2018 22:02:54 +0530 Subject: [PATCH 11/34] addressing the binding test failures --- src/mlpack/methods/perceptron/perceptron_main.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/mlpack/methods/perceptron/perceptron_main.cpp b/src/mlpack/methods/perceptron/perceptron_main.cpp index c7a5b17796..78f29babd6 100644 --- a/src/mlpack/methods/perceptron/perceptron_main.cpp +++ b/src/mlpack/methods/perceptron/perceptron_main.cpp @@ -173,9 +173,22 @@ static void mlpackMain() if (CLI::HasParam("labels")) { labelsIn = std::move(CLI::GetParam>("labels")); + + //Checking the size of the responses and training data + if(labelsIn.n_cols != trainingData.n_cols) { + Log::Fatal << "The responses must have the same number of columns " + "as the training set." << endl; + } } else { + // Checking the size of training data if no labels are passed + if (trainingData.n_rows < 2) + { + Log::Fatal << "Can't get responses from training data " + "since it has less than 2 rows." << endl; + } + // Use the last row of the training data as the labels. Log::Info << "Using the last dimension of training set as labels." << endl; From 8739d84ce8a8eada5dc69522d12554f64fc65aa5 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Tue, 30 Jan 2018 22:03:29 +0530 Subject: [PATCH 12/34] addressing binding test failures --- .../logistic_regression_main.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp index 02a12022e3..bcd6e7fb87 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp @@ -141,9 +141,15 @@ static void mlpackMain() const double tolerance = CLI::GetParam("tolerance"); const double stepSize = CLI::GetParam("step_size"); const size_t batchSize = (size_t) CLI::GetParam("batch_size"); + + // Checking that max iterations is non negative + if(CLI::GetParam("max_iterations") <= 0) { + Log::Fatal << "Max Iterations (" << CLI::GetParam("max_iterations") << ") cannot be negative" << endl; + } + const size_t maxIterations = (size_t) CLI::GetParam("max_iterations"); const double decisionBoundary = CLI::GetParam("decision_boundary"); - + // One of training and input_model must be specified. RequireAtLeastOnePassed({ "training", "input_model" }, true); @@ -228,6 +234,11 @@ static void mlpackMain() } else if (CLI::HasParam("training")) { + + if(regressors.n_rows<2) { + Log::Fatal << "Can't get responses from training data " + "since it has less than 2 rows." << endl; + } // The initial predictors for y, Nx1. responses = arma::conv_to>::from( regressors.row(regressors.n_rows - 1)); @@ -272,6 +283,12 @@ static void mlpackMain() { testSet = std::move(CLI::GetParam("test")); + // checking the dimensionality of the test data + if(testSet.n_rows != model.Parameters().n_cols-1) { + Log::Fatal << "Test data dimensionality (" << testSet.n_rows << ") must " + << "be the same as the dimensionality of the Training Data (" << model.Parameters().n_cols-1 << ")!" << endl; + } + // We must perform predictions on the test set. Training (and the // optimizer) are irrelevant here; we'll pass in the model we have. if (CLI::HasParam("output")) From f9a7ac950e848ac6b55294c96e996f6d0aff895d Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Tue, 30 Jan 2018 22:04:01 +0530 Subject: [PATCH 13/34] Minor changes --- src/mlpack/tests/main_tests/perceptron_test.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/mlpack/tests/main_tests/perceptron_test.cpp b/src/mlpack/tests/main_tests/perceptron_test.cpp index 961c3940e7..d0df6acfc0 100644 --- a/src/mlpack/tests/main_tests/perceptron_test.cpp +++ b/src/mlpack/tests/main_tests/perceptron_test.cpp @@ -307,11 +307,8 @@ BOOST_AUTO_TEST_CASE(PerceptronNonNegMaxIterationTest) trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses - arma::mat testX = arma::randu(D,N); - SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("test", std::move(testX)); SetInputParam("max_iterations", int (-1)); Log::Fatal.ignoreInput = true; From 3e0dffa81950e78d273ce07e9bec7be9d4076182 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 3 Feb 2018 01:13:34 +0530 Subject: [PATCH 14/34] Adding checks for failing binding tests in logistic_regression_test.cpp --- .../logistic_regression_main.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp index bcd6e7fb87..4a05d61ec2 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp @@ -141,12 +141,6 @@ static void mlpackMain() const double tolerance = CLI::GetParam("tolerance"); const double stepSize = CLI::GetParam("step_size"); const size_t batchSize = (size_t) CLI::GetParam("batch_size"); - - // Checking that max iterations is non negative - if(CLI::GetParam("max_iterations") <= 0) { - Log::Fatal << "Max Iterations (" << CLI::GetParam("max_iterations") << ") cannot be negative" << endl; - } - const size_t maxIterations = (size_t) CLI::GetParam("max_iterations"); const double decisionBoundary = CLI::GetParam("decision_boundary"); @@ -167,6 +161,14 @@ static void mlpackMain() ReportIgnoredParam({{ "test", false }}, "output"); ReportIgnoredParam({{ "test", false }}, "output_probabilities"); + // Max Iterations needs to be positive. + RequireParamValue("max_iterations", [](int x) { return x >= 0; }, + true, "max_iterations must be positive or zero"); + + // Batch Size needs to be greater than zero. + RequireParamValue("batch_size", [](int x) { return x > 0; }, + true, "batch_size must be greater than zero"); + // Tolerance needs to be positive. RequireParamValue("tolerance", [](double x) { return x >= 0.0; }, true, "tolerance must be positive or zero"); @@ -234,7 +236,7 @@ static void mlpackMain() } else if (CLI::HasParam("training")) { - + // Checking the size of training data if no labels are passed if(regressors.n_rows<2) { Log::Fatal << "Can't get responses from training data " "since it has less than 2 rows." << endl; From 6991d384937b1d22ff4104e14171f514c9495802 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 3 Feb 2018 01:15:59 +0530 Subject: [PATCH 15/34] adding some additional tests and minor fixes --- .../main_tests/logistic_regression_test.cpp | 307 +++++++++++++++--- 1 file changed, 268 insertions(+), 39 deletions(-) diff --git a/src/mlpack/tests/main_tests/logistic_regression_test.cpp b/src/mlpack/tests/main_tests/logistic_regression_test.cpp index 597d18bfbf..fb3f785d0c 100644 --- a/src/mlpack/tests/main_tests/logistic_regression_test.cpp +++ b/src/mlpack/tests/main_tests/logistic_regression_test.cpp @@ -2,8 +2,14 @@ * @file logistic_regression_test.cpp * @author B Kartheek Reddy * - * Test mlpackMain() of logistic_regression_main.cpp. - **/ + * Test mlpackMain() of logistic_regression_main.cpp + * + * mlpack is free software; you may redistribute it and/or modify it under the + * terms of the 3-clause BSD license. You should have received a copy of the + * 3-clause BSD license along with mlpack. If not, see + * http://www.opensource.org/licenses/BSD-3-Clause for more information. + */ + #define BINDING_TYPE BINDING_TYPE_TEST @@ -25,13 +31,8 @@ struct LogisticRegressionTestFixture public: LogisticRegressionTestFixture() { - try { - // Cache in the options for this program. - CLI::RestoreSettings(testName); - } catch (std::invalid_argument e) { - Log::Fatal << "Invalid Test Name : " << e.what() << std::endl; - } - + // Cache in the options for this program. + CLI::RestoreSettings(testName); } ~LogisticRegressionTestFixture() @@ -53,7 +54,8 @@ BOOST_AUTO_TEST_CASE(LRNoTrainingData) trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses SetInputParam("labels", std::move(trainY)); - + + // training data is not provided. Should throw a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -71,6 +73,7 @@ BOOST_AUTO_TEST_CASE(LRNoResponses) arma::mat trainX = arma::randu(D, N); SetInputParam("training", std::move(trainX)); + // labels to the training data is not provided should throw a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -94,10 +97,13 @@ BOOST_AUTO_TEST_CASE(LRPridictionSizeCheck) SetInputParam("labels", std::move(trainY)); SetInputParam("test", std::move(testX)); + // training the model mlpackMain(); - const arma::Row testY = CLI::GetParam>("output"); + // get the output predictions of the test data + const arma::Row &testY = CLI::GetParam>("output"); + // output predictions size must match the test data set size BOOST_REQUIRE_EQUAL(testY.n_rows, 1); BOOST_REQUIRE_EQUAL(testY.n_cols, M); } @@ -118,6 +124,7 @@ BOOST_AUTO_TEST_CASE(LRWrongResponseSizeTest) SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); + // Labels with incorrect size. Should throw a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); Log::Fatal.ignoreInput = false; @@ -138,12 +145,15 @@ BOOST_AUTO_TEST_CASE(LRResponsesRepresentationTest) // The first solution. mlpackMain(); - const arma::Row testY1 = CLI::GetParam>("output"); + // get the output + const arma::Row testY1 = std::move(CLI::GetParam>("output")); //reset the settings CLI::ClearSettings(); CLI::RestoreSettings(testName); + // now train by providing labels as extra parameter + arma::mat trainX2({{1.0, 2.0, 3.0},{1.0, 4.0, 9.0}}); arma::Row trainY2({0,1,1}); @@ -154,8 +164,10 @@ BOOST_AUTO_TEST_CASE(LRResponsesRepresentationTest) // The second solution. mlpackMain(); - const arma::Row testY2 = CLI::GetParam>("output"); + // get the output + const arma::Row &testY2 = CLI::GetParam>("output"); + // both solutions should be equal. BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); } @@ -183,8 +195,10 @@ BOOST_AUTO_TEST_CASE(LRModelReload) //first solution mlpackMain(); - LogisticRegression<> model = CLI::GetParam>("output_model"); - const arma::Row testY1 = CLI::GetParam>("output"); + // get the output model obtained from training + LogisticRegression<> model = std::move(CLI::GetParam>("output_model")); + // get the output + const arma::Row testY1 = std::move(CLI::GetParam>("output")); //reset the settings CLI::ClearSettings(); @@ -196,8 +210,10 @@ BOOST_AUTO_TEST_CASE(LRModelReload) //second solution mlpackMain(); - const arma::Row testY2 = CLI::GetParam>("output"); + // get the output + const arma::Row &testY2 = CLI::GetParam>("output"); + // Both solutions must be equal. BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); } @@ -220,6 +236,7 @@ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData) SetInputParam("labels", std::move(trainY)); SetInputParam("test", std::move(testX)); + //Dimensionality of test data is wrong. Should throw a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -246,7 +263,8 @@ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData2) // training the model mlpackMain(); - LogisticRegression<> model = CLI::GetParam>("output_model"); + // get the output model obtained from training + LogisticRegression<> model = std::move(CLI::GetParam>("output_model")); //reset the settings CLI::ClearSettings(); @@ -256,6 +274,7 @@ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData2) SetInputParam("input_model", std::move(model)); SetInputParam("test", std::move(testX)); + // test data dimensionality is wrong. Should throw a runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -277,6 +296,7 @@ BOOST_AUTO_TEST_CASE(LRTrainWithMoreThanTwoClasses) SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); + //training data contains more than two classes. Should throw a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); Log::Fatal.ignoreInput = false; @@ -299,34 +319,13 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeMaxIterationTest) SetInputParam("labels", std::move(trainY)); SetInputParam("max_iterations", int(-1)); + // Maximum iterations is negative. Should a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } -/** - * Ensuring that max_iterations for optimizers is integer value - **/ -BOOST_AUTO_TEST_CASE(LRIntegerMaxIterationTest) -{ - constexpr int N = 10; - constexpr int D = 3; - - arma::mat trainX = arma::randu(D,N); - arma::Row trainY; - - trainY << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses - - SetInputParam("training", std::move(trainX)); - SetInputParam("labels", std::move(trainY)); - SetInputParam("max_iterations", int(0.01)); - - Log::Fatal.ignoreInput = true; - BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); - Log::Fatal.ignoreInput = false; -} - /** * Ensuring that step size for optimizer is non negative **/ @@ -345,6 +344,7 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeStepSizeTest) SetInputParam("optimizer", std::string ("sgd")) ; SetInputParam("step_size", double (-0.01)); + // step size for optimizer is negative. Should throw a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -367,12 +367,241 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeToleranceTest) SetInputParam("labels", std::move(trainY)); SetInputParam("tolerance", double (-0.01)); + // tolerance is negative. Should throw a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } +/** + * Ensuring changing Maximum number of iterations changes the output model + **/ +BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) +{ + constexpr int N = 10; + constexpr int D = 3; + + arma::mat trainX = arma::randu(D,N); + arma::Row trainY; + + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + + SetInputParam("training", trainX); + SetInputParam("labels", trainY); + SetInputParam("max_iterations", int (1)); + + // first solution + mlpackMain(); + + // get the parameters of the output model obtained after first training + const arma::rowvec parameters1 = std::move(CLI::GetParam>("output_model").Parameters()); + + //reset the settings + CLI::ClearSettings(); + CLI::RestoreSettings(testName); + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("max_iterations", int (100)); + + // second solution + mlpackMain(); + + // get the parameters of the output model obtained after second training + const arma::rowvec ¶meters2 = CLI::GetParam>("output_model").Parameters(); + + // Check that the parameters (parameters1 and parameters2) are not equal which ensures Max Iteration changes the output model + // arma::all function checks that each element of the vector is equal to zero + BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(Max Iteration) has no effect on the output" ); +} + +/** + * Ensuring that lambda has some effects on the output + **/ +BOOST_AUTO_TEST_CASE(LRLambdaChangeTest) +{ + constexpr int N = 10; + constexpr int D = 4; + + arma::mat trainX = arma::randu(D,N); + arma::Row trainY; + + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + + SetInputParam("training", trainX); + SetInputParam("labels", trainY); + SetInputParam("lambda", double(0)); + + // first solution + mlpackMain(); + + // get the parameters of the output model obtained after first training + const arma::rowvec parameters1 = std::move(CLI::GetParam>("output_model").Parameters()); + + //reset the settings + CLI::ClearSettings(); + CLI::RestoreSettings(testName); + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("lambda", double(1000)); + + // second solution + mlpackMain(); + + // get the parameters of the output model obtained after second training + const arma::rowvec ¶meters2 = CLI::GetParam>("output_model").Parameters(); + + // Check that the parameters (parameters1 and parameters2) are not equal which ensures lambda changes the output model + // arma::all function checks that each element of the vector is equal to zero + BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(lambda) has no effect on the output" ); +} + +/** + * Ensuring that Step size has some effects on the output + **/ +BOOST_AUTO_TEST_CASE(LRStepSizeChangeTest) +{ + constexpr int N = 10; + constexpr int D = 3; + + arma::mat trainX = arma::randu(D,N); + arma::Row trainY; + + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + + SetInputParam("training", trainX); + SetInputParam("labels", trainY); + SetInputParam("optimizer", std::string("sgd")); + SetInputParam("step_size", double(0.02)); + + // first solution + mlpackMain(); + + // get the parameters of the output model obtained after first training + const arma::rowvec parameters1 = std::move(CLI::GetParam>("output_model").Parameters()); + + //reset the settings + CLI::ClearSettings(); + CLI::RestoreSettings(testName); + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("optimizer", std::string("sgd")); + SetInputParam("step_size", double(1.02)); + + // second solution + mlpackMain(); + + // get the parameters of the output model obtained after second training + const arma::rowvec ¶meters2 = CLI::GetParam>("output_model").Parameters(); + + // Check that the parameters (parameters1 and parameters2) are not equal which ensures Step Size changes the output model + // arma::all function checks that each element of the vector is equal to zero + BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(Step Size) has no effect on the output" ); +} + +/** + * Ensuring that lbfgs optimizer converges to a different result than sgd + **/ +BOOST_AUTO_TEST_CASE(LROptimizerChangeTest) +{ + constexpr int N = 10; + constexpr int D = 3; + + arma::mat trainX = arma::randu(D,N); + arma::Row trainY; + + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + + SetInputParam("training", trainX); + SetInputParam("labels", trainY); + SetInputParam("optimizer", std::string("lbfgs")); + SetInputParam("max_iterations", int(1000)); + + // first solution + mlpackMain(); + + // get the parameters of the output model obtained after first training + const arma::rowvec parameters1 = std::move(CLI::GetParam>("output_model").Parameters()); + + //reset the settings + CLI::ClearSettings(); + CLI::RestoreSettings(testName); + + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); + SetInputParam("optimizer", std::string("sgd")); + SetInputParam("max_iterations", int(1000)); + + // second solution + mlpackMain(); + + // get the parameters of the output model obtained after second training + const arma::rowvec ¶meters2 = CLI::GetParam>("output_model").Parameters(); + + // Check that the parameters (parameters1 and parameters2) are not equal which + // ensures that different optimizer converge to different results + // arma::all function checks that each element of the vector is equal to zero + BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(Step Size) has no effect on the output" ); + +} + +/** + * Ensuring decision_boundary parameter does something + **/ +BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) +{ + constexpr int N = 10; + constexpr int D = 3; + constexpr int M = 15; + + arma::mat trainX = arma::randu(D,N); + arma::Row trainY; + + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + + arma::mat testX = arma::randu(D,M); + + SetInputParam("training", trainX); + SetInputParam("labels", trainY); + SetInputParam("decision_boundary", double(1)); + SetInputParam("test", testX); + + // first solution + mlpackMain(); + + // get the output after first training + const arma::Row &output1 = CLI::GetParam>("output"); + + // Check that the parameters (parameters1 and parameters2) are not equal which + // ensures that decision boundary has some effect on the output + // arma::all function checks that each element of the vector is equal to zero + BOOST_REQUIRE_MESSAGE(arma::all(output1 == 0), "Parameter(Decision Boudary) has no effect on the output"); + + //reset the settings + CLI::ClearSettings(); + CLI::RestoreSettings(testName); + + SetInputParam("training", trainX); + SetInputParam("labels", trainY); + SetInputParam("decision_boundary", double(0)); + SetInputParam("test", testX); + + // second solution + mlpackMain(); + + // get the output after second training + const arma::Row &output2 = CLI::GetParam>("output"); + + // Check that the parameters (parameters1 and parameters2) are not equal which + // ensures that decision boundary has som effect on the output + // arma::all function checks that each element of the vector is equal to one + BOOST_REQUIRE_MESSAGE(arma::all(output2 == 1), "Parameter(Decision Boudary) has no effect on the output"); + +} + BOOST_AUTO_TEST_SUITE_END(); From 0310f4aa32fc6ae4f9c909fd54698362cea89975 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 3 Feb 2018 01:18:44 +0530 Subject: [PATCH 16/34] Adding more tests to existing perceptron_test.cpp --- .../tests/main_tests/perceptron_test.cpp | 477 +++++++++++------- 1 file changed, 281 insertions(+), 196 deletions(-) diff --git a/src/mlpack/tests/main_tests/perceptron_test.cpp b/src/mlpack/tests/main_tests/perceptron_test.cpp index 20ee150adf..1dae568787 100644 --- a/src/mlpack/tests/main_tests/perceptron_test.cpp +++ b/src/mlpack/tests/main_tests/perceptron_test.cpp @@ -1,18 +1,21 @@ /** - * @file perceptron_test.cpp - * @author B Kartheek Reddy - * - * Test mlpackMain() of perceptron_main.cpp. - **/ - + * @file perceptron_test.cpp + * @author Manish Kumar + * + * Test mlpackMain() of perceptron_main.cpp. + * + * mlpack is free software; you may redistribute it and/or modify it under the + * terms of the 3-clause BSD license. You should have received a copy of the + * 3-clause BSD license along with mlpack. If not, see + * http://www.opensource.org/licenses/BSD-3-Clause for more information. + */ #define BINDING_TYPE BINDING_TYPE_TEST -static const std::string testName = "PerceptronModel"; - - #include -#include +static const std::string testName = "Perceptron"; + #include +#include #include "test_helper.hpp" #include @@ -22,57 +25,214 @@ using namespace mlpack; struct PerceptronTestFixture { - public: - PerceptronTestFixture() - { - try { - // Cache in the options for this program. - CLI::RestoreSettings(testName); - } catch (std::invalid_argument e) { - Log::Fatal << "Invalid Test Name" << e.what() << std::endl; - } - - } - - ~PerceptronTestFixture() - { - // Clear the settings. - CLI::ClearSettings(); - } + public: + PerceptronTestFixture() + { + // Cache in the options for this program. + CLI::RestoreSettings(testName); + } + ~PerceptronTestFixture() + { + // Clear the settings. + CLI::ClearSettings(); + } }; +// reset the parameters void resetSettings() { - CLI::ClearSettings(); + CLI::ClearSettings(); CLI::RestoreSettings(testName); } -BOOST_FIXTURE_TEST_SUITE(PerceptronMainTest, PerceptronTestFixture); +BOOST_FIXTURE_TEST_SUITE(PerceptronMainTest, + PerceptronTestFixture); /** - * Checking for dimensionality of the test data set - **/ -BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData) + * Ensure that we get desired dimensions when both training + * data and labels are passed. + */ +BOOST_AUTO_TEST_CASE(PerceptronOutputDimensionTest) { - constexpr int N = 10; - constexpr int D = 4; - constexpr int M = 20; + arma::mat inputData; + if (!data::Load("trainSet.csv", inputData)) + BOOST_FAIL("Cannot load train dataset trainSet.csv!"); - arma::mat trainX = arma::randu(D,N); - arma::Row trainY; + // Get the labels out. + arma::Row labels(inputData.n_cols); + for (size_t i = 0; i < inputData.n_cols; ++i) + labels[i] = inputData(inputData.n_rows - 1, i); - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses + // Delete the last row containing labels from input dataset. + inputData.shed_row(inputData.n_rows - 1); - arma::mat testX = arma::randu(D-3,M); // test data with wrong dimensionality + arma::mat testData; + if (!data::Load("testSet.csv", testData)) + BOOST_FAIL("Cannot load test dataset testSet.csv!"); - SetInputParam("training", std::move(trainX)); - // SetInputParam("labels", std::move(trainY)); - SetInputParam("test", std::move(testX)); + // Delete the last row containing labels from test dataset. + testData.shed_row(testData.n_rows - 1); - Log::Fatal.ignoreInput = true; - BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); - Log::Fatal.ignoreInput = false; + size_t testSize = testData.n_cols; + // Input training data. + SetInputParam("training", std::move(inputData)); + SetInputParam("labels", std::move(labels)); + + // Input test data. + SetInputParam("test", std::move(testData)); + + mlpackMain(); + + // Check that number of output points are equal to number of input points. + BOOST_REQUIRE_EQUAL(CLI::GetParam>("output").n_cols, + testSize); + + // Check output have only single row. + BOOST_REQUIRE_EQUAL(CLI::GetParam>("output").n_rows, 1); +} + +/** + * Check that last row of input file is used as labels + * when labels are not passed specifically and results + * are same from both label and labeless models. + */ +BOOST_AUTO_TEST_CASE(PerceptronLabelsLessDimensionTest) +{ + // Train perceptron without providing labels. + arma::mat inputData; + if (!data::Load("trainSet.csv", inputData)) + BOOST_FAIL("Cannot load train dataset trainSet.csv!"); + + // Get the labels out. + arma::Row labels(inputData.n_cols); + for (size_t i = 0; i < inputData.n_cols; ++i) + labels[i] = inputData(inputData.n_rows - 1, i); + + arma::mat testData; + if (!data::Load("testSet.csv", testData)) + BOOST_FAIL("Cannot load test dataset testSet.csv!"); + + // Delete the last row containing labels from test dataset. + testData.shed_row(testData.n_rows - 1); + + size_t testSize = testData.n_cols; + + // Input training data. + SetInputParam("training", inputData); + + // Input test data. + SetInputParam("test", testData); + + mlpackMain(); + + // Check that number of output points are equal to number of input points. + BOOST_REQUIRE_EQUAL(CLI::GetParam>("output").n_cols, + testSize); + + // Check output have only single row. + BOOST_REQUIRE_EQUAL(CLI::GetParam>("output").n_rows, 1); + + // Reset data passed. + CLI::GetSingleton().Parameters()["training"].wasPassed = false; + CLI::GetSingleton().Parameters()["test"].wasPassed = false; + + inputData.shed_row(inputData.n_rows - 1); + + // Store outputs. + arma::Row output; + output = std::move(CLI::GetParam>("output")); + + // Now train pereptron with labels provided. + + // Input training data. + SetInputParam("training", std::move(inputData)); + SetInputParam("test", std::move(testData)); + // Pass Labels. + SetInputParam("labels", std::move(labels)); + + mlpackMain(); + + // Check that number of output points are equal to number of input points. + BOOST_REQUIRE_EQUAL(CLI::GetParam>("output").n_cols, + testSize); + + // Check output have only single row. + BOOST_REQUIRE_EQUAL(CLI::GetParam>("output").n_rows, 1); + + // Check that initial output and final output matrix + // from two models are same. + CheckMatrices(output, CLI::GetParam>("output")); +} + +/** + * Ensure that saved model can be used again. + */ +BOOST_AUTO_TEST_CASE(PerceptronModelReuseTest) +{ + arma::mat inputData; + if (!data::Load("trainSet.csv", inputData)) + BOOST_FAIL("Cannot load train dataset trainSet.csv!"); + + arma::mat testData; + if (!data::Load("testSet.csv", testData)) + BOOST_FAIL("Cannot load test dataset testSet.csv!"); + + // Delete the last row containing labels from test dataset. + testData.shed_row(testData.n_rows - 1); + + size_t testSize = testData.n_cols; + + // Input training data. + SetInputParam("training", std::move(inputData)); + + // Input test data. + SetInputParam("test", testData); + + mlpackMain(); + + arma::Row output; + output = std::move(CLI::GetParam>("output")); + + // Reset passed parameters. + CLI::GetSingleton().Parameters()["training"].wasPassed = false; + CLI::GetSingleton().Parameters()["test"].wasPassed = false; + + // Input trained model. + SetInputParam("test", std::move(testData)); + SetInputParam("input_model", + std::move(CLI::GetParam("output_model"))); + + mlpackMain(); + + // Check that number of output points are equal to number of input points. + BOOST_REQUIRE_EQUAL(CLI::GetParam>("output").n_cols, + testSize); + + // Check output have only single row. + BOOST_REQUIRE_EQUAL(CLI::GetParam>("output").n_rows, 1); + + // Check that initial output and final output matrix + // using saved model are same. + CheckMatrices(output, CLI::GetParam>("output")); +} + +/** + * Ensure that max_iterations is always non-negative. + */ +BOOST_AUTO_TEST_CASE(PerceptronMaxItrTest) +{ + arma::mat inputData; + if (!data::Load("trainSet.csv", inputData)) + BOOST_FAIL("Cannot load train dataset trainSet.csv!"); + + // Input training data. + SetInputParam("training", std::move(inputData)); + SetInputParam("max_iterations", (int) -1); + + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; } /** @@ -80,32 +240,71 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData) **/ BOOST_AUTO_TEST_CASE(PerceptronReTrainWithWrongClasses) { - arma::mat trainX1; - if(!data::Load("train_data_3_classes.csv",trainX1)) { - BOOST_FAIL("Could not load the train data train_data_3_classes.csv"); - } + arma::mat trainX1; + arma::Row labelsX1; - SetInputParam("training",std::move(trainX1)); //last column of trainX1 contains the class labels + // loading a train data set with 3 classes + if(!data::Load("vc2.csv",trainX1)) { + BOOST_FAIL("Could not load the train data (vc2.csv)"); + } - //training model using first training dataset - mlpackMain(); + // loading the corresponding labels to the dataset + if(!data::Load("vc2_labels.txt", labelsX1)) { + BOOST_FAIL("Could not load the train data (vc2_labels.csv)"); + } - PerceptronModel model = CLI::GetParam("output_model"); + SetInputParam("training", std::move(trainX1)); // train data + SetInputParam("labels", std::move(labelsX1)); // labels for the train data - resetSettings(); + //training model using first training dataset + mlpackMain(); - arma::mat trainX2; + // get the output model obtained after training + PerceptronModel model = std::move(CLI::GetParam("output_model")); - if(!data::Load("train_data_5_classes.csv",trainX2)) { - BOOST_FAIL("Could not load the train data train_data_5_classes.csv"); - } + resetSettings(); - SetInputParam("training",std::move(trainX2)); //last column of trainX2 contains the class labels - SetInputParam("input_model",std::move(model)); + arma::mat trainX2; + + // loading a train dataset with 5 classes + if(!data::Load("train_data_5_classes.csv",trainX2)) { + BOOST_FAIL("Could not load the train data train_data_5_classes.csv"); + } + + SetInputParam("training",std::move(trainX2)); //last column of trainX2 contains the class labels + SetInputParam("input_model",std::move(model)); + + // re-training an existing model of 3 classes with training data of 5 classes. Should give runtime error + Log::Fatal.ignoreInput=true; + BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); + Log::Fatal.ignoreInput=false; +} + +/** + * Checking for dimensionality of the test data set + **/ +BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData) +{ + constexpr int N = 10; + constexpr int D = 4; + constexpr int M = 20; + + arma::mat trainX = arma::randu(D,N); + arma::Row trainY; + + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses + + arma::mat testX = arma::randu(D-3,M); // test data with wrong dimensionality + + SetInputParam("training", std::move(trainX)); + // SetInputParam("labels", std::move(trainY)); + SetInputParam("test", std::move(testX)); + + // test data set with wrong dimensionality. Should give runtime error + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); + Log::Fatal.ignoreInput = false; - Log::Fatal.ignoreInput=true; - BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); - Log::Fatal.ignoreInput=false; } /** @@ -113,20 +312,21 @@ BOOST_AUTO_TEST_CASE(PerceptronReTrainWithWrongClasses) **/ BOOST_AUTO_TEST_CASE(PerceptronWrongResponseSizeTest) { - constexpr int D = 2; - constexpr int N = 10; - - arma::mat trainX = arma::randu(D,N); - arma::Row trainY; // response vector with wrong size + constexpr int D = 2; + constexpr int N = 10; + + arma::mat trainX = arma::randu(D,N); + arma::Row trainY; // response vector with wrong size - trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 1 << 0 << endr; // 8 responses + trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 1 << 0 << endr; // 8 responses - SetInputParam("training", std::move(trainX)); - SetInputParam("labels", std::move(trainY)); + SetInputParam("training", std::move(trainX)); + SetInputParam("labels", std::move(trainY)); - Log::Fatal.ignoreInput = true; - BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); - Log::Fatal.ignoreInput = false; + // labels for training data have wrong size. Should give runtime error + Log::Fatal.ignoreInput = true; + BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); + Log::Fatal.ignoreInput = false; } /** @@ -140,11 +340,13 @@ BOOST_AUTO_TEST_CASE(PerceptronNoResponsesTest) arma::mat trainX = arma::randu(D, N); SetInputParam("training", std::move(trainX)); + // No labels for training data. Should give runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } + /** * Ensuring that absence of training data is checked. */ @@ -155,50 +357,12 @@ BOOST_AUTO_TEST_CASE(PerceptronNoTrainingDataTest) SetInputParam("labels", std::move(trainY)); + // No training data. Should give runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } -/** - * Check that model can saved / loaded and used. Ensuring that results are the - * same. - */ -BOOST_AUTO_TEST_CASE(PerceptronModelReload) -{ - constexpr int N = 10; - constexpr int D = 4; - constexpr int M = 15; - - arma::mat trainX = arma::randu(D, N); - arma::Row trainY; - - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses - - arma::mat testX = arma::randu(D, M); - - SetInputParam("training", std::move(trainX)); - SetInputParam("labels", std::move(trainY)); - SetInputParam("test", testX); - - //first solution - mlpackMain(); - - PerceptronModel model = CLI::GetParam("output_model"); - const arma::Row testY1 = CLI::GetParam>("output"); - - resetSettings(); - - SetInputParam("input_model", std::move(model)); - SetInputParam("test", std::move(testX)); - - //second solution - mlpackMain(); - - const arma::Row testY2 = CLI::GetParam>("output"); - - BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); -} /** * Ensuring that test data dimensionality is checked when model is loaded. @@ -220,7 +384,8 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData2) // training the model mlpackMain(); - PerceptronModel model = CLI::GetParam("output_model"); + // get the output model obtained after the training + PerceptronModel model = std::move(CLI::GetParam("output_model")); resetSettings(); @@ -228,92 +393,12 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData2) SetInputParam("input_model", std::move(model)); SetInputParam("test", std::move(testX)); + // wrong dimensionality of test data. Should give runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } -/** - * Checking two options of specifying responses (extra row in train matrix and - * extra parameter) and ensuring that predictions are the same. - */ -BOOST_AUTO_TEST_CASE(PerceptronResponsesRepresentationTest) -{ - arma::mat trainX1({{1.0, 2.0, 3.0}, {1.0, 4.0, 9.0}, {0,1,1}}); - arma::mat testX({{4.0,5.0},{1.0,6.0}}); - - SetInputParam("training", trainX1); - SetInputParam("test", testX); - - // The first solution. - mlpackMain(); - - const arma::Row testY1 = CLI::GetParam>("output"); - - resetSettings(); - - arma::mat trainX2({{1.0, 2.0, 3.0},{1.0, 4.0, 9.0}}); - arma::Row trainY2({0,1,1}); - - SetInputParam("training", std::move(trainX2)); - SetInputParam("labels", std::move(trainY2)); - SetInputParam("test", std::move(testX)); - - // The second solution. - mlpackMain(); - - const arma::Row testY2 = CLI::GetParam>("output"); - - BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); -} - -/** - * Checking that that size and dimensionality of prediction is correct. - */ -BOOST_AUTO_TEST_CASE(PerceptronPredictionDimTest) -{ - constexpr int N = 10; - constexpr int D = 3; - - arma::mat trainX = arma::randu(D,N); - arma::Row trainY; - - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses - - arma::mat testX = arma::randu(D,N); - - SetInputParam("training", std::move(trainX)); - SetInputParam("labels", std::move(trainY)); - SetInputParam("test", std::move(testX)); - - mlpackMain(); - - const arma::Row testY = CLI::GetParam>("output"); - - BOOST_REQUIRE_EQUAL(testY.n_rows,1); - BOOST_REQUIRE_EQUAL(testY.n_cols,N); -} - -/** - * Ensuring that the max_iterations is non negative - **/ -BOOST_AUTO_TEST_CASE(PerceptronNonNegMaxIterationTest) -{ - constexpr int N = 10; - constexpr int D = 3; - - arma::mat trainX = arma::randu(D,N); - arma::Row trainY; - - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses - - SetInputParam("training", std::move(trainX)); - SetInputParam("labels", std::move(trainY)); - SetInputParam("max_iterations", int (-1)); - - Log::Fatal.ignoreInput = true; - BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); - Log::Fatal.ignoreInput = false; -} BOOST_AUTO_TEST_SUITE_END(); + From d77a2f5ef8dc791bf9d4d23bafc332032bbc3ec7 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 3 Feb 2018 01:19:34 +0530 Subject: [PATCH 17/34] adding dataset needed for perceptron binding test --- .../tests/data/train_data_5_classes.csv | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/mlpack/tests/data/train_data_5_classes.csv diff --git a/src/mlpack/tests/data/train_data_5_classes.csv b/src/mlpack/tests/data/train_data_5_classes.csv new file mode 100644 index 0000000000..8d9dd6106e --- /dev/null +++ b/src/mlpack/tests/data/train_data_5_classes.csv @@ -0,0 +1,63 @@ +4.7,3.2,1.6,0.2,0 +4.8,3.1,1.6,0.2,0 +5.4,3.4,1.5,0.4,1 +5.2,4.1,1.5,0.1,0 +5.5,4.2,1.4,0.2,0 +4.9,3.1,1.5,0.1,2 +5,3.2,1.2,0.2,0 +5.5,3.5,1.3,0.2,0 +4.9,3.1,1.5,0.1,2 +4.4,3,1.3,0.2,0 +5.1,3.4,1.5,0.2,0 +5,3.5,1.3,0.3,3 +4.5,2.3,1.3,0.3,0 +4.4,3.2,1.3,0.2,0 +5,3.5,1.6,0.6,0 +5.1,3.8,1.9,0.4,0 +4.8,3,1.4,0.3,4 +5.1,3.8,1.6,0.2,0 +4.6,3.2,1.4,0.2,4 +5.3,3.7,1.5,0.2,1 +5,3.3,1.4,0.2,1 +5.7,2.6,3.5,1,1 +5.5,2.4,3.8,1.1,1 +5.5,2.4,3.7,1,1 +5.8,2.7,3.9,1.2,0 +6,2.7,5.1,1.6,1 +5.4,3,4.5,1.5,0 +6,3.4,4.5,1.6,1 +6.7,3.1,4.7,1.5,4 +6.3,2.3,4.4,1.3,1 +5.6,3,4.1,1.3,1 +5.5,2.5,4,1.3,2 +5.5,2.6,4.4,1.2,1 +6.1,3,4.6,1.4,1 +5.8,2.6,4,1.2,3 +5,2.3,3.3,1,3 +5.6,2.7,4.2,1.3,3 +5.7,3,4.2,1.2,1 +5.7,2.9,4.2,1.3,1 +6.2,2.9,4.3,1.3,4 +5.1,2.5,3,1.1,1 +5.7,2.8,4.1,1.3,1 +7.2,3,5.8,1.6,2 +7.4,2.8,6.1,1.9,1 +7.9,3.8,6.4,2,2 +6.4,2.8,5.6,2.2,0 +6.3,2.8,5.1,1.5,3 +6.1,2.6,5.6,1.4,0 +7.7,3,6.1,2.3,2 +6.3,3.4,5.6,2.4,2 +6.4,3.1,5.5,1.8,0 +6,3,4.8,1.8,0 +6.9,3.1,5.4,2.1,2 +6.7,3.1,5.6,2.4,2 +6.9,3.1,5.1,2.3,4 +5.8,2.7,5.1,1.9,4 +6.8,3.2,5.9,2.3,4 +6.7,3.3,5.7,2.5,3 +6.7,3,5.2,2.3,2 +6.3,2.5,5,1.9,2 +6.5,3,5.2,2,1 +6.2,3.4,5.4,2.3,2 +5.9,3,5.1,1.8,2 From c330581996885e425f78782a8a4443e59bdc0db2 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 3 Feb 2018 01:20:23 +0530 Subject: [PATCH 18/34] Updated CMakeLists.txt with Logistic Regression Binding test --- src/mlpack/tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index 89a02af421..ae3135533f 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -128,6 +128,7 @@ add_executable(mlpack_test main_tests/decision_tree_test.cpp main_tests/decision_stump_test.cpp main_tests/linear_regression_test.cpp + main_tests/logistic_regression_test.cpp main_tests/nbc_test.cpp main_tests/pca_test.cpp main_tests/perceptron_test.cpp From faee9d3c2f833d499606ca4fc6e818d743ebab5d Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sun, 4 Feb 2018 23:22:49 +0530 Subject: [PATCH 19/34] Style fixes --- .../methods/logistic_regression/logistic_regression_main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp index 4a05d61ec2..ecaae67159 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp @@ -237,7 +237,7 @@ static void mlpackMain() else if (CLI::HasParam("training")) { // Checking the size of training data if no labels are passed - if(regressors.n_rows<2) { + if (regressors.n_rows<2) { Log::Fatal << "Can't get responses from training data " "since it has less than 2 rows." << endl; } From 5e8970e2a379fe37be0599a7439dc23182500c35 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sun, 4 Feb 2018 23:30:23 +0530 Subject: [PATCH 20/34] style fix for logistic_regression_main.cpp --- .../methods/logistic_regression/logistic_regression_main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp index ecaae67159..e9e43c418a 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp @@ -237,7 +237,7 @@ static void mlpackMain() else if (CLI::HasParam("training")) { // Checking the size of training data if no labels are passed - if (regressors.n_rows<2) { + if (regressors.n_rows < 2) { Log::Fatal << "Can't get responses from training data " "since it has less than 2 rows." << endl; } From 49359333d6595bc267ad9a69b08fa804fdf27bfd Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sun, 4 Feb 2018 23:35:52 +0530 Subject: [PATCH 21/34] Style fix --- .../methods/logistic_regression/logistic_regression_main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp index e9e43c418a..4f51fcf9c4 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp @@ -286,7 +286,7 @@ static void mlpackMain() testSet = std::move(CLI::GetParam("test")); // checking the dimensionality of the test data - if(testSet.n_rows != model.Parameters().n_cols-1) { + if (testSet.n_rows != model.Parameters().n_cols-1) { Log::Fatal << "Test data dimensionality (" << testSet.n_rows << ") must " << "be the same as the dimensionality of the Training Data (" << model.Parameters().n_cols-1 << ")!" << endl; } From 01e487aad554110787a5a0d5a3b83c56e1b3dac6 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sun, 4 Feb 2018 23:39:50 +0530 Subject: [PATCH 22/34] adding more style fixes --- .../logistic_regression/logistic_regression_main.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp index 4f51fcf9c4..e84dbc979c 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp @@ -237,7 +237,8 @@ static void mlpackMain() else if (CLI::HasParam("training")) { // Checking the size of training data if no labels are passed - if (regressors.n_rows < 2) { + if (regressors.n_rows < 2) + { Log::Fatal << "Can't get responses from training data " "since it has less than 2 rows." << endl; } @@ -286,7 +287,8 @@ static void mlpackMain() testSet = std::move(CLI::GetParam("test")); // checking the dimensionality of the test data - if (testSet.n_rows != model.Parameters().n_cols-1) { + if (testSet.n_rows != model.Parameters().n_cols-1) + { Log::Fatal << "Test data dimensionality (" << testSet.n_rows << ") must " << "be the same as the dimensionality of the Training Data (" << model.Parameters().n_cols-1 << ")!" << endl; } From e2e0ddadb351e634ca9ff6291422dc0816c84985 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sun, 4 Feb 2018 23:44:29 +0530 Subject: [PATCH 23/34] more style fixes --- .../methods/logistic_regression/logistic_regression_main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp index e84dbc979c..57b35a2fe5 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp @@ -242,6 +242,7 @@ static void mlpackMain() Log::Fatal << "Can't get responses from training data " "since it has less than 2 rows." << endl; } + // The initial predictors for y, Nx1. responses = arma::conv_to>::from( regressors.row(regressors.n_rows - 1)); From 5591cfa8d2ba079ebccf08559c7eeb77c28ec837 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Thu, 8 Feb 2018 23:16:23 +0530 Subject: [PATCH 24/34] Style Fix --- src/mlpack/methods/perceptron/perceptron_main.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/mlpack/methods/perceptron/perceptron_main.cpp b/src/mlpack/methods/perceptron/perceptron_main.cpp index 4cc4e27929..e821a3d4ca 100644 --- a/src/mlpack/methods/perceptron/perceptron_main.cpp +++ b/src/mlpack/methods/perceptron/perceptron_main.cpp @@ -178,8 +178,9 @@ static void mlpackMain() { labelsIn = std::move(CLI::GetParam>("labels")); - //Checking the size of the responses and training data - if(labelsIn.n_cols != trainingData.n_cols) { + // Checking the size of the responses and training data + if (labelsIn.n_cols != trainingData.n_cols) + { Log::Fatal << "The responses must have the same number of columns " "as the training set." << endl; } @@ -189,8 +190,8 @@ static void mlpackMain() // Checking the size of training data if no labels are passed if (trainingData.n_rows < 2) { - Log::Fatal << "Can't get responses from training data " - "since it has less than 2 rows." << endl; + Log::Fatal << "Can't get responses from training data " + "since it has less than 2 rows." << endl; } // Use the last row of the training data as the labels. From 876058e3d5b4c8eb9ecda3ff67eba5f32ff3e312 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Thu, 8 Feb 2018 23:17:42 +0530 Subject: [PATCH 25/34] Style Fix --- .../logistic_regression/logistic_regression_main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp index fa08aa96d8..5981baf9b2 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp @@ -238,8 +238,8 @@ static void mlpackMain() } else if (CLI::HasParam("training")) { - // Checking the size of training data if no labels are passed - if (regressors.n_rows < 2) + // Checking the size of training data if no labels are passed + if (regressors.n_rows < 2) { Log::Fatal << "Can't get responses from training data " "since it has less than 2 rows." << endl; @@ -290,10 +290,10 @@ static void mlpackMain() testSet = std::move(CLI::GetParam("test")); // checking the dimensionality of the test data - if (testSet.n_rows != model.Parameters().n_cols-1) + if (testSet.n_rows != model->Parameters().n_cols - 1) { - Log::Fatal << "Test data dimensionality (" << testSet.n_rows << ") must " - << "be the same as the dimensionality of the Training Data (" << model.Parameters().n_cols-1 << ")!" << endl; + Log::Fatal << "Test data dimensionality (" << testSet.n_rows << ") must " + << "be the same as the dimensionality of the Training Data (" << model->Parameters().n_cols-1 << ")!" << endl; } // We must perform predictions on the test set. Training (and the From e24cb1dc316b82a51eb035f715f0b66f6e5ef732 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Thu, 8 Feb 2018 23:17:59 +0530 Subject: [PATCH 26/34] Handle Memory as per 1214 and style fix --- .../tests/main_tests/perceptron_test.cpp | 98 +++++++++++-------- 1 file changed, 57 insertions(+), 41 deletions(-) diff --git a/src/mlpack/tests/main_tests/perceptron_test.cpp b/src/mlpack/tests/main_tests/perceptron_test.cpp index e0a6290e92..aa2b9db067 100644 --- a/src/mlpack/tests/main_tests/perceptron_test.cpp +++ b/src/mlpack/tests/main_tests/perceptron_test.cpp @@ -41,9 +41,11 @@ struct PerceptronTestFixture }; // reset the parameters -void resetSettings() { +void resetSettings() +{ + bindings::tests::CleanMemory(); CLI::ClearSettings(); - CLI::RestoreSettings(testName); + CLI::RestoreSettings(testName); } BOOST_FIXTURE_TEST_SUITE(PerceptronMainTest, @@ -144,9 +146,7 @@ BOOST_AUTO_TEST_CASE(PerceptronLabelsLessDimensionTest) arma::Row output; output = std::move(CLI::GetParam>("output")); - bindings::tests::CleanMemory(); - - // Now train perceptron with labels provided. + // Now train pereptron with labels provided. // Input training data. SetInputParam("training", std::move(inputData)); @@ -239,96 +239,109 @@ BOOST_AUTO_TEST_CASE(PerceptronMaxItrTest) } /** - * Ensuring that re-training of an existing model with different of classes is checked + * Ensuring that re-training of an existing model + * with different of classes is checked **/ BOOST_AUTO_TEST_CASE(PerceptronReTrainWithWrongClasses) { - arma::mat trainX1; + arma::mat trainX1; arma::Row labelsX1; // loading a train data set with 3 classes - if(!data::Load("vc2.csv",trainX1)) { + if (!data::Load("vc2.csv", trainX1)) + { BOOST_FAIL("Could not load the train data (vc2.csv)"); } // loading the corresponding labels to the dataset - if(!data::Load("vc2_labels.txt", labelsX1)) { + if (!data::Load("vc2_labels.txt", labelsX1)) + { BOOST_FAIL("Could not load the train data (vc2_labels.csv)"); } SetInputParam("training", std::move(trainX1)); // train data - SetInputParam("labels", std::move(labelsX1)); // labels for the train data + // labels for the train data + SetInputParam("labels", std::move(labelsX1)); //training model using first training dataset mlpackMain(); // get the output model obtained after training - PerceptronModel model = std::move(CLI::GetParam("output_model")); + PerceptronModel* model = + CLI::GetParam("output_model"); - resetSettings(); + // reset the data passed + CLI::GetSingleton().Parameters()["training"].wasPassed = false; + CLI::GetSingleton().Parameters()["labels"].wasPassed = false; - arma::mat trainX2; + // creating training data with five classes + constexpr int D = 3; + constexpr int N = 10; + arma::mat trainX2 = arma::randu(D, N); + arma::Row labelsX2; - // loading a train dataset with 5 classes - if(!data::Load("train_data_5_classes.csv",trainX2)) { - BOOST_FAIL("Could not load the train data train_data_5_classes.csv"); - } + // 10 responses + labelsX2 << 0 << 1 << 4 << 1 << 2 << 1 << 0 << 3 << 3 << 0 << endr; - SetInputParam("training",std::move(trainX2)); //last column of trainX2 contains the class labels - SetInputParam("input_model",std::move(model)); + //last column of trainX2 contains the class labels + SetInputParam("training", std::move(trainX2)); + SetInputParam("input_model", model); - // re-training an existing model of 3 classes with training data of 5 classes. Should give runtime error + // re-training an existing model of 3 classes + // with training data of 5 classes. Should give runtime error Log::Fatal.ignoreInput=true; - BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput=false; } /** * Checking for dimensionality of the test data set **/ -BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData) +BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData) { constexpr int N = 10; constexpr int D = 4; constexpr int M = 20; - arma::mat trainX = arma::randu(D,N); + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses + // 10 responses + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; - arma::mat testX = arma::randu(D-3,M); // test data with wrong dimensionality + // test data with wrong dimensionality + arma::mat testX = arma::randu(D-3, M); SetInputParam("training", std::move(trainX)); - // SetInputParam("labels", std::move(trainY)); + SetInputParam("labels", std::move(trainY)); SetInputParam("test", std::move(testX)); // test data set with wrong dimensionality. Should give runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; - } /** * Ensuring that the response size is checked **/ -BOOST_AUTO_TEST_CASE(PerceptronWrongResponseSizeTest) +BOOST_AUTO_TEST_CASE(PerceptronWrongResponseSizeTest) { constexpr int D = 2; constexpr int N = 10; - - arma::mat trainX = arma::randu(D,N); + + arma::mat trainX = arma::randu(D, N); arma::Row trainY; // response vector with wrong size - trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 1 << 0 << endr; // 8 responses + // 8 responses + trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - // labels for training data have wrong size. Should give runtime error + // labels for training data have wrong size. Should give runtime error Log::Fatal.ignoreInput = true; - BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } @@ -349,7 +362,6 @@ BOOST_AUTO_TEST_CASE(PerceptronNoResponsesTest) Log::Fatal.ignoreInput = false; } - /** * Ensuring that absence of training data is checked. */ @@ -366,7 +378,6 @@ BOOST_AUTO_TEST_CASE(PerceptronNoTrainingDataTest) Log::Fatal.ignoreInput = false; } - /** * Ensuring that test data dimensionality is checked when model is loaded. */ @@ -379,7 +390,8 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData2) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; // 10 responses + // 10 responses + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); @@ -388,15 +400,19 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData2) mlpackMain(); // get the output model obtained after the training - PerceptronModel model = std::move(CLI::GetParam("output_model")); + PerceptronModel* model = + CLI::GetParam("output_model"); - resetSettings(); + // reset the data passed + CLI::GetSingleton().Parameters()["training"].wasPassed = false; + CLI::GetSingleton().Parameters()["labels"].wasPassed = false; - arma::mat testX = arma::randu(D - 1, M); // test data with Wrong dimensionality. - SetInputParam("input_model", std::move(model)); + // test data with Wrong dimensionality. + arma::mat testX = arma::randu(D - 1, M); + SetInputParam("input_model", model); SetInputParam("test", std::move(testX)); - // wrong dimensionality of test data. Should give runtime error. + // wrong dimensionality of test data. Should give runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; From cb0285055f8ee1389741737cb075ec2f9108a704 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Thu, 8 Feb 2018 23:21:18 +0530 Subject: [PATCH 27/34] Handle Memory as per 1214 and Style Fix --- .../main_tests/logistic_regression_test.cpp | 292 ++++++++++-------- 1 file changed, 167 insertions(+), 125 deletions(-) diff --git a/src/mlpack/tests/main_tests/logistic_regression_test.cpp b/src/mlpack/tests/main_tests/logistic_regression_test.cpp index fb3f785d0c..5f94debfba 100644 --- a/src/mlpack/tests/main_tests/logistic_regression_test.cpp +++ b/src/mlpack/tests/main_tests/logistic_regression_test.cpp @@ -9,13 +9,10 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ - - #define BINDING_TYPE BINDING_TYPE_TEST static const std::string testName = "LogisticRegression"; - #include #include #include @@ -38,28 +35,29 @@ struct LogisticRegressionTestFixture ~LogisticRegressionTestFixture() { // Clear the settings. + bindings::tests::CleanMemory(); CLI::ClearSettings(); } - }; -BOOST_FIXTURE_TEST_SUITE(LogisticRegressionMainTest, LogisticRegressionTestFixture); +BOOST_FIXTURE_TEST_SUITE(LogisticRegressionMainTest, + LogisticRegressionTestFixture); /** * Ensuring that absence of training data is checked. **/ -BOOST_AUTO_TEST_CASE(LRNoTrainingData) +BOOST_AUTO_TEST_CASE(LRNoTrainingData) { arma::Row trainY; - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses - + // 10 responses + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; + SetInputParam("labels", std::move(trainY)); - + // training data is not provided. Should throw a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; - } /** @@ -73,7 +71,7 @@ BOOST_AUTO_TEST_CASE(LRNoResponses) arma::mat trainX = arma::randu(D, N); SetInputParam("training", std::move(trainX)); - // labels to the training data is not provided should throw a runtime error + // labels to the training data is not provided should throw a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -90,7 +88,8 @@ BOOST_AUTO_TEST_CASE(LRPridictionSizeCheck) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses + // 10 responses + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; arma::mat testX = arma::randu(D, M); SetInputParam("training", std::move(trainX)); @@ -111,22 +110,23 @@ BOOST_AUTO_TEST_CASE(LRPridictionSizeCheck) /** * Ensuring that the response size is checked **/ -BOOST_AUTO_TEST_CASE(LRWrongResponseSizeTest) +BOOST_AUTO_TEST_CASE(LRWrongResponseSizeTest) { constexpr int D = 3; constexpr int N = 10; - - arma::mat trainX = arma::randu(D,N); + + arma::mat trainX = arma::randu(D, N); arma::Row trainY; // response vector with wrong size - trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << arma::endr; // 8 responses - incorrect size + // 8 responses - incorrect size + trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); // Labels with incorrect size. Should throw a runtime error Log::Fatal.ignoreInput = true; - BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } @@ -136,26 +136,28 @@ BOOST_AUTO_TEST_CASE(LRWrongResponseSizeTest) */ BOOST_AUTO_TEST_CASE(LRResponsesRepresentationTest) { - arma::mat trainX1({{1.0, 2.0, 3.0}, {1.0, 4.0, 9.0}, {0,1,1}}); - arma::mat testX({{4.0,5.0},{1.0,6.0}}); + arma::mat trainX1({{1.0, 2.0, 3.0}, {1.0, 4.0, 9.0}, {0, 1, 1}}); + arma::mat testX({{4.0, 5.0}, {1.0, 6.0}}); - SetInputParam("training",std::move(trainX1)); + SetInputParam("training", std::move(trainX1)); SetInputParam("test", testX); // The first solution. mlpackMain(); // get the output - const arma::Row testY1 = std::move(CLI::GetParam>("output")); + const arma::Row testY1 = + std::move(CLI::GetParam>("output")); - //reset the settings + // reset the settings + bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); // now train by providing labels as extra parameter - arma::mat trainX2({{1.0, 2.0, 3.0},{1.0, 4.0, 9.0}}); - arma::Row trainY2({0,1,1}); + arma::mat trainX2({{1.0, 2.0, 3.0}, {1.0, 4.0, 9.0}}); + arma::Row trainY2({0, 1, 1}); SetInputParam("training", std::move(trainX2)); SetInputParam("labels", std::move(trainY2)); @@ -165,10 +167,12 @@ BOOST_AUTO_TEST_CASE(LRResponsesRepresentationTest) mlpackMain(); // get the output - const arma::Row &testY2 = CLI::GetParam>("output"); + const arma::Row &testY2 = + CLI::GetParam>("output"); // both solutions should be equal. - BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); + BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), + testY2.begin(), testY2.end()); } /** @@ -184,63 +188,70 @@ BOOST_AUTO_TEST_CASE(LRModelReload) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses - - arma::mat testX = arma::randu(D, M); + // 10 responses + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; + + arma::mat testX = arma::randu(D, M); SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); SetInputParam("test", testX); - //first solution + // first solution mlpackMain(); // get the output model obtained from training - LogisticRegression<> model = std::move(CLI::GetParam>("output_model")); + LogisticRegression<>* model = + CLI::GetParam*>("output_model"); // get the output - const arma::Row testY1 = std::move(CLI::GetParam>("output")); + const arma::Row testY1 = + std::move(CLI::GetParam>("output")); - //reset the settings - CLI::ClearSettings(); - CLI::RestoreSettings(testName); + // reset the data passed + CLI::GetSingleton().Parameters()["training"].wasPassed = false; + CLI::GetSingleton().Parameters()["labels"].wasPassed = false; + CLI::GetSingleton().Parameters()["test"].wasPassed = false; - SetInputParam("input_model", std::move(model)); + SetInputParam("input_model", model); SetInputParam("test", std::move(testX)); - //second solution + // second solution mlpackMain(); // get the output - const arma::Row &testY2 = CLI::GetParam>("output"); + const arma::Row &testY2 = + CLI::GetParam>("output"); // Both solutions must be equal. - BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); + BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), + testY2.begin(), testY2.end()); } /** * Checking for dimensionality of the test data set **/ -BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData) +BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData) { constexpr int N = 10; constexpr int D = 4; - arma::mat trainX = arma::randu(D,N); + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses + // 10 responses + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; - arma::mat testX = arma::randu(D-1,N); // test data with wrong dimensionality + // test data with wrong dimensionality + arma::mat testX = arma::randu(D-1, N); SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); SetInputParam("test", std::move(testX)); - //Dimensionality of test data is wrong. Should throw a runtime error + // Dimensionality of test data is wrong. Should throw a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; - } /** @@ -254,8 +265,8 @@ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData2) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses + // 10 responses + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); @@ -264,14 +275,16 @@ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData2) mlpackMain(); // get the output model obtained from training - LogisticRegression<> model = std::move(CLI::GetParam>("output_model")); + LogisticRegression<>* model = + CLI::GetParam*>("output_model"); - //reset the settings - CLI::ClearSettings(); - CLI::RestoreSettings(testName); + // reset the data passed + CLI::GetSingleton().Parameters()["training"].wasPassed = false; + CLI::GetSingleton().Parameters()["labels"].wasPassed = false; - arma::mat testX = arma::randu(D - 1, M); // test data with Wrong dimensionality. - SetInputParam("input_model", std::move(model)); + // test data with Wrong dimensionality. + arma::mat testX = arma::randu(D - 1, M); + SetInputParam("input_model", model); SetInputParam("test", std::move(testX)); // test data dimensionality is wrong. Should throw a runtime error. @@ -283,22 +296,23 @@ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData2) /** * Ensuring that training responses contain only two classes (0 or 1) **/ -BOOST_AUTO_TEST_CASE(LRTrainWithMoreThanTwoClasses) +BOOST_AUTO_TEST_CASE(LRTrainWithMoreThanTwoClasses) { constexpr int N = 8; constexpr int D = 2; - arma::mat trainX = arma::randu(D,N); + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 0 << 1 << 0 << 1 << 2 << 1 << 3 << 1 << arma::endr; // 8 responses containing more than two classes + // 8 responses containing more than two classes + trainY << 0 << 1 << 0 << 1 << 2 << 1 << 3 << 1 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - //training data contains more than two classes. Should throw a runtime error + // training data contains more than two classes. Should throw a runtime error Log::Fatal.ignoreInput = true; - BOOST_REQUIRE_THROW(mlpackMain(),std::runtime_error); + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } @@ -310,20 +324,20 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeMaxIterationTest) constexpr int N = 10; constexpr int D = 3; - arma::mat trainX = arma::randu(D,N); + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; // 10 responses + // 10 responses + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("max_iterations", int(-1)); + SetInputParam("max_iterations", int (-1)); // Maximum iterations is negative. Should a runtime error Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; - } /** @@ -334,14 +348,15 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeStepSizeTest) constexpr int N = 10; constexpr int D = 2; - arma::mat trainX = arma::randu(D,N); + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + // 10 responses + trainY << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("optimizer", std::string ("sgd")) ; + SetInputParam("optimizer", std::string ("sgd")); SetInputParam("step_size", double (-0.01)); // step size for optimizer is negative. Should throw a runtime error @@ -353,15 +368,16 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeStepSizeTest) /** * Ensuring that tolerance is non negative **/ -BOOST_AUTO_TEST_CASE(LRNonNegativeToleranceTest) +BOOST_AUTO_TEST_CASE(LRNonNegativeToleranceTest) { constexpr int N = 10; constexpr int D = 3; - arma::mat trainX = arma::randu(D,N); + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 1 << 1 << 0 << 1 << 0 << 0 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + // 10 responses + trainY << 1 << 1 << 0 << 1 << 0 << 0 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); @@ -371,21 +387,21 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeToleranceTest) Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; - } /** * Ensuring changing Maximum number of iterations changes the output model **/ -BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) +BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) { constexpr int N = 10; constexpr int D = 3; - - arma::mat trainX = arma::randu(D,N); + + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + // 10 responses + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", trainX); SetInputParam("labels", trainY); @@ -395,9 +411,12 @@ BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) mlpackMain(); // get the parameters of the output model obtained after first training - const arma::rowvec parameters1 = std::move(CLI::GetParam>("output_model").Parameters()); + const arma::rowvec parameters1 = + std::move(CLI::GetParam*>("output_model") + ->Parameters()); - //reset the settings + // reset the settings + bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); @@ -409,143 +428,165 @@ BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) mlpackMain(); // get the parameters of the output model obtained after second training - const arma::rowvec ¶meters2 = CLI::GetParam>("output_model").Parameters(); + const arma::rowvec ¶meters2 = + CLI::GetParam*>("output_model")->Parameters(); - // Check that the parameters (parameters1 and parameters2) are not equal which ensures Max Iteration changes the output model + // Check that the parameters (parameters1 and parameters2) are not equal + // which ensures Max Iteration changes the output model // arma::all function checks that each element of the vector is equal to zero - BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(Max Iteration) has no effect on the output" ); + BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), + "Parameter(Max Iteration) has no effect on the output"); } /** - * Ensuring that lambda has some effects on the output - **/ -BOOST_AUTO_TEST_CASE(LRLambdaChangeTest) + * Ensuring that lambda has some effects on the output + **/ +BOOST_AUTO_TEST_CASE(LRLambdaChangeTest) { constexpr int N = 10; constexpr int D = 4; - arma::mat trainX = arma::randu(D,N); + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + // 10 responses + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", trainX); SetInputParam("labels", trainY); - SetInputParam("lambda", double(0)); + SetInputParam("lambda", double (0)); // first solution mlpackMain(); // get the parameters of the output model obtained after first training - const arma::rowvec parameters1 = std::move(CLI::GetParam>("output_model").Parameters()); + const arma::rowvec parameters1 = + std::move(CLI::GetParam*>("output_model") + ->Parameters()); - //reset the settings + // reset the settings + bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("lambda", double(1000)); + SetInputParam("lambda", double (1000)); // second solution mlpackMain(); // get the parameters of the output model obtained after second training - const arma::rowvec ¶meters2 = CLI::GetParam>("output_model").Parameters(); + const arma::rowvec ¶meters2 = + CLI::GetParam*>("output_model")->Parameters(); - // Check that the parameters (parameters1 and parameters2) are not equal which ensures lambda changes the output model + // Check that the parameters (parameters1 and parameters2) are not equal + // which ensures lambda changes the output model // arma::all function checks that each element of the vector is equal to zero - BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(lambda) has no effect on the output" ); + BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), + "Parameter(lambda) has no effect on the output"); } /** - * Ensuring that Step size has some effects on the output - **/ -BOOST_AUTO_TEST_CASE(LRStepSizeChangeTest) + * Ensuring that Step size has some effects on the output + **/ +BOOST_AUTO_TEST_CASE(LRStepSizeChangeTest) { constexpr int N = 10; constexpr int D = 3; - arma::mat trainX = arma::randu(D,N); + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + // 10 responses + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", trainX); SetInputParam("labels", trainY); SetInputParam("optimizer", std::string("sgd")); - SetInputParam("step_size", double(0.02)); + SetInputParam("step_size", double (0.02)); // first solution mlpackMain(); // get the parameters of the output model obtained after first training - const arma::rowvec parameters1 = std::move(CLI::GetParam>("output_model").Parameters()); + const arma::rowvec parameters1 = + std::move(CLI::GetParam*>("output_model") + ->Parameters()); - //reset the settings + // reset the settings + bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); SetInputParam("optimizer", std::string("sgd")); - SetInputParam("step_size", double(1.02)); + SetInputParam("step_size", double (1.02)); // second solution mlpackMain(); // get the parameters of the output model obtained after second training - const arma::rowvec ¶meters2 = CLI::GetParam>("output_model").Parameters(); + const arma::rowvec ¶meters2 = + CLI::GetParam*>("output_model")->Parameters(); - // Check that the parameters (parameters1 and parameters2) are not equal which ensures Step Size changes the output model + // Check that the parameters (parameters1 and parameters2) are not equal + // which ensures Step Size changes the output model // arma::all function checks that each element of the vector is equal to zero - BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(Step Size) has no effect on the output" ); + BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), + "Parameter(Step Size) has no effect on the output"); } /** * Ensuring that lbfgs optimizer converges to a different result than sgd **/ -BOOST_AUTO_TEST_CASE(LROptimizerChangeTest) +BOOST_AUTO_TEST_CASE(LROptimizerChangeTest) { constexpr int N = 10; constexpr int D = 3; - arma::mat trainX = arma::randu(D,N); + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + // 10 responses + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", trainX); SetInputParam("labels", trainY); SetInputParam("optimizer", std::string("lbfgs")); - SetInputParam("max_iterations", int(1000)); + SetInputParam("max_iterations", int (1000)); // first solution mlpackMain(); // get the parameters of the output model obtained after first training - const arma::rowvec parameters1 = std::move(CLI::GetParam>("output_model").Parameters()); + const arma::rowvec parameters1 = + std::move(CLI::GetParam*>("output_model") + ->Parameters()); - //reset the settings + // reset the settings + bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("optimizer", std::string("sgd")); - SetInputParam("max_iterations", int(1000)); + SetInputParam("optimizer", std::string("sgd")); + SetInputParam("max_iterations", int (1000)); // second solution mlpackMain(); // get the parameters of the output model obtained after second training - const arma::rowvec ¶meters2 = CLI::GetParam>("output_model").Parameters(); + const arma::rowvec ¶meters2 = + CLI::GetParam*>("output_model")->Parameters(); // Check that the parameters (parameters1 and parameters2) are not equal which // ensures that different optimizer converge to different results // arma::all function checks that each element of the vector is equal to zero - BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(Step Size) has no effect on the output" ); - + BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), + "Parameter(Step Size) has no effect on the output"); } /** @@ -557,16 +598,17 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) constexpr int D = 3; constexpr int M = 15; - arma::mat trainX = arma::randu(D,N); + arma::mat trainX = arma::randu(D, N); arma::Row trainY; - trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; // 10 responses + // 10 responses + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; - arma::mat testX = arma::randu(D,M); + arma::mat testX = arma::randu(D, M); SetInputParam("training", trainX); SetInputParam("labels", trainY); - SetInputParam("decision_boundary", double(1)); + SetInputParam("decision_boundary", double (1)); SetInputParam("test", testX); // first solution @@ -578,15 +620,17 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) // Check that the parameters (parameters1 and parameters2) are not equal which // ensures that decision boundary has some effect on the output // arma::all function checks that each element of the vector is equal to zero - BOOST_REQUIRE_MESSAGE(arma::all(output1 == 0), "Parameter(Decision Boudary) has no effect on the output"); + BOOST_REQUIRE_MESSAGE(arma::all(output1 == 0), + "Parameter(Decision Boudary) has no effect on the output"); - //reset the settings + // reset the settings + bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); SetInputParam("training", trainX); SetInputParam("labels", trainY); - SetInputParam("decision_boundary", double(0)); + SetInputParam("decision_boundary", double (0)); SetInputParam("test", testX); // second solution @@ -598,10 +642,8 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) // Check that the parameters (parameters1 and parameters2) are not equal which // ensures that decision boundary has som effect on the output // arma::all function checks that each element of the vector is equal to one - BOOST_REQUIRE_MESSAGE(arma::all(output2 == 1), "Parameter(Decision Boudary) has no effect on the output"); - + BOOST_REQUIRE_MESSAGE(arma::all(output2 == 1), + "Parameter(Decision Boudary) has no effect on the output"); } -BOOST_AUTO_TEST_SUITE_END(); - - +BOOST_AUTO_TEST_SUITE_END(); \ No newline at end of file From 92f268e28befd131ef614de48446753df7c8bfc9 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Fri, 9 Feb 2018 08:38:00 +0530 Subject: [PATCH 28/34] Style Fix --- .../logistic_regression_main.cpp | 7 +- .../methods/perceptron/perceptron_main.cpp | 4 +- .../main_tests/logistic_regression_test.cpp | 65 ++++++++++--------- .../tests/main_tests/perceptron_test.cpp | 14 ++-- 4 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp index 5981baf9b2..e087b20945 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp @@ -143,7 +143,7 @@ static void mlpackMain() const size_t batchSize = (size_t) CLI::GetParam("batch_size"); const size_t maxIterations = (size_t) CLI::GetParam("max_iterations"); const double decisionBoundary = CLI::GetParam("decision_boundary"); - + // One of training and input_model must be specified. RequireAtLeastOnePassed({ "training", "input_model" }, true); @@ -244,7 +244,7 @@ static void mlpackMain() Log::Fatal << "Can't get responses from training data " "since it has less than 2 rows." << endl; } - + // The initial predictors for y, Nx1. responses = arma::conv_to>::from( regressors.row(regressors.n_rows - 1)); @@ -293,7 +293,8 @@ static void mlpackMain() if (testSet.n_rows != model->Parameters().n_cols - 1) { Log::Fatal << "Test data dimensionality (" << testSet.n_rows << ") must " - << "be the same as the dimensionality of the Training Data (" << model->Parameters().n_cols-1 << ")!" << endl; + << "be the same as the dimensionality of the Training Data (" + << model->Parameters().n_cols-1 << ")!" << endl; } // We must perform predictions on the test set. Training (and the diff --git a/src/mlpack/methods/perceptron/perceptron_main.cpp b/src/mlpack/methods/perceptron/perceptron_main.cpp index e821a3d4ca..49a57518b2 100644 --- a/src/mlpack/methods/perceptron/perceptron_main.cpp +++ b/src/mlpack/methods/perceptron/perceptron_main.cpp @@ -179,7 +179,7 @@ static void mlpackMain() labelsIn = std::move(CLI::GetParam>("labels")); // Checking the size of the responses and training data - if (labelsIn.n_cols != trainingData.n_cols) + if (labelsIn.n_cols != trainingData.n_cols) { Log::Fatal << "The responses must have the same number of columns " "as the training set." << endl; @@ -187,7 +187,7 @@ static void mlpackMain() } else { - // Checking the size of training data if no labels are passed + // Checking the size of training data if no labels are passed if (trainingData.n_rows < 2) { Log::Fatal << "Can't get responses from training data " diff --git a/src/mlpack/tests/main_tests/logistic_regression_test.cpp b/src/mlpack/tests/main_tests/logistic_regression_test.cpp index 5f94debfba..34e090cb89 100644 --- a/src/mlpack/tests/main_tests/logistic_regression_test.cpp +++ b/src/mlpack/tests/main_tests/logistic_regression_test.cpp @@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE(LRPridictionSizeCheck) arma::mat trainX = arma::randu(D, N); arma::Row trainY; // 10 responses - trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; + trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; arma::mat testX = arma::randu(D, M); SetInputParam("training", std::move(trainX)); @@ -119,7 +119,7 @@ BOOST_AUTO_TEST_CASE(LRWrongResponseSizeTest) arma::Row trainY; // response vector with wrong size // 8 responses - incorrect size - trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << arma::endr; + trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); @@ -146,7 +146,7 @@ BOOST_AUTO_TEST_CASE(LRResponsesRepresentationTest) mlpackMain(); // get the output - const arma::Row testY1 = + const arma::Row testY1 = std::move(CLI::GetParam>("output")); // reset the settings @@ -319,7 +319,7 @@ BOOST_AUTO_TEST_CASE(LRTrainWithMoreThanTwoClasses) /** * Ensuring that max iteration for optimizers is non negative **/ -BOOST_AUTO_TEST_CASE(LRNonNegativeMaxIterationTest) +BOOST_AUTO_TEST_CASE(LRNonNegativeMaxIterationTest) { constexpr int N = 10; constexpr int D = 3; @@ -332,7 +332,7 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeMaxIterationTest) SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("max_iterations", int (-1)); + SetInputParam("max_iterations", int(-1)); // Maximum iterations is negative. Should a runtime error Log::Fatal.ignoreInput = true; @@ -356,8 +356,8 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeStepSizeTest) SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("optimizer", std::string ("sgd")); - SetInputParam("step_size", double (-0.01)); + SetInputParam("optimizer", std::string("sgd")); + SetInputParam("step_size", double(-0.01)); // step size for optimizer is negative. Should throw a runtime error Log::Fatal.ignoreInput = true; @@ -381,7 +381,7 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeToleranceTest) SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("tolerance", double (-0.01)); + SetInputParam("tolerance", double(-0.01)); // tolerance is negative. Should throw a runtime error Log::Fatal.ignoreInput = true; @@ -405,7 +405,7 @@ BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) SetInputParam("training", trainX); SetInputParam("labels", trainY); - SetInputParam("max_iterations", int (1)); + SetInputParam("max_iterations", int(1)); // first solution mlpackMain(); @@ -422,7 +422,7 @@ BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("max_iterations", int (100)); + SetInputParam("max_iterations", int(100)); // second solution mlpackMain(); @@ -431,7 +431,7 @@ BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) const arma::rowvec ¶meters2 = CLI::GetParam*>("output_model")->Parameters(); - // Check that the parameters (parameters1 and parameters2) are not equal + // Check that the parameters (parameters1 and parameters2) are not equal // which ensures Max Iteration changes the output model // arma::all function checks that each element of the vector is equal to zero BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), @@ -454,13 +454,13 @@ BOOST_AUTO_TEST_CASE(LRLambdaChangeTest) SetInputParam("training", trainX); SetInputParam("labels", trainY); - SetInputParam("lambda", double (0)); + SetInputParam("lambda", double(0)); // first solution mlpackMain(); // get the parameters of the output model obtained after first training - const arma::rowvec parameters1 = + const arma::rowvec parameters1 = std::move(CLI::GetParam*>("output_model") ->Parameters()); @@ -471,7 +471,7 @@ BOOST_AUTO_TEST_CASE(LRLambdaChangeTest) SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - SetInputParam("lambda", double (1000)); + SetInputParam("lambda", double(1000)); // second solution mlpackMain(); @@ -504,7 +504,7 @@ BOOST_AUTO_TEST_CASE(LRStepSizeChangeTest) SetInputParam("training", trainX); SetInputParam("labels", trainY); SetInputParam("optimizer", std::string("sgd")); - SetInputParam("step_size", double (0.02)); + SetInputParam("step_size", double(0.02)); // first solution mlpackMain(); @@ -522,19 +522,19 @@ BOOST_AUTO_TEST_CASE(LRStepSizeChangeTest) SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); SetInputParam("optimizer", std::string("sgd")); - SetInputParam("step_size", double (1.02)); + SetInputParam("step_size", double(1.02)); // second solution mlpackMain(); // get the parameters of the output model obtained after second training const arma::rowvec ¶meters2 = - CLI::GetParam*>("output_model")->Parameters(); + CLI::GetParam*>("output_model")->Parameters(); // Check that the parameters (parameters1 and parameters2) are not equal // which ensures Step Size changes the output model // arma::all function checks that each element of the vector is equal to zero - BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), + BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(Step Size) has no effect on the output"); } @@ -555,9 +555,9 @@ BOOST_AUTO_TEST_CASE(LROptimizerChangeTest) SetInputParam("training", trainX); SetInputParam("labels", trainY); SetInputParam("optimizer", std::string("lbfgs")); - SetInputParam("max_iterations", int (1000)); + SetInputParam("max_iterations", int(1000)); - // first solution + // first solution mlpackMain(); // get the parameters of the output model obtained after first training @@ -569,24 +569,24 @@ BOOST_AUTO_TEST_CASE(LROptimizerChangeTest) bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); - + SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); SetInputParam("optimizer", std::string("sgd")); - SetInputParam("max_iterations", int (1000)); + SetInputParam("max_iterations", int(1000)); // second solution mlpackMain(); // get the parameters of the output model obtained after second training - const arma::rowvec ¶meters2 = - CLI::GetParam*>("output_model")->Parameters(); + const arma::rowvec ¶meters2 = + CLI::GetParam*>("output_model")->Parameters(); // Check that the parameters (parameters1 and parameters2) are not equal which // ensures that different optimizer converge to different results // arma::all function checks that each element of the vector is equal to zero BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), - "Parameter(Step Size) has no effect on the output"); + "Parameter(Step Size) has no effect on the output"); } /** @@ -602,13 +602,13 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) arma::Row trainY; // 10 responses - trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; + trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; arma::mat testX = arma::randu(D, M); SetInputParam("training", trainX); SetInputParam("labels", trainY); - SetInputParam("decision_boundary", double (1)); + SetInputParam("decision_boundary", double(1)); SetInputParam("test", testX); // first solution @@ -621,7 +621,8 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) // ensures that decision boundary has some effect on the output // arma::all function checks that each element of the vector is equal to zero BOOST_REQUIRE_MESSAGE(arma::all(output1 == 0), - "Parameter(Decision Boudary) has no effect on the output"); + "Parameter(Decision Boudary) has + no effect on the output"); // reset the settings bindings::tests::CleanMemory(); @@ -630,7 +631,7 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) SetInputParam("training", trainX); SetInputParam("labels", trainY); - SetInputParam("decision_boundary", double (0)); + SetInputParam("decision_boundary", double(0)); SetInputParam("test", testX); // second solution @@ -642,8 +643,8 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) // Check that the parameters (parameters1 and parameters2) are not equal which // ensures that decision boundary has som effect on the output // arma::all function checks that each element of the vector is equal to one - BOOST_REQUIRE_MESSAGE(arma::all(output2 == 1), - "Parameter(Decision Boudary) has no effect on the output"); + BOOST_REQUIRE_MESSAGE(arma::all(output2 == 1), + "Parameter(Decision Boudary) has + no effect on the output"); } - BOOST_AUTO_TEST_SUITE_END(); \ No newline at end of file diff --git a/src/mlpack/tests/main_tests/perceptron_test.cpp b/src/mlpack/tests/main_tests/perceptron_test.cpp index aa2b9db067..56ed19d810 100644 --- a/src/mlpack/tests/main_tests/perceptron_test.cpp +++ b/src/mlpack/tests/main_tests/perceptron_test.cpp @@ -41,7 +41,7 @@ struct PerceptronTestFixture }; // reset the parameters -void resetSettings() +void resetSettings() { bindings::tests::CleanMemory(); CLI::ClearSettings(); @@ -263,7 +263,7 @@ BOOST_AUTO_TEST_CASE(PerceptronReTrainWithWrongClasses) // labels for the train data SetInputParam("labels", std::move(labelsX1)); - //training model using first training dataset + // training model using first training dataset mlpackMain(); // get the output model obtained after training @@ -283,15 +283,15 @@ BOOST_AUTO_TEST_CASE(PerceptronReTrainWithWrongClasses) // 10 responses labelsX2 << 0 << 1 << 4 << 1 << 2 << 1 << 0 << 3 << 3 << 0 << endr; - //last column of trainX2 contains the class labels + // last column of trainX2 contains the class labels SetInputParam("training", std::move(trainX2)); SetInputParam("input_model", model); - // re-training an existing model of 3 classes + // re-training an existing model of 3 classes // with training data of 5 classes. Should give runtime error - Log::Fatal.ignoreInput=true; + Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); - Log::Fatal.ignoreInput=false; + Log::Fatal.ignoreInput = false; } /** @@ -334,7 +334,7 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongResponseSizeTest) arma::Row trainY; // response vector with wrong size // 8 responses - trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << endr; + trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); From 45724c363c99d8e6b29dac501f1d267e1bcc9b39 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Fri, 9 Feb 2018 08:41:06 +0530 Subject: [PATCH 29/34] Style Fix --- src/mlpack/tests/main_tests/logistic_regression_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/tests/main_tests/logistic_regression_test.cpp b/src/mlpack/tests/main_tests/logistic_regression_test.cpp index 34e090cb89..e7a75810bb 100644 --- a/src/mlpack/tests/main_tests/logistic_regression_test.cpp +++ b/src/mlpack/tests/main_tests/logistic_regression_test.cpp @@ -647,4 +647,5 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) "Parameter(Decision Boudary) has no effect on the output"); } -BOOST_AUTO_TEST_SUITE_END(); \ No newline at end of file + +BOOST_AUTO_TEST_SUITE_END(); From 9d1553f846621daacac2abb3c307a18013749983 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Fri, 9 Feb 2018 08:58:59 +0530 Subject: [PATCH 30/34] resolve error --- src/mlpack/tests/main_tests/logistic_regression_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mlpack/tests/main_tests/logistic_regression_test.cpp b/src/mlpack/tests/main_tests/logistic_regression_test.cpp index e7a75810bb..d2c2195345 100644 --- a/src/mlpack/tests/main_tests/logistic_regression_test.cpp +++ b/src/mlpack/tests/main_tests/logistic_regression_test.cpp @@ -621,8 +621,8 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) // ensures that decision boundary has some effect on the output // arma::all function checks that each element of the vector is equal to zero BOOST_REQUIRE_MESSAGE(arma::all(output1 == 0), - "Parameter(Decision Boudary) has - no effect on the output"); + "Parameter(Decision Boudary) has" + "no effect on the output"); // reset the settings bindings::tests::CleanMemory(); @@ -644,8 +644,8 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) // ensures that decision boundary has som effect on the output // arma::all function checks that each element of the vector is equal to one BOOST_REQUIRE_MESSAGE(arma::all(output2 == 1), - "Parameter(Decision Boudary) has - no effect on the output"); + "Parameter(Decision Boudary) has" + "no effect on the output"); } BOOST_AUTO_TEST_SUITE_END(); From 28c358651c5bdb733de99591f1a6609c44f51e7e Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 10 Feb 2018 00:08:16 +0530 Subject: [PATCH 31/34] Minor Changes --- src/mlpack/tests/main_tests/perceptron_test.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/mlpack/tests/main_tests/perceptron_test.cpp b/src/mlpack/tests/main_tests/perceptron_test.cpp index 56ed19d810..c09bb12f37 100644 --- a/src/mlpack/tests/main_tests/perceptron_test.cpp +++ b/src/mlpack/tests/main_tests/perceptron_test.cpp @@ -40,14 +40,6 @@ struct PerceptronTestFixture } }; -// reset the parameters -void resetSettings() -{ - bindings::tests::CleanMemory(); - CLI::ClearSettings(); - CLI::RestoreSettings(testName); -} - BOOST_FIXTURE_TEST_SUITE(PerceptronMainTest, PerceptronTestFixture); @@ -146,7 +138,9 @@ BOOST_AUTO_TEST_CASE(PerceptronLabelsLessDimensionTest) arma::Row output; output = std::move(CLI::GetParam>("output")); - // Now train pereptron with labels provided. + bindings::tests::CleanMemory(); + + // Now train perceptron with labels provided. // Input training data. SetInputParam("training", std::move(inputData)); @@ -418,6 +412,4 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData2) Log::Fatal.ignoreInput = false; } -BOOST_AUTO_TEST_SUITE_END(); - - +BOOST_AUTO_TEST_SUITE_END(); \ No newline at end of file From ee5d303ea61746d9c249528b6524dbcfb71385f2 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 10 Feb 2018 00:09:45 +0530 Subject: [PATCH 32/34] removing unwanted files --- .../tests/data/train_data_5_classes.csv | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 src/mlpack/tests/data/train_data_5_classes.csv diff --git a/src/mlpack/tests/data/train_data_5_classes.csv b/src/mlpack/tests/data/train_data_5_classes.csv deleted file mode 100644 index 8d9dd6106e..0000000000 --- a/src/mlpack/tests/data/train_data_5_classes.csv +++ /dev/null @@ -1,63 +0,0 @@ -4.7,3.2,1.6,0.2,0 -4.8,3.1,1.6,0.2,0 -5.4,3.4,1.5,0.4,1 -5.2,4.1,1.5,0.1,0 -5.5,4.2,1.4,0.2,0 -4.9,3.1,1.5,0.1,2 -5,3.2,1.2,0.2,0 -5.5,3.5,1.3,0.2,0 -4.9,3.1,1.5,0.1,2 -4.4,3,1.3,0.2,0 -5.1,3.4,1.5,0.2,0 -5,3.5,1.3,0.3,3 -4.5,2.3,1.3,0.3,0 -4.4,3.2,1.3,0.2,0 -5,3.5,1.6,0.6,0 -5.1,3.8,1.9,0.4,0 -4.8,3,1.4,0.3,4 -5.1,3.8,1.6,0.2,0 -4.6,3.2,1.4,0.2,4 -5.3,3.7,1.5,0.2,1 -5,3.3,1.4,0.2,1 -5.7,2.6,3.5,1,1 -5.5,2.4,3.8,1.1,1 -5.5,2.4,3.7,1,1 -5.8,2.7,3.9,1.2,0 -6,2.7,5.1,1.6,1 -5.4,3,4.5,1.5,0 -6,3.4,4.5,1.6,1 -6.7,3.1,4.7,1.5,4 -6.3,2.3,4.4,1.3,1 -5.6,3,4.1,1.3,1 -5.5,2.5,4,1.3,2 -5.5,2.6,4.4,1.2,1 -6.1,3,4.6,1.4,1 -5.8,2.6,4,1.2,3 -5,2.3,3.3,1,3 -5.6,2.7,4.2,1.3,3 -5.7,3,4.2,1.2,1 -5.7,2.9,4.2,1.3,1 -6.2,2.9,4.3,1.3,4 -5.1,2.5,3,1.1,1 -5.7,2.8,4.1,1.3,1 -7.2,3,5.8,1.6,2 -7.4,2.8,6.1,1.9,1 -7.9,3.8,6.4,2,2 -6.4,2.8,5.6,2.2,0 -6.3,2.8,5.1,1.5,3 -6.1,2.6,5.6,1.4,0 -7.7,3,6.1,2.3,2 -6.3,3.4,5.6,2.4,2 -6.4,3.1,5.5,1.8,0 -6,3,4.8,1.8,0 -6.9,3.1,5.4,2.1,2 -6.7,3.1,5.6,2.4,2 -6.9,3.1,5.1,2.3,4 -5.8,2.7,5.1,1.9,4 -6.8,3.2,5.9,2.3,4 -6.7,3.3,5.7,2.5,3 -6.7,3,5.2,2.3,2 -6.3,2.5,5,1.9,2 -6.5,3,5.2,2,1 -6.2,3.4,5.4,2.3,2 -5.9,3,5.1,1.8,2 From 7521a99492daa91b31a5a0c92062c4460b0d5ee6 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 10 Feb 2018 00:35:43 +0530 Subject: [PATCH 33/34] Checking comments --- .../logistic_regression_main.cpp | 4 +- .../methods/perceptron/perceptron_main.cpp | 4 +- .../main_tests/logistic_regression_test.cpp | 181 +++++++++--------- .../tests/main_tests/perceptron_test.cpp | 60 +++--- 4 files changed, 124 insertions(+), 125 deletions(-) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp index e087b20945..9582f4c08d 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_main.cpp @@ -238,7 +238,7 @@ static void mlpackMain() } else if (CLI::HasParam("training")) { - // Checking the size of training data if no labels are passed + // Checking the size of training data if no labels are passed. if (regressors.n_rows < 2) { Log::Fatal << "Can't get responses from training data " @@ -289,7 +289,7 @@ static void mlpackMain() { testSet = std::move(CLI::GetParam("test")); - // checking the dimensionality of the test data + // Checking the dimensionality of the test data. if (testSet.n_rows != model->Parameters().n_cols - 1) { Log::Fatal << "Test data dimensionality (" << testSet.n_rows << ") must " diff --git a/src/mlpack/methods/perceptron/perceptron_main.cpp b/src/mlpack/methods/perceptron/perceptron_main.cpp index 49a57518b2..d35e44aeeb 100644 --- a/src/mlpack/methods/perceptron/perceptron_main.cpp +++ b/src/mlpack/methods/perceptron/perceptron_main.cpp @@ -178,7 +178,7 @@ static void mlpackMain() { labelsIn = std::move(CLI::GetParam>("labels")); - // Checking the size of the responses and training data + // Checking the size of the responses and training data. if (labelsIn.n_cols != trainingData.n_cols) { Log::Fatal << "The responses must have the same number of columns " @@ -187,7 +187,7 @@ static void mlpackMain() } else { - // Checking the size of training data if no labels are passed + // Checking the size of training data if no labels are passed. if (trainingData.n_rows < 2) { Log::Fatal << "Can't get responses from training data " diff --git a/src/mlpack/tests/main_tests/logistic_regression_test.cpp b/src/mlpack/tests/main_tests/logistic_regression_test.cpp index d2c2195345..23687b5356 100644 --- a/src/mlpack/tests/main_tests/logistic_regression_test.cpp +++ b/src/mlpack/tests/main_tests/logistic_regression_test.cpp @@ -49,12 +49,12 @@ BOOST_FIXTURE_TEST_SUITE(LogisticRegressionMainTest, BOOST_AUTO_TEST_CASE(LRNoTrainingData) { arma::Row trainY; - // 10 responses + // 10 responses. trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; SetInputParam("labels", std::move(trainY)); - // training data is not provided. Should throw a runtime error + // Training data is not provided. Should throw a runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -71,7 +71,7 @@ BOOST_AUTO_TEST_CASE(LRNoResponses) arma::mat trainX = arma::randu(D, N); SetInputParam("training", std::move(trainX)); - // labels to the training data is not provided should throw a runtime error + // Labels to the training data is not provided. It should throw a runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -88,7 +88,7 @@ BOOST_AUTO_TEST_CASE(LRPridictionSizeCheck) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; arma::mat testX = arma::randu(D, M); @@ -96,19 +96,19 @@ BOOST_AUTO_TEST_CASE(LRPridictionSizeCheck) SetInputParam("labels", std::move(trainY)); SetInputParam("test", std::move(testX)); - // training the model + // Training the model. mlpackMain(); - // get the output predictions of the test data + // Get the output predictions of the test data. const arma::Row &testY = CLI::GetParam>("output"); - // output predictions size must match the test data set size + // Output predictions size must match the test data set size. BOOST_REQUIRE_EQUAL(testY.n_rows, 1); BOOST_REQUIRE_EQUAL(testY.n_cols, M); } /** - * Ensuring that the response size is checked + * Ensuring that the response size is checked. **/ BOOST_AUTO_TEST_CASE(LRWrongResponseSizeTest) { @@ -116,15 +116,15 @@ BOOST_AUTO_TEST_CASE(LRWrongResponseSizeTest) constexpr int N = 10; arma::mat trainX = arma::randu(D, N); - arma::Row trainY; // response vector with wrong size + arma::Row trainY; // Response vector with wrong size. - // 8 responses - incorrect size + // 8 responses - incorrect size. trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - // Labels with incorrect size. Should throw a runtime error + // Labels with incorrect size. It should throw a runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -145,17 +145,16 @@ BOOST_AUTO_TEST_CASE(LRResponsesRepresentationTest) // The first solution. mlpackMain(); - // get the output + // Get the output. const arma::Row testY1 = std::move(CLI::GetParam>("output")); - // reset the settings + // Reset the settings. bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); - // now train by providing labels as extra parameter - + // Now train by providing labels as extra parameter. arma::mat trainX2({{1.0, 2.0, 3.0}, {1.0, 4.0, 9.0}}); arma::Row trainY2({0, 1, 1}); @@ -170,7 +169,7 @@ BOOST_AUTO_TEST_CASE(LRResponsesRepresentationTest) const arma::Row &testY2 = CLI::GetParam>("output"); - // both solutions should be equal. + // Both solutions should be equal. BOOST_REQUIRE_EQUAL_COLLECTIONS(testY1.begin(), testY1.end(), testY2.begin(), testY2.end()); } @@ -188,7 +187,7 @@ BOOST_AUTO_TEST_CASE(LRModelReload) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; arma::mat testX = arma::randu(D, M); @@ -197,17 +196,17 @@ BOOST_AUTO_TEST_CASE(LRModelReload) SetInputParam("labels", std::move(trainY)); SetInputParam("test", testX); - // first solution + // First solution mlpackMain(); - // get the output model obtained from training + // Get the output model obtained from training. LogisticRegression<>* model = CLI::GetParam*>("output_model"); - // get the output + // Get the output. const arma::Row testY1 = std::move(CLI::GetParam>("output")); - // reset the data passed + // Reset the data passed. CLI::GetSingleton().Parameters()["training"].wasPassed = false; CLI::GetSingleton().Parameters()["labels"].wasPassed = false; CLI::GetSingleton().Parameters()["test"].wasPassed = false; @@ -215,10 +214,10 @@ BOOST_AUTO_TEST_CASE(LRModelReload) SetInputParam("input_model", model); SetInputParam("test", std::move(testX)); - // second solution + // Second solution. mlpackMain(); - // get the output + // Get the output. const arma::Row &testY2 = CLI::GetParam>("output"); @@ -228,7 +227,7 @@ BOOST_AUTO_TEST_CASE(LRModelReload) } /** - * Checking for dimensionality of the test data set + * Checking for dimensionality of the test data set. **/ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData) { @@ -238,17 +237,17 @@ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; - // test data with wrong dimensionality + // Test data with wrong dimensionality. arma::mat testX = arma::randu(D-1, N); SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); SetInputParam("test", std::move(testX)); - // Dimensionality of test data is wrong. Should throw a runtime error + // Dimensionality of test data is wrong. It should throw a runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -271,30 +270,30 @@ BOOST_AUTO_TEST_CASE(LRWrongDimOfTestData2) SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - // training the model + // Training the model. mlpackMain(); - // get the output model obtained from training + // Get the output model obtained from training. LogisticRegression<>* model = CLI::GetParam*>("output_model"); - // reset the data passed + // Reset the data passed. CLI::GetSingleton().Parameters()["training"].wasPassed = false; CLI::GetSingleton().Parameters()["labels"].wasPassed = false; - // test data with Wrong dimensionality. + // Test data with Wrong dimensionality. arma::mat testX = arma::randu(D - 1, M); SetInputParam("input_model", model); SetInputParam("test", std::move(testX)); - // test data dimensionality is wrong. Should throw a runtime error. + // Test data dimensionality is wrong. It should throw a runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } /** - * Ensuring that training responses contain only two classes (0 or 1) + * Ensuring that training responses contain only two classes (0 or 1). **/ BOOST_AUTO_TEST_CASE(LRTrainWithMoreThanTwoClasses) { @@ -304,20 +303,20 @@ BOOST_AUTO_TEST_CASE(LRTrainWithMoreThanTwoClasses) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 8 responses containing more than two classes + // 8 responses containing more than two classes. trainY << 0 << 1 << 0 << 1 << 2 << 1 << 3 << 1 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - // training data contains more than two classes. Should throw a runtime error + // Training data contains more than two classes. It should throw a runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } /** - * Ensuring that max iteration for optimizers is non negative + * Ensuring that max iteration for optimizers is non negative. **/ BOOST_AUTO_TEST_CASE(LRNonNegativeMaxIterationTest) { @@ -327,21 +326,21 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeMaxIterationTest) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); SetInputParam("max_iterations", int(-1)); - // Maximum iterations is negative. Should a runtime error + // Maximum iterations is negative. It should a runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } /** - * Ensuring that step size for optimizer is non negative + * Ensuring that step size for optimizer is non negative. **/ BOOST_AUTO_TEST_CASE(LRNonNegativeStepSizeTest) { @@ -351,7 +350,7 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeStepSizeTest) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", std::move(trainX)); @@ -359,14 +358,14 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeStepSizeTest) SetInputParam("optimizer", std::string("sgd")); SetInputParam("step_size", double(-0.01)); - // step size for optimizer is negative. Should throw a runtime error + // Step size for optimizer is negative. It should throw a runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } /** - * Ensuring that tolerance is non negative + * Ensuring that tolerance is non negative. **/ BOOST_AUTO_TEST_CASE(LRNonNegativeToleranceTest) { @@ -376,21 +375,21 @@ BOOST_AUTO_TEST_CASE(LRNonNegativeToleranceTest) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 1 << 1 << 0 << 1 << 0 << 0 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); SetInputParam("tolerance", double(-0.01)); - // tolerance is negative. Should throw a runtime error + // Tolerance is negative. It should throw a runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } /** - * Ensuring changing Maximum number of iterations changes the output model + * Ensuring changing Maximum number of iterations changes the output model. **/ BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) { @@ -400,22 +399,22 @@ BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", trainX); SetInputParam("labels", trainY); SetInputParam("max_iterations", int(1)); - // first solution + // First solution. mlpackMain(); - // get the parameters of the output model obtained after first training + // Get the parameters of the output model obtained after first training. const arma::rowvec parameters1 = std::move(CLI::GetParam*>("output_model") ->Parameters()); - // reset the settings + // Reset the settings. bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); @@ -424,22 +423,22 @@ BOOST_AUTO_TEST_CASE(LRMaxIterationsChangeTest) SetInputParam("labels", std::move(trainY)); SetInputParam("max_iterations", int(100)); - // second solution + // Second solution. mlpackMain(); - // get the parameters of the output model obtained after second training + // Get the parameters of the output model obtained after second training. const arma::rowvec ¶meters2 = CLI::GetParam*>("output_model")->Parameters(); // Check that the parameters (parameters1 and parameters2) are not equal - // which ensures Max Iteration changes the output model - // arma::all function checks that each element of the vector is equal to zero + // which ensures Max Iteration changes the output model. + // arma::all function checks that each element of the vector is equal to zero. BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(Max Iteration) has no effect on the output"); } /** - * Ensuring that lambda has some effects on the output + * Ensuring that lambda has some effects on the output. **/ BOOST_AUTO_TEST_CASE(LRLambdaChangeTest) { @@ -449,22 +448,22 @@ BOOST_AUTO_TEST_CASE(LRLambdaChangeTest) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", trainX); SetInputParam("labels", trainY); SetInputParam("lambda", double(0)); - // first solution + // First solution. mlpackMain(); - // get the parameters of the output model obtained after first training + // Get the parameters of the output model obtained after first training. const arma::rowvec parameters1 = std::move(CLI::GetParam*>("output_model") ->Parameters()); - // reset the settings + // Reset the settings. bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); @@ -473,22 +472,22 @@ BOOST_AUTO_TEST_CASE(LRLambdaChangeTest) SetInputParam("labels", std::move(trainY)); SetInputParam("lambda", double(1000)); - // second solution + // Second solution. mlpackMain(); - // get the parameters of the output model obtained after second training + // Get the parameters of the output model obtained after second training. const arma::rowvec ¶meters2 = CLI::GetParam*>("output_model")->Parameters(); // Check that the parameters (parameters1 and parameters2) are not equal - // which ensures lambda changes the output model - // arma::all function checks that each element of the vector is equal to zero + // which ensures lambda changes the output model. + // arma::all function checks that each element of the vector is equal to zero. BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(lambda) has no effect on the output"); } /** - * Ensuring that Step size has some effects on the output + * Ensuring that Step size has some effects on the output. **/ BOOST_AUTO_TEST_CASE(LRStepSizeChangeTest) { @@ -498,7 +497,7 @@ BOOST_AUTO_TEST_CASE(LRStepSizeChangeTest) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", trainX); @@ -506,15 +505,15 @@ BOOST_AUTO_TEST_CASE(LRStepSizeChangeTest) SetInputParam("optimizer", std::string("sgd")); SetInputParam("step_size", double(0.02)); - // first solution + // First solution. mlpackMain(); - // get the parameters of the output model obtained after first training + // Get the parameters of the output model obtained after first training. const arma::rowvec parameters1 = std::move(CLI::GetParam*>("output_model") ->Parameters()); - // reset the settings + // Reset the settings. bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); @@ -524,22 +523,22 @@ BOOST_AUTO_TEST_CASE(LRStepSizeChangeTest) SetInputParam("optimizer", std::string("sgd")); SetInputParam("step_size", double(1.02)); - // second solution + // Second solution. mlpackMain(); - // get the parameters of the output model obtained after second training + // Get the parameters of the output model obtained after second training. const arma::rowvec ¶meters2 = CLI::GetParam*>("output_model")->Parameters(); // Check that the parameters (parameters1 and parameters2) are not equal - // which ensures Step Size changes the output model - // arma::all function checks that each element of the vector is equal to zero + // which ensures Step Size changes the output model. + // arma::all function checks that each element of the vector is equal to zero. BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(Step Size) has no effect on the output"); } /** - * Ensuring that lbfgs optimizer converges to a different result than sgd + * Ensuring that lbfgs optimizer converges to a different result than sgd. **/ BOOST_AUTO_TEST_CASE(LROptimizerChangeTest) { @@ -549,7 +548,7 @@ BOOST_AUTO_TEST_CASE(LROptimizerChangeTest) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; SetInputParam("training", trainX); @@ -557,15 +556,15 @@ BOOST_AUTO_TEST_CASE(LROptimizerChangeTest) SetInputParam("optimizer", std::string("lbfgs")); SetInputParam("max_iterations", int(1000)); - // first solution + // First solution. mlpackMain(); - // get the parameters of the output model obtained after first training + // Get the parameters of the output model obtained after first training. const arma::rowvec parameters1 = std::move(CLI::GetParam*>("output_model") ->Parameters()); - // reset the settings + // Reset the settings. bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); @@ -575,22 +574,22 @@ BOOST_AUTO_TEST_CASE(LROptimizerChangeTest) SetInputParam("optimizer", std::string("sgd")); SetInputParam("max_iterations", int(1000)); - // second solution + // Second solution. mlpackMain(); - // get the parameters of the output model obtained after second training + // Get the parameters of the output model obtained after second training. const arma::rowvec ¶meters2 = CLI::GetParam*>("output_model")->Parameters(); // Check that the parameters (parameters1 and parameters2) are not equal which - // ensures that different optimizer converge to different results - // arma::all function checks that each element of the vector is equal to zero + // ensures that different optimizer converge to different results. + // arma::all function checks that each element of the vector is equal to zero. BOOST_REQUIRE_MESSAGE(!arma::all((parameters1-parameters2) == 0), "Parameter(Step Size) has no effect on the output"); } /** - * Ensuring decision_boundary parameter does something + * Ensuring decision_boundary parameter does something. **/ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) { @@ -601,7 +600,7 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 1 << 0 << 0 << 1 << 0 << 1 << 0 << 1 << 0 << 1 << arma::endr; arma::mat testX = arma::randu(D, M); @@ -611,20 +610,20 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) SetInputParam("decision_boundary", double(1)); SetInputParam("test", testX); - // first solution + // First solution. mlpackMain(); - // get the output after first training + // Get the output after first training. const arma::Row &output1 = CLI::GetParam>("output"); // Check that the parameters (parameters1 and parameters2) are not equal which - // ensures that decision boundary has some effect on the output - // arma::all function checks that each element of the vector is equal to zero + // ensures that decision boundary has some effect on the output. + // arma::all function checks that each element of the vector is equal to zero. BOOST_REQUIRE_MESSAGE(arma::all(output1 == 0), "Parameter(Decision Boudary) has" "no effect on the output"); - // reset the settings + // Reset the settings. bindings::tests::CleanMemory(); CLI::ClearSettings(); CLI::RestoreSettings(testName); @@ -634,15 +633,15 @@ BOOST_AUTO_TEST_CASE(LRDecisionBoundaryTest) SetInputParam("decision_boundary", double(0)); SetInputParam("test", testX); - // second solution + // Second solution. mlpackMain(); - // get the output after second training + // Get the output after second training. const arma::Row &output2 = CLI::GetParam>("output"); // Check that the parameters (parameters1 and parameters2) are not equal which - // ensures that decision boundary has som effect on the output - // arma::all function checks that each element of the vector is equal to one + // ensures that decision boundary has som effect on the output. + // arma::all function checks that each element of the vector is equal to one. BOOST_REQUIRE_MESSAGE(arma::all(output2 == 1), "Parameter(Decision Boudary) has" "no effect on the output"); diff --git a/src/mlpack/tests/main_tests/perceptron_test.cpp b/src/mlpack/tests/main_tests/perceptron_test.cpp index c09bb12f37..c096088bb0 100644 --- a/src/mlpack/tests/main_tests/perceptron_test.cpp +++ b/src/mlpack/tests/main_tests/perceptron_test.cpp @@ -234,62 +234,62 @@ BOOST_AUTO_TEST_CASE(PerceptronMaxItrTest) /** * Ensuring that re-training of an existing model - * with different of classes is checked + * with different of classes is checked. **/ BOOST_AUTO_TEST_CASE(PerceptronReTrainWithWrongClasses) { arma::mat trainX1; arma::Row labelsX1; - // loading a train data set with 3 classes + // Loading a train data set with 3 classes. if (!data::Load("vc2.csv", trainX1)) { BOOST_FAIL("Could not load the train data (vc2.csv)"); } - // loading the corresponding labels to the dataset + // Loading the corresponding labels to the dataset. if (!data::Load("vc2_labels.txt", labelsX1)) { BOOST_FAIL("Could not load the train data (vc2_labels.csv)"); } - SetInputParam("training", std::move(trainX1)); // train data - // labels for the train data + SetInputParam("training", std::move(trainX1)); // Training data. + // Labels for the training data. SetInputParam("labels", std::move(labelsX1)); - // training model using first training dataset + // Training model using first training dataset. mlpackMain(); - // get the output model obtained after training + // Get the output model obtained after training. PerceptronModel* model = CLI::GetParam("output_model"); - // reset the data passed + // Reset the data passed. CLI::GetSingleton().Parameters()["training"].wasPassed = false; CLI::GetSingleton().Parameters()["labels"].wasPassed = false; - // creating training data with five classes + // Creating training data with five classes. constexpr int D = 3; constexpr int N = 10; arma::mat trainX2 = arma::randu(D, N); arma::Row labelsX2; - // 10 responses + // 10 responses. labelsX2 << 0 << 1 << 4 << 1 << 2 << 1 << 0 << 3 << 3 << 0 << endr; - // last column of trainX2 contains the class labels + // Last column of trainX2 contains the class labels. SetInputParam("training", std::move(trainX2)); SetInputParam("input_model", model); - // re-training an existing model of 3 classes - // with training data of 5 classes. Should give runtime error + // Re-training an existing model of 3 classes + // with training data of 5 classes. It should give runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } /** - * Checking for dimensionality of the test data set + * Checking for dimensionality of the test data set. **/ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData) { @@ -300,24 +300,24 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; - // test data with wrong dimensionality + // Test data with wrong dimensionality. arma::mat testX = arma::randu(D-3, M); SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); SetInputParam("test", std::move(testX)); - // test data set with wrong dimensionality. Should give runtime error + // Test data set with wrong dimensionality. It should give runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } /** - * Ensuring that the response size is checked + * Ensuring that the response size is checked. **/ BOOST_AUTO_TEST_CASE(PerceptronWrongResponseSizeTest) { @@ -325,15 +325,15 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongResponseSizeTest) constexpr int N = 10; arma::mat trainX = arma::randu(D, N); - arma::Row trainY; // response vector with wrong size + arma::Row trainY; // Response vector with wrong size. - // 8 responses + // 8 responses. trainY << 0 << 0 << 1 << 0 << 1 << 1 << 1 << 0 << endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - // labels for training data have wrong size. Should give runtime error + // Labels for training data have wrong size. It should give runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -350,7 +350,7 @@ BOOST_AUTO_TEST_CASE(PerceptronNoResponsesTest) arma::mat trainX = arma::randu(D, N); SetInputParam("training", std::move(trainX)); - // No labels for training data. Should give runtime error + // No labels for training data. It should give runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -366,7 +366,7 @@ BOOST_AUTO_TEST_CASE(PerceptronNoTrainingDataTest) SetInputParam("labels", std::move(trainY)); - // No training data. Should give runtime error. + // No training data. It should give runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; @@ -384,32 +384,32 @@ BOOST_AUTO_TEST_CASE(PerceptronWrongDimOfTestData2) arma::mat trainX = arma::randu(D, N); arma::Row trainY; - // 10 responses + // 10 responses. trainY << 0 << 1 << 0 << 1 << 1 << 1 << 0 << 1 << 0 << 0 << endr; SetInputParam("training", std::move(trainX)); SetInputParam("labels", std::move(trainY)); - // training the model + // Training the model. mlpackMain(); - // get the output model obtained after the training + // Get the output model obtained after the training. PerceptronModel* model = CLI::GetParam("output_model"); - // reset the data passed + // Reset the data passed. CLI::GetSingleton().Parameters()["training"].wasPassed = false; CLI::GetSingleton().Parameters()["labels"].wasPassed = false; - // test data with Wrong dimensionality. + // Test data with Wrong dimensionality. arma::mat testX = arma::randu(D - 1, M); SetInputParam("input_model", model); SetInputParam("test", std::move(testX)); - // wrong dimensionality of test data. Should give runtime error. + // Wrong dimensionality of test data. It should give runtime error. Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; } -BOOST_AUTO_TEST_SUITE_END(); \ No newline at end of file +BOOST_AUTO_TEST_SUITE_END(); From e60ce603240856e07b6ca4ba9efe4182a42099f7 Mon Sep 17 00:00:00 2001 From: KARTHEEKCIC Date: Sat, 10 Feb 2018 01:00:04 +0530 Subject: [PATCH 34/34] Updating contributors list --- COPYRIGHT.txt | 1 + src/mlpack/core.hpp | 1 + 2 files changed, 2 insertions(+) diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt index 79e41b787c..a98a5340e0 100644 --- a/COPYRIGHT.txt +++ b/COPYRIGHT.txt @@ -88,6 +88,7 @@ Copyright: Copyright 2017, Haritha Sreedharan Nair Copyright 2017&2018, Sourabh Varshney Copyright 2018, Nikhil Goel + Copyright 2018, B Kartheek Reddy License: BSD-3-clause All rights reserved. diff --git a/src/mlpack/core.hpp b/src/mlpack/core.hpp index 918421a6e7..d8f5e63bd7 100644 --- a/src/mlpack/core.hpp +++ b/src/mlpack/core.hpp @@ -231,6 +231,7 @@ * - Haritha Sreedharan Nair * - Sourabh Varshney * - Nikhil Goel + * - B Kartheek Reddy */ // First, include all of the prerequisites.