[mlpack] 110/149: Refactor test with negative elements to decompose the random matrix into its proper low-rank decomposition, then test the reconstructed matrix.
Barak A. Pearlmutter
barak+git at pearlmutter.net
Sat May 2 09:11:15 UTC 2015
This is an automated email from the git hooks/post-receive script.
bap pushed a commit to branch svn-trunk
in repository mlpack.
commit 9b614a82d7c645b8c848702c28244dcf4cf5de81
Author: rcurtin <rcurtin at 9d5b8971-822b-0410-80eb-d18c1038ef23>
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.
git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@17391 9d5b8971-822b-0410-80eb-d18c1038ef23
---
src/mlpack/tests/svd_batch_test.cpp | 53 ++++++++++++++++++-------------------
1 file changed, 26 insertions(+), 27 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,
+ 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 @@ 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();
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git
More information about the debian-science-commits
mailing list