MLPACK  1.0.10
nmf_als.hpp
Go to the documentation of this file.
1 
28 #ifndef __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_ALS_HPP
29 #define __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_ALS_HPP
30 
31 #include <mlpack/core.hpp>
32 
33 namespace mlpack {
34 namespace amf {
35 
40 {
41  public:
42  // Empty constructor required for the UpdateRule template.
44 
45  template<typename MatType>
46  void Initialize(const MatType& dataset, const size_t rank)
47  {
48  (void)dataset;
49  (void)rank;
50  }
51 
64  template<typename MatType>
65  inline static void WUpdate(const MatType& V,
66  arma::mat& W,
67  const arma::mat& H)
68  {
69  // The call to inv() sometimes fails; so we are using the psuedoinverse.
70  // W = (inv(H * H.t()) * H * V.t()).t();
71  W = V * H.t() * pinv(H * H.t());
72 
73  // Set all negative numbers to machine epsilon
74  for (size_t i = 0; i < W.n_elem; i++)
75  {
76  if (W(i) < 0.0)
77  {
78  W(i) = 0.0;
79  }
80  }
81  }
82 
95  template<typename MatType>
96  inline static void HUpdate(const MatType& V,
97  const arma::mat& W,
98  arma::mat& H)
99  {
100  H = pinv(W.t() * W) * W.t() * V;
101 
102  // Set all negative numbers to 0.
103  for (size_t i = 0; i < H.n_elem; i++)
104  {
105  if (H(i) < 0.0)
106  {
107  H(i) = 0.0;
108  }
109  }
110  }
111 };
112 
113 }; // namespace amf
114 }; // namespace mlpack
115 
116 #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:65
void Initialize(const MatType &dataset, const size_t rank)
Definition: nmf_als.hpp:46
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:96
The alternating least square update rules of matrices W and H.
Definition: nmf_als.hpp:39