mlpack  2.0.1
svd_incomplete_incremental_learning.hpp
Go to the documentation of this file.
1 
14 #ifndef __MLPACK_METHODS_AMF_SVD_INCOMPLETE_INCREMENTAL_LEARNING_HPP
15 #define __MLPACK_METHODS_AMF_SVD_INCOMPLETE_INCREMENTAL_LEARNING_HPP
16 
17 namespace mlpack
18 {
19 namespace amf
20 {
21 
46 {
47  public:
56  double kw = 0,
57  double kh = 0)
58  : u(u), kw(kw), kh(kh)
59  {
60  // Nothing to do.
61  }
62 
71  template<typename MatType>
72  void Initialize(const MatType& /* dataset */, const size_t /* rank */)
73  {
74  // Set the current user to 0.
75  currentUserIndex = 0;
76  }
77 
87  template<typename MatType>
88  inline void WUpdate(const MatType& V,
89  arma::mat& W,
90  const arma::mat& H)
91  {
92  arma::mat deltaW;
93  deltaW.zeros(V.n_rows, W.n_cols);
94 
95  // Iterate through all the rating by this user to update corresponding item
96  // feature feature vector.
97  for (size_t i = 0; i < V.n_rows; ++i)
98  {
99  const double val = V(i, currentUserIndex);
100  // Update only if the rating is non-zero.
101  if (val != 0)
102  deltaW.row(i) += (val - arma::dot(W.row(i), H.col(currentUserIndex))) *
103  H.col(currentUserIndex).t();
104  // Add regularization.
105  if (kw != 0)
106  deltaW.row(i) -= kw * W.row(i);
107  }
108 
109  W += u * deltaW;
110  }
111 
120  template<typename MatType>
121  inline void HUpdate(const MatType& V,
122  const arma::mat& W,
123  arma::mat& H)
124  {
125  arma::vec deltaH;
126  deltaH.zeros(H.n_rows);
127 
128  // Iterate through all the rating by this user to update corresponding item
129  // feature feature vector.
130  for (size_t i = 0; i < V.n_rows; ++i)
131  {
132  const double val = V(i, currentUserIndex);
133  // Update only if the rating is non-zero.
134  if (val != 0)
135  deltaH += (val - arma::dot(W.row(i), H.col(currentUserIndex))) *
136  W.row(i).t();
137  }
138  // Add regularization.
139  if (kh != 0)
140  deltaH -= kh * H.col(currentUserIndex);
141 
142  // Update H matrix and move on to the next user.
143  H.col(currentUserIndex++) += u * deltaH;
144  currentUserIndex = currentUserIndex % V.n_cols;
145  }
146 
147  private:
149  double u;
151  double kw;
153  double kh;
154 
157 };
158 
161 
163 template<>
164 inline void SVDIncompleteIncrementalLearning::
165  WUpdate<arma::sp_mat>(const arma::sp_mat& V,
166  arma::mat& W,
167  const arma::mat& H)
168 {
169  arma::mat deltaW(V.n_rows, W.n_cols);
170  deltaW.zeros();
171  for(arma::sp_mat::const_iterator it = V.begin_col(currentUserIndex);
172  it != V.end_col(currentUserIndex);it++)
173  {
174  double val = *it;
175  size_t i = it.row();
176  deltaW.row(i) += (val - arma::dot(W.row(i), H.col(currentUserIndex))) *
177  arma::trans(H.col(currentUserIndex));
178  if(kw != 0) deltaW.row(i) -= kw * W.row(i);
179  }
180 
181  W += u*deltaW;
182 }
183 
184 template<>
185 inline void SVDIncompleteIncrementalLearning::
186  HUpdate<arma::sp_mat>(const arma::sp_mat& V,
187  const arma::mat& W,
188  arma::mat& H)
189 {
190  arma::mat deltaH(H.n_rows, 1);
191  deltaH.zeros();
192 
193  for(arma::sp_mat::const_iterator it = V.begin_col(currentUserIndex);
194  it != V.end_col(currentUserIndex);it++)
195  {
196  double val = *it;
197  size_t i = it.row();
198  if((val = V(i, currentUserIndex)) != 0)
199  deltaH += (val - arma::dot(W.row(i), H.col(currentUserIndex))) *
200  arma::trans(W.row(i));
201  }
202  if(kh != 0) deltaH -= kh * H.col(currentUserIndex);
203 
204  H.col(currentUserIndex++) += u * deltaH;
205  currentUserIndex = currentUserIndex % V.n_cols;
206 }
207 
208 } // namepsace amf
209 } // namespace mlpack
210 
211 #endif
This class computes SVD using incomplete incremental batch learning, as described in the following pa...
Linear algebra utility functions, generally performed on matrices or vectors.
void WUpdate(const MatType &V, arma::mat &W, const arma::mat &H)
The update rule for the basis matrix W.
void HUpdate(const MatType &V, const arma::mat &W, arma::mat &H)
The update rule for the encoding matrix H.
void Initialize(const MatType &, const size_t)
Initialize parameters before factorization.
SVDIncompleteIncrementalLearning(double u=0.001, double kw=0, double kh=0)
Initialize the parameters of SVDIncompleteIncrementalLearning.