[mlpack-git] master: Add test for MiniBatchSGD. (46e0a23)

gitdub at mlpack.org gitdub at mlpack.org
Fri Feb 12 17:47:23 EST 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/ec35c2d4bc564e1431abf7c9aa14737c1d40328b...46e0a233c29ae638ba60eb224826168516c0ec4e

>---------------------------------------------------------------

commit 46e0a233c29ae638ba60eb224826168516c0ec4e
Author: Ryan Curtin <ryan at ratml.org>
Date:   Fri Feb 12 14:47:23 2016 -0800

    Add test for MiniBatchSGD.
    
    The second test doesn't work yet, but I think it's because I have set up the
    problem wrong.


>---------------------------------------------------------------

46e0a233c29ae638ba60eb224826168516c0ec4e
 src/mlpack/tests/CMakeLists.txt         |  1 +
 src/mlpack/tests/minibatch_sgd_test.cpp | 63 +++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt
index 70b276c..9fca06a 100644
--- a/src/mlpack/tests/CMakeLists.txt
+++ b/src/mlpack/tests/CMakeLists.txt
@@ -44,6 +44,7 @@ add_executable(mlpack_test
   maximal_inputs_test.cpp
   mean_shift_test.cpp
   metric_test.cpp
+  minibatch_sgd_test.cpp
   nbc_test.cpp
   nca_test.cpp
   nmf_test.cpp
diff --git a/src/mlpack/tests/minibatch_sgd_test.cpp b/src/mlpack/tests/minibatch_sgd_test.cpp
new file mode 100644
index 0000000..a05121a
--- /dev/null
+++ b/src/mlpack/tests/minibatch_sgd_test.cpp
@@ -0,0 +1,63 @@
+/**
+ * @file minibatch_sgd_test.cpp
+ * @author Ryan Curtin
+ *
+ * Test file for minibatch SGD.
+ */
+#include <mlpack/core.hpp>
+#include <mlpack/core/optimizers/sgd/sgd.hpp>
+#include <mlpack/core/optimizers/minibatch_sgd/minibatch_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(MiniBatchSGDTest);
+
+/**
+ * If the batch size is 1, and we aren't shuffling, we should get the exact same
+ * results as regular SGD.
+ */
+BOOST_AUTO_TEST_CASE(SGDSimilarityTest)
+{
+  SGDTestFunction f;
+  SGD<SGDTestFunction> s(f, 0.0003, 5000000, 1e-9, false);
+  MiniBatchSGD<SGDTestFunction> ms(f, 1, 0.0003, 5000000, 1e-9, false);
+
+  arma::mat sCoord = f.GetInitialPoint();
+  arma::mat msCoord = f.GetInitialPoint();
+
+  const double sResult = s.Optimize(sCoord);
+  const double msResult = s.Optimize(msCoord);
+
+  BOOST_REQUIRE_CLOSE(sResult, msResult, 1e-8);
+  BOOST_REQUIRE_CLOSE(sCoord[0], msCoord[0], 1e-8);
+  BOOST_REQUIRE_CLOSE(sCoord[1], msCoord[1], 1e-8);
+  BOOST_REQUIRE_CLOSE(sCoord[2], msCoord[2], 1e-8);
+}
+
+/*
+BOOST_AUTO_TEST_CASE(SimpleSGDTestFunction)
+{
+  SGDTestFunction f;
+  // Batch size of 3.
+  MiniBatchSGD<SGDTestFunction> s(f, 3, 0.0005, 2000000, 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_SUITE_END();




More information about the mlpack-git mailing list