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

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Nov 19 12:18:01 EST 2014


Author: rcurtin
Date: Wed Nov 19 12:18:01 2014
New Revision: 17391

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


Modified:
   mlpack/trunk/src/mlpack/tests/svd_batch_test.cpp

Modified: mlpack/trunk/src/mlpack/tests/svd_batch_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/svd_batch_test.cpp	(original)
+++ mlpack/trunk/src/mlpack/tests/svd_batch_test.cpp	Wed Nov 19 12:18:01 2014
@@ -21,17 +21,16 @@
  */
 BOOST_AUTO_TEST_CASE(SVDBatchConvergenceElementTest)
 {
-  mlpack::math::RandomSeed(10);
   sp_mat data;
   data.sprandn(1000, 1000, 0.2);
-  AMF<SimpleToleranceTermination<sp_mat>, 
-      AverageInitialization, 
+  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());
+
+  BOOST_REQUIRE_NE(amf.TerminationPolicy().Iteration(),
+                   amf.TerminationPolicy().MaxIterations());
 }
 
 /**
@@ -61,6 +60,8 @@
   // 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 @@
                               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(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 @@
                             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-svn mailing list