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

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Oct 30 18:48:07 EDT 2012


Author: rcurtin
Date: 2012-10-30 18:48:07 -0400 (Tue, 30 Oct 2012)
New Revision: 13793

Added:
   mlpack/trunk/src/mlpack/tests/sgd_test.cpp
Modified:
   mlpack/trunk/src/mlpack/tests/CMakeLists.txt
Log:
Add a test for SGD.


Modified: mlpack/trunk/src/mlpack/tests/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/tests/CMakeLists.txt	2012-10-30 22:47:54 UTC (rev 13792)
+++ mlpack/trunk/src/mlpack/tests/CMakeLists.txt	2012-10-30 22:48:07 UTC (rev 13793)
@@ -28,6 +28,7 @@
   radical_test.cpp
   range_search_test.cpp
   save_restore_utility_test.cpp
+  sgd_test.cpp
   sort_policy_test.cpp
   sparse_coding_test.cpp
   tree_test.cpp

Added: mlpack/trunk/src/mlpack/tests/sgd_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/sgd_test.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/tests/sgd_test.cpp	2012-10-30 22:48:07 UTC (rev 13793)
@@ -0,0 +1,56 @@
+/**
+ * @file sgd_test.cpp
+ * @author Ryan Curtin
+ *
+ * Test file for SGD (stochastic gradient descent).
+ */
+#include <mlpack/core.hpp>
+#include <mlpack/core/optimizers/sgd/sgd.hpp>
+#include <mlpack/core/optimizers/lbfgs/test_functions.hpp>
+#include <mlpack/core/optimizers/sgd/test_function.hpp>
+
+#include <boost/test/unit_test.hpp>
+#include "old_boost_test_definitions.hpp"
+
+using namespace std;
+using namespace arma;
+using namespace mlpack;
+using namespace mlpack::optimization;
+using namespace mlpack::optimization::test;
+
+BOOST_AUTO_TEST_SUITE(SGDTest);
+
+BOOST_AUTO_TEST_CASE(SimpleSGDTestFunction)
+{
+  SGDTestFunction f;
+  SGD<SGDTestFunction> s(f, 0.0003, 5000000, 1e-9, true);
+
+  arma::mat coordinates = f.GetInitialPoint();
+  double result = s.Optimize(coordinates);
+
+  BOOST_REQUIRE_CLOSE(result, -1.0, 0.05);
+  BOOST_REQUIRE_SMALL(coordinates[0], 1e-3);
+  BOOST_REQUIRE_SMALL(coordinates[1], 1e-7);
+  BOOST_REQUIRE_SMALL(coordinates[2], 1e-7);
+}
+
+BOOST_AUTO_TEST_CASE(GeneralizedRosenbrockTest)
+{
+  // Loop over several variants.
+  for (size_t i = 10; i < 50; i += 5)
+  {
+    // Create the generalized Rosenbrock function.
+    GeneralizedRosenbrockFunction f(i);
+
+    SGD<GeneralizedRosenbrockFunction> s(f, 0.001, 0, 1e-15, true);
+
+    arma::mat coordinates = f.GetInitialPoint();
+    double result = s.Optimize(coordinates);
+
+    BOOST_REQUIRE_SMALL(result, 1e-10);
+    for (size_t j = 0; j < i; ++j)
+      BOOST_REQUIRE_CLOSE(coordinates[j], 1, 1e-3);
+  }
+}
+
+BOOST_AUTO_TEST_SUITE_END();




More information about the mlpack-svn mailing list