mlpack  1.0.12
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 
35 {
36  public:
37  // Empty constructor required for the WUpdateRule template.
39 
40  template<typename MatType>
41  void Initialize(const MatType& dataset, const size_t rank)
42  {
43  (void)dataset;
44  (void)rank;
45  }
46 
60  template<typename MatType>
61  inline static void WUpdate(const MatType& V,
62  arma::mat& W,
63  const arma::mat& H)
64  {
65  // Simple implementation left in the header file.
66  arma::mat t1;
67  arma::rowvec t2;
68 
69  t1 = W * H;
70  for (size_t i = 0; i < W.n_rows; ++i)
71  {
72  for (size_t j = 0; j < W.n_cols; ++j)
73  {
74  // Writing this as a single expression does not work as of Armadillo
75  // 3.920. This should be fixed in a future release, and then the code
76  // below can be fixed.
77  //t2 = H.row(j) % V.row(i) / t1.row(i);
78  t2.set_size(H.n_cols);
79  for (size_t k = 0; k < t2.n_elem; ++k)
80  {
81  t2(k) = H(j, k) * V(i, k) / t1(i, k);
82  }
83 
84  W(i, j) = W(i, j) * sum(t2) / sum(H.row(j));
85  }
86  }
87  }
88 
102  template<typename MatType>
103  inline static void HUpdate(const MatType& V,
104  const arma::mat& W,
105  arma::mat& H)
106  {
107  // Simple implementation left in the header file.
108  arma::mat t1;
109  arma::colvec t2;
110 
111  t1 = W * H;
112  for (size_t i = 0; i < H.n_rows; i++)
113  {
114  for (size_t j = 0; j < H.n_cols; j++)
115  {
116  // Writing this as a single expression does not work as of Armadillo
117  // 3.920. This should be fixed in a future release, and then the code
118  // below can be fixed.
119  //t2 = W.col(i) % V.col(j) / t1.col(j);
120  t2.set_size(W.n_rows);
121  for (size_t k = 0; k < t2.n_elem; ++k)
122  {
123  t2(k) = W(k, i) * V(k, j) / t1(k, j);
124  }
125 
126  H(i,j) = H(i,j) * sum(t2) / sum(W.col(i));
127  }
128  }
129  }
130 };
131 
132 }; // namespace amf
133 }; // namespace mlpack
134 
135 #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.
Definition: load.hpp:23
void Initialize(const MatType &dataset, const size_t rank)
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 Matrix Factorization' by D...