MLPACK  1.0.11
nmf_mult_div.hpp
Go to the documentation of this file.
1 
22 #ifndef __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_MULT_DIV_HPP
23 #define __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_MULT_DIV_HPP
24 
25 #include <mlpack/core.hpp>
26 
27 namespace mlpack {
28 namespace amf {
29 
43 {
44  public:
45  // Empty constructor required for the WUpdateRule template.
47 
48  template<typename MatType>
49  void Initialize(const MatType& dataset, const size_t rank)
50  {
51  (void)dataset;
52  (void)rank;
53  }
54 
68  template<typename MatType>
69  inline static void WUpdate(const MatType& V,
70  arma::mat& W,
71  const arma::mat& H)
72  {
73  // Simple implementation left in the header file.
74  arma::mat t1;
75  arma::rowvec t2;
76 
77  t1 = W * H;
78  for (size_t i = 0; i < W.n_rows; ++i)
79  {
80  for (size_t j = 0; j < W.n_cols; ++j)
81  {
82  // Writing this as a single expression does not work as of Armadillo
83  // 3.920. This should be fixed in a future release, and then the code
84  // below can be fixed.
85  //t2 = H.row(j) % V.row(i) / t1.row(i);
86  t2.set_size(H.n_cols);
87  for (size_t k = 0; k < t2.n_elem; ++k)
88  {
89  t2(k) = H(j, k) * V(i, k) / t1(i, k);
90  }
91 
92  W(i, j) = W(i, j) * sum(t2) / sum(H.row(j));
93  }
94  }
95  }
96 
110  template<typename MatType>
111  inline static void HUpdate(const MatType& V,
112  const arma::mat& W,
113  arma::mat& H)
114  {
115  // Simple implementation left in the header file.
116  arma::mat t1;
117  arma::colvec t2;
118 
119  t1 = W * H;
120  for (size_t i = 0; i < H.n_rows; i++)
121  {
122  for (size_t j = 0; j < H.n_cols; j++)
123  {
124  // Writing this as a single expression does not work as of Armadillo
125  // 3.920. This should be fixed in a future release, and then the code
126  // below can be fixed.
127  //t2 = W.col(i) % V.col(j) / t1.col(j);
128  t2.set_size(W.n_rows);
129  for (size_t k = 0; k < t2.n_elem; ++k)
130  {
131  t2(k) = W(k, i) * V(k, j) / t1(k, j);
132  }
133 
134  H(i,j) = H(i,j) * sum(t2) / sum(W.col(i));
135  }
136  }
137  }
138 };
139 
140 }; // namespace amf
141 }; // namespace mlpack
142 
143 #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:31
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 &#39;Algorithms for Non-negative Matrix Factorization&#39; by D...