mlpack  2.0.1
nmf_mult_div.hpp
Go to the documentation of this file.
1 
14 #ifndef __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_MULT_DIV_HPP
15 #define __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_MULT_DIV_HPP
16 
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 namespace amf {
21 
51 {
52  public:
53  // Empty constructor required for the WUpdateRule template.
55 
60  template<typename MatType>
61  void Initialize(const MatType& /* dataset */, const size_t /* rank */)
62  {
63  // Nothing to do.
64  }
65 
81  template<typename MatType>
82  inline static void WUpdate(const MatType& V,
83  arma::mat& W,
84  const arma::mat& H)
85  {
86  // Simple implementation left in the header file.
87  arma::mat t1;
88  arma::rowvec t2;
89 
90  t1 = W * H;
91  for (size_t i = 0; i < W.n_rows; ++i)
92  {
93  for (size_t j = 0; j < W.n_cols; ++j)
94  {
95  // Writing this as a single expression does not work as of Armadillo
96  // 3.920. This should be fixed in a future release, and then the code
97  // below can be fixed.
98  //t2 = H.row(j) % V.row(i) / t1.row(i);
99  t2.set_size(H.n_cols);
100  for (size_t k = 0; k < t2.n_elem; ++k)
101  {
102  t2(k) = H(j, k) * V(i, k) / t1(i, k);
103  }
104 
105  W(i, j) = W(i, j) * sum(t2) / sum(H.row(j));
106  }
107  }
108  }
109 
125  template<typename MatType>
126  inline static void HUpdate(const MatType& V,
127  const arma::mat& W,
128  arma::mat& H)
129  {
130  // Simple implementation left in the header file.
131  arma::mat t1;
132  arma::colvec t2;
133 
134  t1 = W * H;
135  for (size_t i = 0; i < H.n_rows; i++)
136  {
137  for (size_t j = 0; j < H.n_cols; j++)
138  {
139  // Writing this as a single expression does not work as of Armadillo
140  // 3.920. This should be fixed in a future release, and then the code
141  // below can be fixed.
142  //t2 = W.col(i) % V.col(j) / t1.col(j);
143  t2.set_size(W.n_rows);
144  for (size_t k = 0; k < t2.n_elem; ++k)
145  {
146  t2(k) = W(k, i) * V(k, j) / t1(k, j);
147  }
148 
149  H(i,j) = H(i,j) * sum(t2) / sum(W.col(i));
150  }
151  }
152  }
153 
155  template<typename Archive>
156  void Serialize(Archive& /* ar */, const unsigned int /* version */) { }
157 };
158 
159 } // namespace amf
160 } // namespace mlpack
161 
162 #endif
static void WUpdate(const MatType &V, arma::mat &W, const arma::mat &H)
The update rule for the basis matrix W.
Linear algebra utility functions, generally performed on matrices or vectors.
void Serialize(Archive &, const unsigned int)
Serialize the object (in this case, there is nothing to serialize).
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
void Initialize(const MatType &, const size_t)
Initialize the factorization.
static void HUpdate(const MatType &V, const arma::mat &W, arma::mat &H)
The update rule for the encoding matrix H.
This follows a method described in the paper 'Algorithms for Non-negative.