[mlpack-git] master: Refactor test with negative elements to decompose the random matrix into its proper low-rank decomposition, then test the reconstructed matrix. (f039262)

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


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

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

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

commit f039262e46800173f2390efaa2fa24090c459c28
Author: Ryan Curtin <ryan at ratml.org>
Date:   Wed Nov 19 17:18:01 2014 +0000

    Refactor test with negative elements to decompose the random matrix into its
    proper low-rank decomposition, then test the reconstructed matrix.


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

f039262e46800173f2390efaa2fa24090c459c28
 src/mlpack/tests/svd_batch_test.cpp | 45 ++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 23 deletions(-)

diff --git a/src/mlpack/tests/svd_batch_test.cpp b/src/mlpack/tests/svd_batch_test.cpp
index a5deb7f..70d9a3a 100644
--- a/src/mlpack/tests/svd_batch_test.cpp
+++ b/src/mlpack/tests/svd_batch_test.cpp
@@ -21,17 +21,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>,
       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());
 }
 
 /**
@@ -61,6 +60,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>,
@@ -122,7 +123,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);
@@ -142,17 +143,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,
@@ -160,17 +162,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