mlpack 3.4.2
nmf_als.hpp
Go to the documentation of this file.
1
12#ifndef MLPACK_METHODS_LMF_UPDATE_RULES_NMF_ALS_HPP
13#define MLPACK_METHODS_LMF_UPDATE_RULES_NMF_ALS_HPP
14
15#include <mlpack/prereqs.hpp>
16
17namespace mlpack {
18namespace amf {
19
42{
43 public:
46
51 template<typename MatType>
52 void Initialize(const MatType& /* dataset */, const size_t /* rank */)
53 {
54 // Nothing to do.
55 }
56
71 template<typename MatType>
72 inline static void WUpdate(const MatType& V,
73 arma::mat& W,
74 const arma::mat& H)
75 {
76 // The call to inv() sometimes fails; so we are using the psuedoinverse.
77 // W = (inv(H * H.t()) * H * V.t()).t();
78 W = V * H.t() * pinv(H * H.t());
79
80 // Set all negative numbers to machine epsilon.
81 for (size_t i = 0; i < W.n_elem; ++i)
82 {
83 if (W(i) < 0.0)
84 {
85 W(i) = 0.0;
86 }
87 }
88 }
89
104 template<typename MatType>
105 inline static void HUpdate(const MatType& V,
106 const arma::mat& W,
107 arma::mat& H)
108 {
109 H = pinv(W.t() * W) * W.t() * V;
110
111 // Set all negative numbers to 0.
112 for (size_t i = 0; i < H.n_elem; ++i)
113 {
114 if (H(i) < 0.0)
115 {
116 H(i) = 0.0;
117 }
118 }
119 }
120
122 template<typename Archive>
123 void serialize(Archive& /* ar */, const unsigned int /* version */) { }
124}; // class NMFALSUpdate
125
126} // namespace amf
127} // namespace mlpack
128
129#endif
This class implements a method titled 'Alternating Least Squares' described in the following paper:
Definition: nmf_als.hpp:42
NMFALSUpdate()
Empty constructor required for the UpdateRule template.
Definition: nmf_als.hpp:45
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:72
void serialize(Archive &, const unsigned int)
Serialize the object (in this case, there is nothing to serialize).
Definition: nmf_als.hpp:123
void Initialize(const MatType &, const size_t)
Set initial values for the factorization.
Definition: nmf_als.hpp:52
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:105
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.