MLPACK  1.0.7
als_update_rules.hpp
Go to the documentation of this file.
1 
28 #ifndef __MLPACK_METHODS_NMF_ALS_UPDATE_RULES_HPP
29 #define __MLPACK_METHODS_NMF_ALS_UPDATE_RULES_HPP
30 
31 #include <mlpack/core.hpp>
32 
33 namespace mlpack {
34 namespace nmf {
35 
43 {
44  public:
45  // Empty constructor required for the WUpdateRule template.
47 
56  template<typename MatType>
57  inline static void Update(const MatType& V,
58  arma::mat& W,
59  const arma::mat& H)
60  {
61  // The call to inv() sometimes fails; so we are using the psuedoinverse.
62  // W = (inv(H * H.t()) * H * V.t()).t();
63  W = V * H.t() * pinv(H * H.t());
64 
65  // Set all negative numbers to machine epsilon
66  for (size_t i = 0; i < W.n_elem; i++)
67  {
68  if (W(i) < 0.0)
69  {
70  W(i) = 0.0;
71  }
72  }
73  }
74 };
75 
83 {
84  public:
85  // Empty constructor required for the HUpdateRule template.
87 
96  template<typename MatType>
97  inline static void Update(const MatType& V,
98  const arma::mat& W,
99  arma::mat& H)
100  {
101  H = pinv(W.t() * W) * W.t() * V;
102 
103  // Set all negative numbers to 0.
104  for (size_t i = 0; i < H.n_elem; i++)
105  {
106  if (H(i) < 0.0)
107  {
108  H(i) = 0.0;
109  }
110  }
111  }
112 };
113 
114 }; // namespace nmf
115 }; // namespace mlpack
116 
117 #endif