[mlpack-svn] r16033 - mlpack/trunk/src/mlpack/tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Nov 14 11:11:03 EST 2013
Author: rcurtin
Date: Thu Nov 14 11:11:03 2013
New Revision: 16033
Log:
Test regularization for separable Evaluate().
Modified:
mlpack/trunk/src/mlpack/tests/logistic_regression_test.cpp
Modified: mlpack/trunk/src/mlpack/tests/logistic_regression_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/logistic_regression_test.cpp (original)
+++ mlpack/trunk/src/mlpack/tests/logistic_regression_test.cpp Thu Nov 14 11:11:03 2013
@@ -213,4 +213,53 @@
BOOST_REQUIRE_SMALL(lrf.Evaluate(arma::vec("200 -100 20"), 2), 1e-5);
}
+/**
+ * Test regularization for the separable LogisticRegressionFunction Evaluate()
+ * function.
+ */
+BOOST_AUTO_TEST_CASE(LogisticRegressionFunctionRegularizationSeparableEvaluate)
+{
+ const size_t points = 5000;
+ const size_t dimension = 25;
+ const size_t trials = 10;
+
+ // Create a random dataset.
+ arma::mat data;
+ data.randu(dimension, points);
+ // Create random responses.
+ arma::vec responses(points);
+ for (size_t i = 0; i < points; ++i)
+ responses[i] = math::RandInt(0, 2);
+
+ LogisticRegressionFunction lrfNoReg(data, responses, 0.0);
+ LogisticRegressionFunction lrfSmallReg(data, responses, 0.5);
+ LogisticRegressionFunction lrfBigReg(data, responses, 20.0);
+
+ // Check that the number of functions is correct.
+ BOOST_REQUIRE_EQUAL(lrfNoReg.NumFunctions(), points);
+ BOOST_REQUIRE_EQUAL(lrfSmallReg.NumFunctions(), points);
+ BOOST_REQUIRE_EQUAL(lrfBigReg.NumFunctions(), points);
+
+ for (size_t i = 0; i < trials; ++i)
+ {
+ arma::vec parameters(dimension);
+ parameters.randu();
+
+ // Regularization term: 0.5 * lambda * || parameters ||_2^2 (but note that
+ // the first parameters term is ignored).
+ const double smallRegTerm = (0.25 * std::pow(arma::norm(parameters, 2), 2.0)
+ - 0.25 * std::pow(parameters[0], 2.0)) / points;
+ const double bigRegTerm = (10.0 * std::pow(arma::norm(parameters, 2), 2.0)
+ - 10.0 * std::pow(parameters[0], 2.0)) / points;
+
+ for (size_t j = 0; j < points; ++j)
+ {
+ BOOST_REQUIRE_CLOSE(lrfNoReg.Evaluate(parameters, j) - smallRegTerm,
+ lrfSmallReg.Evaluate(parameters, j), 1e-5);
+ BOOST_REQUIRE_CLOSE(lrfNoReg.Evaluate(parameters, j) - bigRegTerm,
+ lrfBigReg.Evaluate(parameters, j), 1e-5);
+ }
+ }
+}
+
BOOST_AUTO_TEST_SUITE_END();
More information about the mlpack-svn
mailing list