[mlpack-svn] r16023 - mlpack/trunk/src/mlpack/tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Nov 13 13:02:31 EST 2013


Author: rcurtin
Date: Wed Nov 13 13:02:30 2013
New Revision: 16023

Log:
Add a randomized test for the logistic regression likelihood function that is
not just comprised of simple examples.


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	Wed Nov 13 13:02:30 2013
@@ -16,7 +16,7 @@
 BOOST_AUTO_TEST_SUITE(LogisticRegressionTest);
 
 /**
- * Test the LogisticFunction on a simple set of points.
+ * Test the LogisticRegressionFunction on a simple set of points.
  */
 BOOST_AUTO_TEST_CASE(LogisticRegressionFunctionEvaluate)
 {
@@ -38,4 +38,46 @@
   BOOST_REQUIRE_CLOSE(lrf.Evaluate(arma::vec("200 -100 20")), 0.0, 1e-5);
 }
 
+/**
+ * A more complicated test for the LogisticRegressionFunction.
+ */
+BOOST_AUTO_TEST_CASE(LogisticRegressionFunctionRandomEvaluate)
+{
+  const size_t points = 1000;
+  const size_t dimension = 10;
+  const size_t trials = 50;
+
+  // 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 lrf(data, responses, 0.0 /* no regularization */);
+
+  // Run a bunch of trials.
+  for (size_t i = 0; i < trials; ++i)
+  {
+    // Generate a random set of parameters.
+    arma::vec parameters;
+    parameters.randu(dimension);
+
+    // Hand-calculate the loss function.
+    double loglikelihood = 0.0;
+    for (size_t j = 0; j < points; ++j)
+    {
+      const double sigmoid = (1.0 / (1.0 +
+          exp(-arma::dot(data.col(j), parameters))));
+      if (responses[j] == 1.0)
+        loglikelihood += log(std::pow(sigmoid, responses[j]));
+      else
+        loglikelihood += log(std::pow(1.0 - sigmoid, 1.0 - responses[j]));
+    }
+
+    BOOST_REQUIRE_CLOSE(lrf.Evaluate(parameters), -loglikelihood, 1e-5);
+  }
+}
+
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-svn mailing list