mlpack  1.0.12
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 
31 {
32  public:
35 
36  template<typename MatType>
37  void Initialize(const MatType& dataset, const size_t rank)
38  {
39  (void)dataset;
40  (void)rank;
41  }
42 
55  template<typename MatType>
56  inline static void WUpdate(const MatType& V,
57  arma::mat& W,
58  const arma::mat& H)
59  {
60  // The call to inv() sometimes fails; so we are using the psuedoinverse.
61  // W = (inv(H * H.t()) * H * V.t()).t();
62  W = V * H.t() * pinv(H * H.t());
63 
64  // Set all negative numbers to machine epsilon
65  for (size_t i = 0; i < W.n_elem; i++)
66  {
67  if (W(i) < 0.0)
68  {
69  W(i) = 0.0;
70  }
71  }
72  }
73 
86  template<typename MatType>
87  inline static void HUpdate(const MatType& V,
88  const arma::mat& W,
89  arma::mat& H)
90  {
91  H = pinv(W.t() * W) * W.t() * V;
92 
93  // Set all negative numbers to 0.
94  for (size_t i = 0; i < H.n_elem; i++)
95  {
96  if (H(i) < 0.0)
97  {
98  H(i) = 0.0;
99  }
100  }
101  }
102 }; // class NMFALSUpdate
103 
104 }; // namespace amf
105 }; // namespace mlpack
106 
107 #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:56
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
void Initialize(const MatType &dataset, const size_t rank)
Definition: nmf_als.hpp:37
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:87
This class implements a method titled 'Alternating Least Squares' described in the paper 'Positive Ma...
Definition: nmf_als.hpp:30
NMFALSUpdate()
Empty constructor required for the UpdateRule template.
Definition: nmf_als.hpp:34