Add randomized SVD test suite.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
Reference in New Issue
Block a user