[mlpack-git] mlpack-1.0.x: Backport r17390-17395. (a3c8675)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 22:06:36 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : mlpack-1.0.x
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

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

commit a3c8675fb9c025e1ea31d4e1dacb3f5f39b95c31
Author: Ryan Curtin <ryan at ratml.org>
Date:   Sun Dec 7 19:39:59 2014 +0000

    Backport r17390-17395.


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

a3c8675fb9c025e1ea31d4e1dacb3f5f39b95c31
 HISTORY.txt                             |  3 +++
 src/mlpack/core.hpp                     |  3 +++
 src/mlpack/core/data/load_impl.hpp      |  2 --
 src/mlpack/core/math/random.hpp         |  6 +++++
 src/mlpack/core/util/sfinae_utility.hpp |  1 +
 src/mlpack/prereqs.hpp                  |  7 +++++
 src/mlpack/tests/svd_batch_test.cpp     | 47 ++++++++++++++++-----------------
 7 files changed, 43 insertions(+), 26 deletions(-)

diff --git a/HISTORY.txt b/HISTORY.txt
index ffbab31..d0ce8c4 100644
--- a/HISTORY.txt
+++ b/HISTORY.txt
@@ -15,6 +15,9 @@
 
   * Fixes for numerous intermittent test failures.
 
+  * math::RandomSeed() now sets the random seed for recent (>=3.930) Armadillo
+    versions.
+
 2014-08-29    mlpack 1.0.10
 
   * Bugfix for NeighborSearch regression which caused very slow allknn/allkfn.
diff --git a/src/mlpack/core.hpp b/src/mlpack/core.hpp
index a68a14b..541f06c 100644
--- a/src/mlpack/core.hpp
+++ b/src/mlpack/core.hpp
@@ -193,6 +193,9 @@
 #include <mlpack/core/kernels/spherical_kernel.hpp>
 #include <mlpack/core/kernels/triangular_kernel.hpp>
 
+// Use Armadillo's C++ version detection.
+#ifdef ARMA_USE_CXX11
+  #define MLPACK_USE_CX11
 #endif
 
 // Clean up unfortunate Windows preprocessor definitions, even if this file was
