MLPACK  1.0.11
nmf_als.hpp
Go to the documentation of this file.
1 
22 #ifndef __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_ALS_HPP
23 #define __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_ALS_HPP
24 
25 #include <mlpack/core.hpp>
26 
27 namespace mlpack {
28 namespace amf {
29 
39 {
40  public:
43 
44  template<typename MatType>
45  void Initialize(const MatType& dataset, const size_t rank)
46  {
47  (void)dataset;
48  (void)rank;
49  }
50 
63  template<typename MatType>
64  inline static void WUpdate(const MatType& V,
65  arma::mat& W,
66  const arma::mat& H)
67  {
68  // The call to inv() sometimes fails; so we are using the psuedoinverse.
69  // W = (inv(H * H.t()) * H * V.t()).t();
70  W = V * H.t() * pinv(H * H.t());
71 
72  // Set all negative numbers to machine epsilon
73  for (size_t i = 0; i < W.n_elem; i++)
74  {
75  if (W(i) < 0.0)
76  {
77  W(i) = 0.0;
78  }
79  }
80  }
81 
94  template<typename MatType>
95  inline static void HUpdate(const MatType& V,
96  const arma::mat& W,
97  arma::mat& H)
98  {
99  H = pinv(W.t() * W) * W.t() * V;
100 
101  // Set all negative numbers to 0.
102  for (size_t i = 0; i < H.n_elem; i++)
103  {
104  if (H(i) < 0.0)
105  {
106  H(i) = 0.0;
107  }
108  }
109  }
110 }; // class NMFALSUpdate
111 
112 }; // namespace amf
113 }; // namespace mlpack
114 
115 #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:64
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:31
void Initialize(const MatType &dataset, const size_t rank)
Definition: nmf_als.hpp:45
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:95
This class implements a method titled 'Alternating Least Squares' described in the paper 'Positive Ma...
Definition: nmf_als.hpp:38
NMFALSUpdate()
Empty constructor required for the UpdateRule template.
Definition: nmf_als.hpp:42