mlpack 3.4.2
batch_svd_method.hpp
Go to the documentation of this file.
1
13#ifndef MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_BATCH_SVD_METHOD_HPP
14#define MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_BATCH_SVD_METHOD_HPP
15
16#include <mlpack/prereqs.hpp>
21
22namespace mlpack {
23namespace cf {
24
44{
45 public:
58 template<typename MatType>
59 void Apply(const MatType& /* data */,
60 const arma::sp_mat& cleanedData,
61 const size_t rank,
62 const size_t maxIterations,
63 const double minResidue,
64 const bool mit)
65 {
66 if (mit)
67 {
68 amf::MaxIterationTermination iter(maxIterations);
69
70 // Do singular value decomposition using the batch SVD algorithm.
72 amf::SVDBatchLearning> svdbatch(iter);
73
74 svdbatch.Apply(cleanedData, rank, w, h);
75 }
76 else
77 {
78 amf::SimpleResidueTermination srt(minResidue, maxIterations);
79
80 // Do singular value decomposition using the batch SVD algorithm.
81 amf::SVDBatchFactorizer<> svdbatch(srt);
82
83 svdbatch.Apply(cleanedData, rank, w, h);
84 }
85 }
86
93 double GetRating(const size_t user, const size_t item) const
94 {
95 double rating = arma::as_scalar(w.row(item) * h.col(user));
96 return rating;
97 }
98
105 void GetRatingOfUser(const size_t user, arma::vec& rating) const
106 {
107 rating = w * h.col(user);
108 }
109
122 template<typename NeighborSearchPolicy>
123 void GetNeighborhood(const arma::Col<size_t>& users,
124 const size_t numUsersForSimilarity,
125 arma::Mat<size_t>& neighborhood,
126 arma::mat& similarities) const
127 {
128 // We want to avoid calculating the full rating matrix, so we will do
129 // nearest neighbor search only on the H matrix, using the observation that
130 // if the rating matrix X = W*H, then d(X.col(i), X.col(j)) = d(W H.col(i),
131 // W H.col(j)). This can be seen as nearest neighbor search on the H
132 // matrix with the Mahalanobis distance where M^{-1} = W^T W. So, we'll
133 // decompose M^{-1} = L L^T (the Cholesky decomposition), and then multiply
134 // H by L^T. Then we can perform nearest neighbor search.
135 arma::mat l = arma::chol(w.t() * w);
136 arma::mat stretchedH = l * h; // Due to the Armadillo API, l is L^T.
137
138 // Temporarily store feature vector of queried users.
139 arma::mat query(stretchedH.n_rows, users.n_elem);
140 // Select feature vectors of queried users.
141 for (size_t i = 0; i < users.n_elem; ++i)
142 query.col(i) = stretchedH.col(users(i));
143
144 NeighborSearchPolicy neighborSearch(stretchedH);
145 neighborSearch.Search(
146 query, numUsersForSimilarity, neighborhood, similarities);
147 }
148
150 const arma::mat& W() const { return w; }
152 const arma::mat& H() const { return h; }
153
157 template<typename Archive>
158 void serialize(Archive& ar, const unsigned int /* version */)
159 {
160 ar & BOOST_SERIALIZATION_NVP(w);
161 ar & BOOST_SERIALIZATION_NVP(h);
162 }
163
164 private:
166 arma::mat w;
168 arma::mat h;
169};
170
171} // namespace cf
172} // namespace mlpack
173
174#endif
This class implements AMF (alternating matrix factorization) on the given matrix V.
Definition: amf.hpp:79
double Apply(const MatType &V, const size_t r, arma::mat &W, arma::mat &H)
Apply Alternating Matrix Factorization to the provided matrix.
This termination policy only terminates when the maximum number of iterations has been reached.
This initialization rule for AMF simply fills the W and H matrices with uniform random noise in [0,...
Definition: random_init.hpp:26
This class implements SVD batch learning with momentum.
This class implements a simple residue-based termination policy.
Implementation of the Batch SVD policy to act as a wrapper when accessing Batch SVD from within CFTyp...
double GetRating(const size_t user, const size_t item) const
Return predicted rating given user ID and item ID.
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.
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 data set using the batch SVD method.
const arma::mat & W() const
Get the Item Matrix.
const arma::mat & H() const
Get the User Matrix.
void GetRatingOfUser(const size_t user, arma::vec &rating) const
Get predicted ratings for a user.
void serialize(Archive &ar, const unsigned int)
Serialization.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.