regularized_svd_method.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_REGULARIZED_SVD_METHOD_HPP
15 #define MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_REGULARIZED_SVD_METHOD_HPP
16 
17 #include <mlpack/prereqs.hpp>
19 
20 namespace mlpack {
21 namespace cf {
22 
42 {
43  public:
50  RegSVDPolicy(const size_t maxIterations = 10) :
51  maxIterations(maxIterations)
52  {
53  /* Nothing to do here */
54  }
55 
68  void Apply(const arma::mat& data,
69  const arma::sp_mat& /* cleanedData */,
70  const size_t rank,
71  const size_t maxIterations,
72  const double /* minResidue */,
73  const bool /* mit */)
74  {
75  // Do singular value decomposition using the regularized SVD algorithm.
76  svd::RegularizedSVD<> regsvd(maxIterations);
77  regsvd.Apply(data, rank, w, h);
78  }
79 
86  double GetRating(const size_t user, const size_t item) const
87  {
88  double rating = arma::as_scalar(w.row(item) * h.col(user));
89  return rating;
90  }
91 
98  void GetRatingOfUser(const size_t user, arma::vec& rating) const
99  {
100  rating = w * h.col(user);
101  }
102 
115  template<typename NeighborSearchPolicy>
116  void GetNeighborhood(const arma::Col<size_t>& users,
117  const size_t numUsersForSimilarity,
118  arma::Mat<size_t>& neighborhood,
119  arma::mat& similarities) const
120  {
121  // We want to avoid calculating the full rating matrix, so we will do
122  // nearest neighbor search only on the H matrix, using the observation that
123  // if the rating matrix X = W*H, then d(X.col(i), X.col(j)) = d(W H.col(i),
124  // W H.col(j)). This can be seen as nearest neighbor search on the H
125  // matrix with the Mahalanobis distance where M^{-1} = W^T W. So, we'll
126  // decompose M^{-1} = L L^T (the Cholesky decomposition), and then multiply
127  // H by L^T. Then we can perform nearest neighbor search.
128  arma::mat l = arma::chol(w.t() * w);
129  arma::mat stretchedH = l * h; // Due to the Armadillo API, l is L^T.
130 
131  // Temporarily store feature vector of queried users.
132  arma::mat query(stretchedH.n_rows, users.n_elem);
133  // Select feature vectors of queried users.
134  for (size_t i = 0; i < users.n_elem; ++i)
135  query.col(i) = stretchedH.col(users(i));
136 
137  NeighborSearchPolicy neighborSearch(stretchedH);
138  neighborSearch.Search(
139  query, numUsersForSimilarity, neighborhood, similarities);
140  }
141 
143  const arma::mat& W() const { return w; }
145  const arma::mat& H() const { return h; }
146 
148  size_t MaxIterations() const { return maxIterations; }
150  size_t& MaxIterations() { return maxIterations; }
151 
155  template<typename Archive>
156  void serialize(Archive& ar, const uint32_t /* version */)
157  {
158  ar(CEREAL_NVP(w));
159  ar(CEREAL_NVP(h));
160  }
161 
162  private:
164  size_t maxIterations;
166  arma::mat w;
168  arma::mat h;
169 };
170 
171 } // namespace cf
172 } // namespace mlpack
173 
174 #endif
RegSVDPolicy(const size_t maxIterations=10)
Use regularized SVD method to perform collaborative filtering.
Regularized SVD is a matrix factorization technique that seeks to reduce the error on the training se...
Linear algebra utility functions, generally performed on matrices or vectors.
const arma::mat & H() const
Get the User Matrix.
size_t MaxIterations() const
Get the number of iterations.
The core includes that mlpack expects; standard C++ includes and Armadillo.
void GetNeighborhood(const arma::Col< size_t > &users, const size_t numUsersForSimilarity, arma::Mat< size_t > &neighborhood, arma::mat &similarities) const
Get the neighborhood and corresponding similarities for a set of users.
double GetRating(const size_t user, const size_t item) const
Return predicted rating given user ID and item ID.
void serialize(Archive &ar, const uint32_t)
Serialization.
size_t & MaxIterations()
Modify the number of iterations.
Implementation of the Regularized SVD policy to act as a wrapper when accessing Regularized SVD from ...
const arma::mat & W() const
Get the Item Matrix.
constexpr auto data(Container const &container) noexcept -> decltype(container.data())
Definition: iterator.hpp:79
void Apply(const arma::mat &data, const size_t rank, arma::mat &u, arma::mat &v)
Obtains the user and item matrices using the provided data and rank.
void Apply(const arma::mat &data, const arma::sp_mat &, const size_t rank, const size_t maxIterations, const double, const bool)
Apply Collaborative Filtering to the provided data set using the regularized SVD. ...
void GetRatingOfUser(const size_t user, arma::vec &rating) const
Get predicted ratings for a user.