[mlpack-git] master: Add randomized SVD test suite. (982b199)

gitdub at mlpack.org gitdub at mlpack.org
Wed Jul 6 15:30:09 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/98babfc774bce91170df994763b670b9abd20917...e7b9b042d1d6e2d9895d5fa141e9c135b2d2ea57

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

commit 982b199e4fe3bc4f34af2e9e7f3cd48d299daaa3
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date:   Wed Jul 6 01:00:14 2016 +0200

    Add randomized SVD test suite.


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

982b199e4fe3bc4f34af2e9e7f3cd48d299daaa3
 src/mlpack/tests/CMakeLists.txt          |  3 +-
 src/mlpack/tests/randomized_svd_test.cpp | 62 ++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt
index 967edee..b13494d 100644
--- a/src/mlpack/tests/CMakeLists.txt
+++ b/src/mlpack/tests/CMakeLists.txt
@@ -57,7 +57,9 @@ add_executable(mlpack_test
   perceptron_test.cpp
   quic_svd_test.cpp
   radical_test.cpp
+  randomized_svd_test.cpp
   range_search_test.cpp
+  recurrent_network_test.cpp
   rectangle_tree_test.cpp
   regularized_svd_test.cpp
   rmsprop_test.cpp
@@ -80,7 +82,6 @@ add_executable(mlpack_test
   svd_incremental_test.cpp
   nystroem_method_test.cpp
   armadillo_svd_test.cpp
-  recurrent_network_test.cpp
 )
 # Link dependencies of test executable.
 target_link_libraries(mlpack_test
diff --git a/src/mlpack/tests/randomized_svd_test.cpp b/src/mlpack/tests/randomized_svd_test.cpp
new file mode 100644
index 0000000..a911279
--- /dev/null
+++ b/src/mlpack/tests/randomized_svd_test.cpp
@@ -0,0 +1,62 @@
+/**
+ * @file randomized_svd_test.cpp
+ * @author Marcus Edel
+ *
+ * Test file for the Randomized SVD class.
+ */
+
+#include <mlpack/core.hpp>
+#include <mlpack/methods/randomized_svd/randomized_svd.hpp>
+
+#include <boost/test/unit_test.hpp>
+#include "test_tools.hpp"
+
+BOOST_AUTO_TEST_SUITE(RandomizedSVDTest);
+
+using namespace mlpack;
+
+/**
+ * The reconstruction and sigular value error of the obtained SVD should be
+ * small.
+ */
+BOOST_AUTO_TEST_CASE(RandomizedSVDReconstructionError)
+{
+  arma::mat U = arma::randn<arma::mat>(3, 20);
+  arma::mat V = arma::randn<arma::mat>(10, 3);
+
+  arma::mat R;
+  arma::qr_econ(U, R, U);
+  arma::qr_econ(V, R, V);
+
+  arma::mat s = arma::diagmat(arma::vec("1 0.1 0.01"));
+
+  arma::mat data = arma::trans(U * arma::diagmat(s) * V.t());
+
+  // Center the data into a temporary matrix.
+  arma::mat centeredData;
+  math::Center(data, centeredData);
+
+  arma::mat U1, U2, V1, V2;
+  arma::vec s1, s2, s3;
+
+  arma::svd_econ(U1, s1, V1, centeredData);
+
+  svd::RandomizedSVD rSVD(0, 6);
+  rSVD.Apply(data, U2, s2, V2, 3);
+
+  // Use the same amount of data for the compariosn (matrix rank).
+  s3 = s1.subvec(0, s2.n_elem - 1);
+
+  // The sigular value error should be small.
+  double error = arma::norm(s2 - s3, "frob");
+  BOOST_REQUIRE_SMALL(error, 1e-5);
+
+  arma::mat reconstruct = U2 * arma::diagmat(s2) * V2.t();
+
+  // The relative reconstruction error should be small.
+  error = arma::norm(centeredData - reconstruct, "frob") /
+      arma::norm(centeredData, "frob");
+  BOOST_REQUIRE_SMALL(error, 1e-5);
+}
+
+BOOST_AUTO_TEST_SUITE_END();




More information about the mlpack-git mailing list