mlpack 3.4.2
svd_complete_method.hpp
Go to the documentation of this file.
1
14#ifndef MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_SVD_COMPLETE_METHOD_HPP
15#define MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_SVD_COMPLETE_METHOD_HPP
16
17#include <mlpack/prereqs.hpp>
22
23namespace mlpack {
24namespace cf {
25
45{
46 public:
59 template<typename MatType>
60 void Apply(const MatType& /* data */,
61 const arma::sp_mat& cleanedData,
62 const size_t rank,
63 const size_t maxIterations,
64 const double minResidue,
65 const bool mit)
66 {
67 if (mit)
68 {
69 amf::MaxIterationTermination iter(maxIterations);
70
71 // Do singular value decomposition using complete incremental method
72 // using cleaned data in form of sparse matrix.
75
76 svdci.Apply(cleanedData, rank, w, h);
77 }
78 else
79 {
80 amf::SimpleResidueTermination srt(minResidue, maxIterations);
81
82 // Do singular value decomposition using complete incremental method
83 // using cleaned data in form of sparse matrix.
85
86 svdci.Apply(cleanedData, rank, w, h);
87 }
88 }
89
96 double GetRating(const size_t user, const size_t item) const
97 {
98 double rating = arma::as_scalar(w.row(item) * h.col(user));
99 return rating;
100 }
101
108 void GetRatingOfUser(const size_t user, arma::vec& rating) const
109 {
110 rating = w * h.col(user);
111 }
112
125 template<typename NeighborSearchPolicy>
126 void GetNeighborhood(const arma::Col<size_t>& users,
127 const size_t numUsersForSimilarity,
128 arma::Mat<size_t>& neighborhood,
129 arma::mat& similarities) const
130 {
131 // We want to avoid calculating the full rating matrix, so we will do
132 // nearest neighbor search only on the H matrix, using the observation that
133 // if the rating matrix X = W*H, then d(X.col(i), X.col(j)) = d(W H.col(i),
134 // W H.col(j)). This can be seen as nearest neighbor search on the H
135 // matrix with the Mahalanobis distance where M^{-1} = W^T W. So, we'll
136 // decompose M^{-1} = L L^T (the Cholesky decomposition), and then multiply
137 // H by L^T. Then we can perform nearest neighbor search.
138 arma::mat l = arma::chol(w.t() * w);
139 arma::mat stretchedH = l * h; // Due to the Armadillo API, l is L^T.
140
141 // Temporarily store feature vector of queried users.
142 arma::mat query(stretchedH.n_rows, users.n_elem);
143 // Select feature vectors of queried users.
144 for (size_t i = 0; i < users.n_elem; ++i)
145 query.col(i) = stretchedH.col(users(i));
146
147 NeighborSearchPolicy neighborSearch(stretchedH);
148 neighborSearch.Search(
149 query, numUsersForSimilarity, neighborhood, similarities);
150 }
151
153 const arma::mat& W() const { return w; }
155 const arma::mat& H() const { return h; }
156
160 template<typename Archive>
161 void serialize(Archive& ar, const unsigned int /* version */)
162 {
163 ar & BOOST_SERIALIZATION_NVP(w);
164 ar & BOOST_SERIALIZATION_NVP(h);
165 }
166
167 private:
169 arma::mat w;
171 arma::mat h;
172};
173
174} // namespace cf
175} // namespace mlpack
176
177#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
TODO : Merge this template specialized function for sparse matrix using common row_col_iterator.
This class implements a simple residue-based termination policy.
Implementation of the SVD complete incremental policy to act as a wrapper when accessing SVD complete...
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 SVD complete incremental policy.
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.