svdplusplus_method.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_SVDPLUSPLUS_METHOD_HPP
15 #define MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_SVDPLUSPLUS_METHOD_HPP
16 
17 #include <mlpack/prereqs.hpp>
19 
20 namespace mlpack {
21 namespace cf {
22 
42 {
43  public:
51  SVDPlusPlusPolicy(const size_t maxIterations = 10,
52  const double alpha = 0.001,
53  const double lambda = 0.1) :
54  maxIterations(maxIterations),
55  alpha(alpha),
56  lambda(lambda)
57  {
58  /* Nothing to do here */
59  }
60 
73  void Apply(const arma::mat& data,
74  const arma::sp_mat& /* cleanedData */,
75  const size_t rank,
76  const size_t maxIterations,
77  const double /* minResidue */,
78  const bool /* mit */)
79  {
80  svd::SVDPlusPlus<> svdpp(maxIterations, alpha, lambda);
81 
82  // Save implicit data in the form of sparse matrix.
83  arma::mat implicitDenseData = data.submat(0, 0, 1, data.n_cols - 1);
84  svdpp.CleanData(implicitDenseData, implicitData, data);
85 
86  // Perform decomposition using the svdplusplus algorithm.
87  svdpp.Apply(data, implicitDenseData, rank, w, h, p, q, y);
88  }
89 
96  double GetRating(const size_t user, const size_t item) const
97  {
98  // Iterate through each item which the user interacted with to calculate
99  // user vector.
100  arma::vec userVec(h.n_rows, arma::fill::zeros);
101  arma::sp_mat::const_iterator it = implicitData.begin_col(user);
102  arma::sp_mat::const_iterator it_end = implicitData.end_col(user);
103  size_t implicitCount = 0;
104  for (; it != it_end; ++it)
105  {
106  userVec += y.col(it.row());
107  implicitCount += 1;
108  }
109  if (implicitCount != 0)
110  userVec /= std::sqrt(implicitCount);
111  userVec += h.col(user);
112 
113  double rating =
114  arma::as_scalar(w.row(item) * userVec) + p(item) + q(user);
115  return rating;
116  }
117 
124  void GetRatingOfUser(const size_t user, arma::vec& rating) const
125  {
126  // Iterate through each item which the user interacted with to calculate
127  // user vector.
128  arma::vec userVec(h.n_rows, arma::fill::zeros);
129  arma::sp_mat::const_iterator it = implicitData.begin_col(user);
130  arma::sp_mat::const_iterator it_end = implicitData.end_col(user);
131  size_t implicitCount = 0;
132  for (; it != it_end; ++it)
133  {
134  userVec += y.col(it.row());
135  implicitCount += 1;
136  }
137  if (implicitCount != 0)
138  userVec /= std::sqrt(implicitCount);
139  userVec += h.col(user);
140 
141  rating = w * userVec + p + q(user);
142  }
143 
156  template<typename NeighborSearchPolicy>
157  void GetNeighborhood(const arma::Col<size_t>& users,
158  const size_t numUsersForSimilarity,
159  arma::Mat<size_t>& neighborhood,
160  arma::mat& similarities) const
161  {
162  // User latent vectors (matrix H) are used for neighbor search.
163  // Temporarily store feature vector of queried users.
164  arma::mat query(h.n_rows, users.n_elem);
165  // Select feature vectors of queried users.
166  for (size_t i = 0; i < users.n_elem; ++i)
167  query.col(i) = h.col(users(i));
168 
169  NeighborSearchPolicy neighborSearch(h);
170  neighborSearch.Search(
171  query, numUsersForSimilarity, neighborhood, similarities);
172  }
173 
175  const arma::mat& W() const { return w; }
177  const arma::mat& H() const { return h; }
179  const arma::vec& Q() const { return q; }
181  const arma::vec& P() const { return p; }
183  const arma::mat& Y() const { return y; }
185  const arma::sp_mat& ImplicitData() const { return implicitData; }
186 
188  size_t MaxIterations() const { return maxIterations; }
190  size_t& MaxIterations() { return maxIterations; }
191 
193  double Alpha() const { return alpha; }
195  double& Alpha() { return alpha; }
196 
198  double Lambda() const { return lambda; }
200  double& Lambda() { return lambda; }
201 
205  template<typename Archive>
206  void serialize(Archive& ar, const uint32_t /* version */)
207  {
208  ar(CEREAL_NVP(maxIterations));
209  ar(CEREAL_NVP(alpha));
210  ar(CEREAL_NVP(lambda));
211  ar(CEREAL_NVP(w));
212  ar(CEREAL_NVP(h));
213  ar(CEREAL_NVP(p));
214  ar(CEREAL_NVP(q));
215  ar(CEREAL_NVP(y));
216  ar(CEREAL_NVP(implicitData));
217  }
218 
219  private:
221  size_t maxIterations;
223  double alpha;
225  double lambda;
227  arma::mat w;
229  arma::mat h;
231  arma::vec p;
233  arma::vec q;
235  arma::mat y;
237  arma::sp_mat implicitData;
238 };
239 
240 } // namespace cf
241 } // namespace mlpack
242 
243 #endif
SVD++ is a matrix decomposition tenique used in collaborative filtering.
Definition: svdplusplus.hpp:76
double GetRating(const size_t user, const size_t item) const
Return predicted rating given user ID and item ID.
size_t & MaxIterations()
Modify the number of iterations.
Linear algebra utility functions, generally performed on matrices or vectors.
static void CleanData(const arma::mat &implicitData, arma::sp_mat &cleanedData, const arma::mat &data)
Converts the User, Item matrix of implicit data to Item-User Table.
void GetRatingOfUser(const size_t user, arma::vec &rating) const
Get predicted ratings for a user.
const arma::mat & H() const
Get the User Matrix.
void serialize(Archive &ar, const uint32_t)
Serialization.
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 svdplusplus.
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Lambda() const
Get regularization parameter.
const arma::vec & Q() const
Get the User Bias Vector.
const arma::sp_mat & ImplicitData() const
Get Implicit Feedback Data.
size_t MaxIterations() const
Get the number of iterations.
const arma::mat & W() const
Get the Item Matrix.
Implementation of the SVDPlusPlus policy to act as a wrapper when accessing SVDPlusPlus from within C...
const arma::vec & P() const
Get the Item Bias Vector.
double & Alpha()
Modify learning rate.
double Alpha() const
Get learning rate.
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.
SVDPlusPlusPolicy(const size_t maxIterations=10, const double alpha=0.001, const double lambda=0.1)
Use SVDPlusPlus method to perform collaborative filtering.
void Apply(const arma::mat &data, const arma::mat &implicitData, const size_t rank, arma::mat &u, arma::mat &v, arma::vec &p, arma::vec &q, arma::mat &y)
Trains the model and obtains user/item matrices, user/item bias, and item implicit matrix...
double & Lambda()
Modify regularization parameter.
const arma::mat & Y() const
Get the Item Implicit Matrix.