diff --git a/src/mlpack/core/data/load_impl.hpp b/src/mlpack/core/data/load_impl.hpp
index a995b71..b81ad3b 100644
--- a/src/mlpack/core/data/load_impl.hpp
+++ b/src/mlpack/core/data/load_impl.hpp
@@ -216,8 +216,6 @@ bool Load(const std::string& filename,
     Log::Info << "Loading '" << filename << "' as " << stringType << ".  "
         << std::flush;
 
-  Log::Debug << "load type " << loadType << "\n";
-
   const bool success = matrix.load(stream, loadType);
 
   if (!success)
diff --git a/src/mlpack/core/math/random.hpp b/src/mlpack/core/math/random.hpp
index 1e72e25..ce5ab44 100644
--- a/src/mlpack/core/math/random.hpp
+++ b/src/mlpack/core/math/random.hpp
@@ -64,6 +64,12 @@ inline void RandomSeed(const size_t seed)
 {
   randGen.seed((uint32_t) seed);
   srand((unsigned int) seed);
+#if ARMA_VERSION_MAJOR > 3 || \
+    (ARMA_VERSION_MAJOR == 3 && ARMA_VERSION_MINOR >= 930)
+  // Armadillo >= 3.930 has its own random number generator internally that we
+  // need to set the seed for also.
+  arma::arma_rng::set_seed(seed);
+#endif
 }
 
 /**
diff --git a/src/mlpack/core/util/sfinae_utility.hpp b/src/mlpack/core/util/sfinae_utility.hpp
index 91c5f05..7f1d5a0 100644
--- a/src/mlpack/core/util/sfinae_utility.hpp
+++ b/src/mlpack/core/util/sfinae_utility.hpp
@@ -27,6 +27,7 @@
 
 #include <boost/utility/enable_if.hpp>
 #include <boost/type_traits.hpp>
+#include <mlpack/prereqs.hpp>
 
 /*
  * Constructs a template supporting the SFINAE pattern.
diff --git a/src/mlpack/prereqs.hpp b/src/mlpack/prereqs.hpp
index 84d36d0..0507e59 100644
--- a/src/mlpack/prereqs.hpp
+++ b/src/mlpack/prereqs.hpp
@@ -63,4 +63,11 @@ problems.  It should only be necessary to include <mlpack/core.hpp> and not\
 // Now include Armadillo through the special mlpack extensions.
 #include <mlpack/core/arma_extend/arma_extend.hpp>
 
+// On Visual Studio, disable C4519 (default arguments for function templates)
+// since it's by default an error, which doesn't even make any sense because
+// it's part of the C++11 standard.
+#ifdef _MSC_VER
+  #pragma warning(disable : 4519)
+#endif
+
 #endif
diff --git a/src/mlpack/tests/svd_batch_test.cpp b/src/mlpack/tests/svd_batch_test.cpp
index 8c6ba8f..1774209 100644
--- a/src/mlpack/tests/svd_batch_test.cpp
+++ b/src/mlpack/tests/svd_batch_test.cpp
@@ -35,17 +35,16 @@ using namespace arma;
  */
 BOOST_AUTO_TEST_CASE(SVDBatchConvergenceElementTest)
 {
-  mlpack::math::RandomSeed(10);
   sp_mat data;
   data.sprandn(1000, 1000, 0.2);
   AMF<SimpleToleranceTermination<sp_mat>,
-      RandomInitialization, 
+      AverageInitialization,
       SVDBatchLearning> amf;
-  mat m1,m2;
+  mat m1, m2;
   amf.Apply(data, 2, m1, m2);
 
   BOOST_REQUIRE_NE(amf.TerminationPolicy().Iteration(),
-                    amf.TerminationPolicy().MaxIterations());
+                   amf.TerminationPolicy().MaxIterations());
 }
 
 /**
@@ -75,6 +74,8 @@ BOOST_AUTO_TEST_CASE(SVDBatchMomentumTest)
   // Fill sparse matrix.
   sp_mat cleanedData = arma::sp_mat(locations, values, maxUserID, maxItemID);
 
+  // Explicitly setting the random seed forces the random initialization to be
+  // the same.  There may be a better way to do this.
   mlpack::math::RandomSeed(10);
   ValidationRMSETermination<sp_mat> vrt(cleanedData, 2000);
   AMF<ValidationRMSETermination<sp_mat>,
@@ -136,7 +137,7 @@ BOOST_AUTO_TEST_CASE(SVDBatchRegularizationTest)
                               RandomInitialization(),
                               SVDBatchLearning(0.0009, 0, 0, 0));
 
-  mat m1,m2;
+  mat m1, m2;
   double RMSE_1 = amf_1.Apply(cleanedData, 2, m1, m2);
 
   mlpack::math::RandomSeed(10);
@@ -156,17 +157,18 @@ BOOST_AUTO_TEST_CASE(SVDBatchRegularizationTest)
  */
 BOOST_AUTO_TEST_CASE(SVDBatchNegativeElementTest)
 {
-  mat test;
-  test.zeros(3,3);
-  test(0, 0) = 1;
-  test(0, 1) = -2;
-  test(0, 2) = 3;
-  test(1, 0) = 2;
-  test(1, 1) = -1;
-  test(1, 2) = 2;
-  test(2, 0) = 2;
-  test(2, 1) = 2;
-  test(2, 2) = 2;
+  math::RandomSeed(std::time(NULL));
+  // Create two 5x3 matrices that we should be able to recover.
+  mat testLeft;
+  testLeft.randu(5, 3);
+  testLeft -= 0.5; // Shift so elements are negative.
+
+  mat testRight;
+  testRight.randu(3, 5);
+  testRight -= 0.5; // Shift so elements are negative.
+
+  // Assemble a rank-3 matrix that is 5x5.
+  mat test = testLeft * testRight;
 
   AMF<SimpleToleranceTermination<mat>,
       RandomInitialization,
@@ -174,17 +176,14 @@ BOOST_AUTO_TEST_CASE(SVDBatchNegativeElementTest)
                             RandomInitialization(),
                             SVDBatchLearning(0.3, 0.001, 0.001, 0));
   mat m1, m2;
-  amf.Apply(test, 2, m1, m2);
+  amf.Apply(test, 3, m1, m2);
 
   arma::mat result = m1 * m2;
 
-  for(size_t i = 0;i < 3;i++)
-  {
-    for(size_t j = 0;j < 3;j++)
-    {
-      BOOST_REQUIRE_LE(abs(test(i,j) - result(i,j)), 0.5);
-    }
-  }
+  // 5% element-wise tolerance.
+  for (size_t i = 0; i < 3; i++)
+    for (size_t j = 0; j < 3; j++)
+      BOOST_REQUIRE_CLOSE(test(i, j), result(i, j), 5.0);
 }
 
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-git mailing list