mlpack  2.0.1
nmf_als.hpp
Go to the documentation of this file.
1 
14 #ifndef __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_ALS_HPP
15 #define __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_ALS_HPP
16 
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 namespace amf {
21 
44 {
45  public:
48 
53  template<typename MatType>
54  void Initialize(const MatType& /* dataset */, const size_t /* rank */)
55  {
56  // Nothing to do.
57  }
58 
73  template<typename MatType>
74  inline static void WUpdate(const MatType& V,
75  arma::mat& W,
76  const arma::mat& H)
77  {
78  // The call to inv() sometimes fails; so we are using the psuedoinverse.
79  // W = (inv(H * H.t()) * H * V.t()).t();
80  W = V * H.t() * pinv(H * H.t());
81 
82  // Set all negative numbers to machine epsilon.
83  for (size_t i = 0; i < W.n_elem; i++)
84  {
85  if (W(i) < 0.0)
86  {
87  W(i) = 0.0;
88  }
89  }
90  }
91 
106  template<typename MatType>
107  inline static void HUpdate(const MatType& V,
108  const arma::mat& W,
109  arma::mat& H)
110  {
111  H = pinv(W.t() * W) * W.t() * V;
112 
113  // Set all negative numbers to 0.
114  for (size_t i = 0; i < H.n_elem; i++)
115  {
116  if (H(i) < 0.0)
117  {
118  H(i) = 0.0;
119  }
120  }
121  }
122 
124  template<typename Archive>
125  void Serialize(Archive& /* ar */, const unsigned int /* version */) { }
126 }; // class NMFALSUpdate
127 
128 } // namespace amf
129 } // namespace mlpack
130 
131 #endif
static void WUpdate(const MatType &V, arma::mat &W, const arma::mat &H)
The update rule for the basis matrix W.
Definition: nmf_als.hpp:74
Linear algebra utility functions, generally performed on matrices or vectors.
void Initialize(const MatType &, const size_t)
Set initial values for the factorization.
Definition: nmf_als.hpp:54
static void HUpdate(const MatType &V, const arma::mat &W, arma::mat &H)
The update rule for the encoding matrix H.
Definition: nmf_als.hpp:107
This class implements a method titled 'Alternating Least Squares' described in the following paper: ...
Definition: nmf_als.hpp:43
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
NMFALSUpdate()
Empty constructor required for the UpdateRule template.
Definition: nmf_als.hpp:47
void Serialize(Archive &, const unsigned int)
Serialize the object (in this case, there is nothing to serialize).
Definition: nmf_als.hpp:125