bias_svd_method.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_BIAS_SVD_METHOD_HPP
15 #define MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_BIAS_SVD_METHOD_HPP
16 
17 #include <mlpack/prereqs.hpp>
19 
20 namespace mlpack {
21 namespace cf {
22 
42 {
43  public:
51  BiasSVDPolicy(const size_t maxIterations = 10,
52  const double alpha = 0.02,
53  const double lambda = 0.05) :
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  // Perform decomposition using the bias SVD algorithm.
81  svd::BiasSVD<> biassvd(maxIterations, alpha, lambda);
82  biassvd.Apply(data, rank, w, h, p, q);
83  }
84 
91  double GetRating(const size_t user, const size_t item) const
92  {
93  double rating =
94  arma::as_scalar(w.row(item) * h.col(user)) + p(item) + q(user);
95  return rating;
96  }
97 
104  void GetRatingOfUser(const size_t user, arma::vec& rating) const
105  {
106  rating = w * h.col(user) + p + q(user);
107  }
108 
121  template<typename NeighborSearchPolicy>
122  void GetNeighborhood(const arma::Col<size_t>& users,
123  const size_t numUsersForSimilarity,
124  arma::Mat<size_t>& neighborhood,
125  arma::mat& similarities) const
126  {
127  // User latent vectors (matrix H) are used for neighbor search.
128  // Temporarily store feature vector of queried users.
129  arma::mat query(h.n_rows, users.n_elem);
130  // Select feature vectors of queried users.
131  for (size_t i = 0; i < users.n_elem; ++i)
132  query.col(i) = h.col(users(i));
133 
134  NeighborSearchPolicy neighborSearch(h);
135  neighborSearch.Search(
136  query, numUsersForSimilarity, neighborhood, similarities);
137  }
138 
140  const arma::mat& W() const { return w; }
142  const arma::mat& H() const { return h; }
144  const arma::vec& Q() const { return q; }
146  const arma::vec& P() const { return p; }
147 
149  size_t MaxIterations() const { return maxIterations; }
151  size_t& MaxIterations() { return maxIterations; }
152 
154  double Alpha() const { return alpha; }
156  double& Alpha() { return alpha; }
157 
159  double Lambda() const { return lambda; }
161  double& Lambda() { return lambda; }
162 
166  template<typename Archive>
167  void serialize(Archive& ar, const uint32_t /* version */)
168  {
169  ar(CEREAL_NVP(maxIterations));
170  ar(CEREAL_NVP(alpha));
171  ar(CEREAL_NVP(lambda));
172  ar(CEREAL_NVP(w));
173  ar(CEREAL_NVP(h));
174  ar(CEREAL_NVP(p));
175  ar(CEREAL_NVP(q));
176  }
177 
178  private:
180  size_t maxIterations;
182  double alpha;
184  double lambda;
186  arma::mat w;
188  arma::mat h;
190  arma::vec p;
192  arma::vec q;
193 };
194 
195 } // namespace cf
196 } // namespace mlpack
197 
198 #endif
double GetRating(const size_t user, const size_t item) const
Return predicted rating given user ID and item ID.
double & Lambda()
Modify regularization parameter.
double Lambda() const
Get regularization parameter.
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 bias SVD.
Bias SVD is an improvement on Regularized SVD which is a matrix factorization techniques.
Definition: bias_svd.hpp:57
void Apply(const arma::mat &data, const size_t rank, arma::mat &u, arma::mat &v, arma::vec &p, arma::vec &q)
Trains the model and obtains user/item matrices and user/item bias.
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.
Linear algebra utility functions, generally performed on matrices or vectors.
const arma::vec & P() const
Get the Item Bias Vector.
The core includes that mlpack expects; standard C++ includes and Armadillo.
const arma::vec & Q() const
Get the User Bias Vector.
const arma::mat & W() const
Get the Item Matrix.
size_t MaxIterations() const
Get the number of iterations.
size_t & MaxIterations()
Modify the number of iterations.
void serialize(Archive &ar, const uint32_t)
Serialization.
BiasSVDPolicy(const size_t maxIterations=10, const double alpha=0.02, const double lambda=0.05)
Use Bias SVD method to perform collaborative filtering.
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.
Implementation of the Bias SVD policy to act as a wrapper when accessing Bias SVD from within CFType...
double Alpha() const
Get learning rate.
double & Alpha()
Modify learning rate.