From 982b199e4fe3bc4f34af2e9e7f3cd48d299daaa3 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Wed, 6 Jul 2016 01:00:14 +0200 Subject: [PATCH] Add randomized SVD test suite. --- src/mlpack/tests/CMakeLists.txt | 3 +- src/mlpack/tests/randomized_svd_test.cpp | 62 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/mlpack/tests/randomized_svd_test.cpp diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index 967edeee4e..b13494dff0 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 0000000000..a911279c52 --- /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 +#include + +#include +#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(3, 20); + arma::mat V = arma::randn(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();