13 #ifndef MLPACK_METHODS_SVDWRAPPER_IMPL_HPP 14 #define MLPACK_METHODS_SVDWRAPPER_IMPL_HPP 22 template<
class Factorizer>
30 factorizer.Apply(W, E, H, V);
33 sigma.zeros(V.n_rows, V.n_cols);
35 for (
size_t i = 0; i < sigma.n_rows && i < sigma.n_cols; ++i)
36 sigma(i, i) = E(i, 0);
38 arma::mat V_rec = W * sigma * arma::trans(H);
41 return arma::norm(V - V_rec,
"fro") / arma::norm(V,
"fro");
52 arma::svd(W, E, H, V);
55 sigma.zeros(V.n_rows, V.n_cols);
57 for (
size_t i = 0; i < sigma.n_rows && i < sigma.n_cols; ++i)
58 sigma(i, i) = E(i, 0);
60 arma::mat V_rec = W * sigma * arma::trans(H);
63 return arma::norm(V - V_rec,
"fro") / arma::norm(V,
"fro");
66 template<
class Factorizer>
73 if (r > V.n_rows || r > V.n_cols)
75 Log::Info <<
"Rank " << r <<
", given for decomposition is invalid." 78 r = (V.n_rows > V.n_cols) ? V.n_cols : V.n_rows;
79 Log::Info <<
"Setting decomposition rank to " << r << std::endl;
84 factorizer.Apply(W, sigma, H, V);
87 W = W.submat(0, 0, W.n_rows - 1, r - 1);
88 H = H.submat(0, 0, H.n_cols - 1, r - 1);
91 sigma = sigma.subvec(0, r - 1);
95 W = W * arma::diagmat(sigma);
101 arma::mat V_rec = W * H;
104 return arma::norm(V - V_rec,
"fro") / arma::norm(V,
"fro");
114 if (r > V.n_rows || r > V.n_cols)
116 Log::Info <<
"Rank " << r <<
", given for decomposition is invalid." 119 r = (V.n_rows > V.n_cols) ? V.n_cols : V.n_rows;
120 Log::Info <<
"Setting decomposition rank to " << r << std::endl;
125 arma::svd(W, sigma, H, V);
128 W = W.submat(0, 0, W.n_rows - 1, r - 1);
129 H = H.submat(0, 0, H.n_cols - 1, r - 1);
132 sigma = sigma.subvec(0, r - 1);
136 W = W * arma::diagmat(sigma);
142 arma::mat V_rec = W * H;
145 return arma::norm(V - V_rec,
"fro") / arma::norm(V,
"fro");
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
double Apply(const arma::mat &V, arma::mat &W, arma::mat &sigma, arma::mat &H) const
Factorizer function which takes SVD of the given matrix and returns the frobenius norm of error...
Definition: svd_wrapper_impl.hpp:23
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84