mlpack
svd_wrapper_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_SVDWRAPPER_IMPL_HPP
14 #define MLPACK_METHODS_SVDWRAPPER_IMPL_HPP
15 
16 // In case it hasn't yet been included.
17 #include "svd_wrapper.hpp"
18 
19 namespace mlpack {
20 namespace cf {
21 
22 template<class Factorizer>
23 double SVDWrapper<Factorizer>::Apply(const arma::mat& V,
24  arma::mat& W,
25  arma::mat& sigma,
26  arma::mat& H) const
27 {
28  // get svd factorization
29  arma::vec E;
30  factorizer.Apply(W, E, H, V);
31 
32  // construct sigma matrix
33  sigma.zeros(V.n_rows, V.n_cols);
34 
35  for (size_t i = 0; i < sigma.n_rows && i < sigma.n_cols; ++i)
36  sigma(i, i) = E(i, 0);
37 
38  arma::mat V_rec = W * sigma * arma::trans(H);
39 
40  // return normalized frobenius error
41  return arma::norm(V - V_rec, "fro") / arma::norm(V, "fro");
42 }
43 
44 template<>
45 double SVDWrapper<DummyClass>::Apply(const arma::mat& V,
46  arma::mat& W,
47  arma::mat& sigma,
48  arma::mat& H) const
49 {
50  // get svd factorization
51  arma::vec E;
52  arma::svd(W, E, H, V);
53 
54  // construct sigma matrix
55  sigma.zeros(V.n_rows, V.n_cols);
56 
57  for (size_t i = 0; i < sigma.n_rows && i < sigma.n_cols; ++i)
58  sigma(i, i) = E(i, 0);
59 
60  arma::mat V_rec = W * sigma * arma::trans(H);
61 
62  // return normalized frobenius error
63  return arma::norm(V - V_rec, "fro") / arma::norm(V, "fro");
64 }
65 
66 template<class Factorizer>
67 double SVDWrapper<Factorizer>::Apply(const arma::mat& V,
68  size_t r,
69  arma::mat& W,
70  arma::mat& H) const
71 {
72  // check if the given rank is valid
73  if (r > V.n_rows || r > V.n_cols)
74  {
75  Log::Info << "Rank " << r << ", given for decomposition is invalid."
76  << std::endl;
77 
78  r = (V.n_rows > V.n_cols) ? V.n_cols : V.n_rows;
79  Log::Info << "Setting decomposition rank to " << r << std::endl;
80  }
81 
82  // get svd factorization
83  arma::vec sigma;
84  factorizer.Apply(W, sigma, H, V);
85 
86  // remove the part of W and H depending upon the value of rank
87  W = W.submat(0, 0, W.n_rows - 1, r - 1);
88  H = H.submat(0, 0, H.n_cols - 1, r - 1);
89 
90  // take only required eigenvalues
91  sigma = sigma.subvec(0, r - 1);
92 
93  // eigenvalue matrix is multiplied to W
94  // it can either be multiplied to H matrix
95  W = W * arma::diagmat(sigma);
96 
97  // take transpose of the matrix H as required by CF module
98  H = arma::trans(H);
99 
100  // reconstruct the matrix
101  arma::mat V_rec = W * H;
102 
103  // return the normalized frobenius norm
104  return arma::norm(V - V_rec, "fro") / arma::norm(V, "fro");
105 }
106 
107 template<>
108 double SVDWrapper<DummyClass>::Apply(const arma::mat& V,
109  size_t r,
110  arma::mat& W,
111  arma::mat& H) const
112 {
113  // check if the given rank is valid
114  if (r > V.n_rows || r > V.n_cols)
115  {
116  Log::Info << "Rank " << r << ", given for decomposition is invalid."
117  << std::endl;
118 
119  r = (V.n_rows > V.n_cols) ? V.n_cols : V.n_rows;
120  Log::Info << "Setting decomposition rank to " << r << std::endl;
121  }
122 
123  // get svd factorization
124  arma::vec sigma;
125  arma::svd(W, sigma, H, V);
126 
127  // remove the part of W and H depending upon the value of rank
128  W = W.submat(0, 0, W.n_rows - 1, r - 1);
129  H = H.submat(0, 0, H.n_cols - 1, r - 1);
130 
131  // take only required eigenvalues
132  sigma = sigma.subvec(0, r - 1);
133 
134  // eigenvalue matrix is multiplied to W
135  // it can either be multiplied to H matrix
136  W = W * arma::diagmat(sigma);
137 
138  // take transpose of the matrix H as required by CF module
139  H = arma::trans(H);
140 
141  // reconstruct the matrix
142  arma::mat V_rec = W * H;
143 
144  // return the normalized frobenius norm
145  return arma::norm(V - V_rec, "fro") / arma::norm(V, "fro");
146 }
147 
148 } // namespace cf
149 } // namespace mlpack
150 
151 #endif
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