nmf_method.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_NMF_METHOD_HPP
14 #define MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_NMF_METHOD_HPP
15 
16 #include <mlpack/prereqs.hpp>
21 
22 namespace mlpack {
23 namespace cf {
24 
43 class NMFPolicy
44 {
45  public:
57  template<typename MatType>
58  void Apply(const MatType& /* data */,
59  const arma::sp_mat& cleanedData,
60  const size_t rank,
61  const size_t maxIterations,
62  const double minResidue,
63  const bool mit)
64  {
65  if (mit)
66  {
67  amf::MaxIterationTermination iter(maxIterations);
68 
69  // Do singular value decomposition using the NMF algorithm.
71  amf::NMFALSUpdate> nmf(iter);
72  nmf.Apply(cleanedData, rank, w, h);
73  }
74  else
75  {
76  amf::SimpleResidueTermination srt(minResidue, maxIterations);
77 
78  // Do singular value decomposition using the NMF algorithm.
79  amf::NMFALSFactorizer nmf(srt);
80  nmf.Apply(cleanedData, rank, w, h);
81  }
82  }
83 
90  double GetRating(const size_t user, const size_t item) const
91  {
92  double rating = arma::as_scalar(w.row(item) * h.col(user));
93  return rating;
94  }
95 
102  void GetRatingOfUser(const size_t user, arma::vec& rating) const
103  {
104  rating = w * h.col(user);
105  }
106 
119  template<typename NeighborSearchPolicy>
120  void GetNeighborhood(const arma::Col<size_t>& users,
121  const size_t numUsersForSimilarity,
122  arma::Mat<size_t>& neighborhood,
123  arma::mat& similarities) const
124  {
125  // We want to avoid calculating the full rating matrix, so we will do
126  // nearest neighbor search only on the H matrix, using the observation that
127  // if the rating matrix X = W*H, then d(X.col(i), X.col(j)) = d(W H.col(i),
128  // W H.col(j)). This can be seen as nearest neighbor search on the H
129  // matrix with the Mahalanobis distance where M^{-1} = W^T W. So, we'll
130  // decompose M^{-1} = L L^T (the Cholesky decomposition), and then multiply
131  // H by L^T. Then we can perform nearest neighbor search.
132  arma::mat l = arma::chol(w.t() * w);
133  arma::mat stretchedH = l * h; // Due to the Armadillo API, l is L^T.
134 
135  // Temporarily store feature vector of queried users.
136  arma::mat query(stretchedH.n_rows, users.n_elem);
137  // Select feature vectors of queried users.
138  for (size_t i = 0; i < users.n_elem; ++i)
139  query.col(i) = stretchedH.col(users(i));
140 
141  NeighborSearchPolicy neighborSearch(stretchedH);
142  neighborSearch.Search(
143  query, numUsersForSimilarity, neighborhood, similarities);
144  }
145 
147  const arma::mat& W() const { return w; }
149  const arma::mat& H() const { return h; }
150 
154  template<typename Archive>
155  void serialize(Archive& ar, const uint32_t /* version */)
156  {
157  ar(CEREAL_NVP(w));
158  ar(CEREAL_NVP(h));
159  }
160 
161  private:
163  arma::mat w;
165  arma::mat h;
166 };
167 
168 } // namespace cf
169 } // namespace mlpack
170 
171 #endif
This class implements AMF (alternating matrix factorization) on the given matrix V.
Definition: amf.hpp:78
This initialization rule for AMF simply fills the W and H matrices with uniform random noise in [0...
Definition: random_init.hpp:25
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
Implementation of the NMF policy to act as a wrapper when accessing NMF from within CFType...
Definition: nmf_method.hpp:43
This class implements a simple residue-based termination policy.
void serialize(Archive &ar, const uint32_t)
Serialization.
Definition: nmf_method.hpp:155
const arma::mat & H() const
Get the User Matrix.
Definition: nmf_method.hpp:149
double GetRating(const size_t user, const size_t item) const
Return predicted rating given user ID and item ID.
Definition: nmf_method.hpp:90
This class implements a method titled &#39;Alternating Least Squares&#39; described in the following paper: ...
Definition: nmf_als.hpp:41
double Apply(const MatType &V, const size_t r, arma::mat &W, arma::mat &H)
Apply Alternating Matrix Factorization to the provided matrix.
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.
Definition: nmf_method.hpp:120
const arma::mat & W() const
Get the Item Matrix.
Definition: nmf_method.hpp:147
This termination policy only terminates when the maximum number of iterations has been reached...
void GetRatingOfUser(const size_t user, arma::vec &rating) const
Get predicted ratings for a user.
Definition: nmf_method.hpp:102
void Apply(const MatType &, const arma::sp_mat &cleanedData, const size_t rank, const size_t maxIterations, const double minResidue, const bool mit)
Apply Collaborative Filtering to the provided dataset using NMF method.
Definition: nmf_method.hpp:58