[mlpack-svn] r15858 - mlpack/trunk/src/mlpack/tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Sat Sep 28 00:22:07 EDT 2013
Author: rcurtin
Date: Sat Sep 28 00:22:07 2013
New Revision: 15858
Log:
Test sparse NMF.
Modified:
mlpack/trunk/src/mlpack/tests/nmf_test.cpp
Modified: mlpack/trunk/src/mlpack/tests/nmf_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/nmf_test.cpp (original)
+++ mlpack/trunk/src/mlpack/tests/nmf_test.cpp Sat Sep 28 00:22:07 2013
@@ -22,7 +22,7 @@
/**
* Check the if the product of the calculated factorization is close to the
- * input matrix. Default case
+ * input matrix. Default case.
*/
BOOST_AUTO_TEST_CASE(NMFDefaultTest)
{
@@ -36,14 +36,14 @@
mat wh = w * h;
- for (size_t row = 0; row < 5; row++)
- for (size_t col = 0; col < 5; col++)
+ for (size_t row = 0; row < 20; row++)
+ for (size_t col = 0; col < 20; col++)
BOOST_REQUIRE_CLOSE(v(row, col), wh(row, col), 10.0);
}
/**
* Check the if the product of the calculated factorization is close to the
- * input matrix. Random Acol Initialization Distance Minimization Update
+ * input matrix. Random Acol initialization distance minimization update.
*/
BOOST_AUTO_TEST_CASE(NMFAcolDistTest)
{
@@ -57,14 +57,14 @@
mat wh = w * h;
- for (size_t row = 0; row < 5; row++)
- for (size_t col = 0; col < 5; col++)
+ for (size_t row = 0; row < 20; row++)
+ for (size_t col = 0; col < 20; col++)
BOOST_REQUIRE_CLOSE(v(row, col), wh(row, col), 10.0);
}
/**
* Check the if the product of the calculated factorization is close to the
- * input matrix. Random Initialization Divergence Minimization Update
+ * input matrix. Random initialization divergence minimization update.
*/
BOOST_AUTO_TEST_CASE(NMFRandomDivTest)
{
@@ -80,8 +80,8 @@
mat wh = w * h;
- for (size_t row = 0; row < 5; row++)
- for (size_t col = 0; col < 5; col++)
+ for (size_t row = 0; row < 20; row++)
+ for (size_t col = 0; col < 20; col++)
BOOST_REQUIRE_CLOSE(v(row, col), wh(row, col), 10.0);
}
@@ -104,9 +104,165 @@
mat wh = w * h;
- for (size_t row = 0; row < 5; row++)
- for (size_t col = 0; col < 5; col++)
- BOOST_REQUIRE_CLOSE(v(row, col), wh(row, col), 13.0);
+ for (size_t row = 0; row < 20; row++)
+ for (size_t col = 0; col < 20; col++)
+ BOOST_REQUIRE_CLOSE(v(row, col), wh(row, col), 15.0);
+}
+
+/**
+ * Check the if the product of the calculated factorization is close to the
+ * input matrix, with a sparse input matrix.. Default case.
+ */
+BOOST_AUTO_TEST_CASE(SparseNMFDefaultTest)
+{
+ mat w, h;
+ sp_mat v;
+ v.sprandu(20, 20, 0.2);
+ mat dv(v); // Make a dense copy.
+ mat dw, dh;
+ size_t r = 18;
+
+ // It seems to hit the iteration limit first.
+ NMF<> nmf(10000, 1e-20);
+ math::RandomSeed(1000); // Set random seed so results are the same.
+ nmf.Apply(v, r, w, h);
+ math::RandomSeed(1000);
+ nmf.Apply(dv, r, dw, dh);
+
+ // Make sure the results are about equal for the W and H matrices.
+ for (size_t i = 0; i < w.n_elem; ++i)
+ {
+ if (w(i) == 0.0)
+ BOOST_REQUIRE_SMALL(dw(i), 1e-15);
+ else
+ BOOST_REQUIRE_CLOSE(w(i), dw(i), 1e-5);
+ }
+
+ for (size_t i = 0; i < h.n_elem; ++i)
+ {
+ if (h(i) == 0.0)
+ BOOST_REQUIRE_SMALL(dh(i), 1e-15);
+ else
+ BOOST_REQUIRE_CLOSE(h(i), dh(i), 1e-5);
+ }
+}
+
+/**
+ * Check the if the product of the calculated factorization is close to the
+ * input matrix, with a sparse input matrix. Random Acol initialization,
+ * distance minimization update.
+ */
+BOOST_AUTO_TEST_CASE(SparseNMFAcolDistTest)
+{
+ mat w, h;
+ sp_mat v;
+ v.sprandu(20, 20, 0.3);
+ mat dv(v); // Make a dense copy.
+ mat dw, dh;
+ size_t r = 16;
+
+ NMF<RandomAcolInitialization<> > nmf;
+ math::RandomSeed(1000); // Set random seed so results are the same.
+ nmf.Apply(v, r, w, h);
+ math::RandomSeed(1000);
+ nmf.Apply(dv, r, dw, dh);
+
+ // Make sure the results are about equal for the W and H matrices.
+ for (size_t i = 0; i < w.n_elem; ++i)
+ {
+ if (w(i) == 0.0)
+ BOOST_REQUIRE_SMALL(dw(i), 1e-15);
+ else
+ BOOST_REQUIRE_CLOSE(w(i), dw(i), 1e-5);
+ }
+
+ for (size_t i = 0; i < h.n_elem; ++i)
+ {
+ if (h(i) == 0.0)
+ BOOST_REQUIRE_SMALL(dh(i), 1e-15);
+ else
+ BOOST_REQUIRE_CLOSE(h(i), dh(i), 1e-5);
+ }
+}
+
+/**
+ * Check the if the product of the calculated factorization is close to the
+ * input matrix, with a sparse input matrix. Random initialization, divergence
+ * minimization update.
+ */
+BOOST_AUTO_TEST_CASE(SparseNMFRandomDivTest)
+{
+ mat w, h;
+ sp_mat v;
+ v.sprandu(20, 20, 0.3);
+ mat dv(v); // Make a dense copy.
+ mat dw, dh;
+ size_t r = 16;
+
+ NMF<RandomInitialization,
+ WMultiplicativeDivergenceRule,
+ HMultiplicativeDivergenceRule> nmf;
+ math::RandomSeed(10); // Set random seed so the results are the same.
+ nmf.Apply(v, r, w, h);
+ math::RandomSeed(10);
+ nmf.Apply(dv, r, dw, dh);
+
+ // Make sure the results are about equal for the W and H matrices.
+ for (size_t i = 0; i < w.n_elem; ++i)
+ {
+ if (w(i) == 0.0)
+ BOOST_REQUIRE_SMALL(dw(i), 1e-15);
+ else
+ BOOST_REQUIRE_CLOSE(w(i), dw(i), 1e-5);
+ }
+
+ for (size_t i = 0; i < h.n_elem; ++i)
+ {
+ if (h(i) == 0.0)
+ BOOST_REQUIRE_SMALL(dh(i), 1e-15);
+ else
+ BOOST_REQUIRE_CLOSE(h(i), dh(i), 1e-5);
+ }
+}
+
+/**
+ * Check that the product of the calculated factorization is close to the
+ * input matrix, with a sparse input matrix. This uses the random
+ * initialization and alternating least squares update rule.
+ */
+BOOST_AUTO_TEST_CASE(SparseNMFALSTest)
+{
+ mat w, h;
+ sp_mat v;
+ v.sprandu(10, 10, 0.3);
+ mat dv(v); // Make a dense copy.
+ mat dw, dh;
+ size_t r = 8;
+
+ NMF<RandomInitialization,
+ WAlternatingLeastSquaresRule,
+ HAlternatingLeastSquaresRule> nmf;
+ math::RandomSeed(40);
+ nmf.Apply(v, r, w, h);
+ math::RandomSeed(40);
+ nmf.Apply(dv, r, dw, dh);
+
+ // Make sure the results are about equal for the W and H matrices.
+ for (size_t i = 0; i < w.n_elem; ++i)
+ {
+ if (w(i) == 0.0)
+ BOOST_REQUIRE_SMALL(dw(i), 1e-15);
+ else
+ BOOST_REQUIRE_CLOSE(w(i), dw(i), 1e-5);
+ }
+
+ for (size_t i = 0; i < h.n_elem; ++i)
+ {
+ if (h(i) == 0.0)
+ BOOST_REQUIRE_SMALL(dh(i), 1e-15);
+ else
+ BOOST_REQUIRE_CLOSE(h(i), dh(i), 1e-5);
+ }
}
BOOST_AUTO_TEST_SUITE_END();
More information about the mlpack-svn
mailing list