MLPACK  1.0.8
mult_div_update_rules.hpp
Go to the documentation of this file.
1 
28 #ifndef __MLPACK_METHODS_NMF_MULT_DIV_UPDATE_RULES_HPP
29 #define __MLPACK_METHODS_NMF_MULT_DIV_UPDATE_RULES_HPP
30 
31 #include <mlpack/core.hpp>
32 
33 namespace mlpack {
34 namespace nmf {
35 
44 {
45  public:
46  // Empty constructor required for the WUpdateRule template.
48 
57  template<typename MatType>
58  inline static void Update(const MatType& V,
59  arma::mat& W,
60  const arma::mat& H)
61  {
62  // Simple implementation left in the header file.
63  arma::mat t1;
64  arma::rowvec t2;
65 
66  t1 = W * H;
67  for (size_t i = 0; i < W.n_rows; ++i)
68  {
69  for (size_t j = 0; j < W.n_cols; ++j)
70  {
71  // Writing this as a single expression does not work as of Armadillo
72  // 3.920. This should be fixed in a future release, and then the code
73  // below can be fixed.
74  //t2 = H.row(j) % V.row(i) / t1.row(i);
75  t2.set_size(H.n_cols);
76  for (size_t k = 0; k < t2.n_elem; ++k)
77  {
78  t2(k) = H(j, k) * V(i, k) / t1(i, k);
79  }
80 
81  W(i, j) = W(i, j) * sum(t2) / sum(H.row(j));
82  }
83  }
84  }
85 };
86 
95 {
96  public:
97  // Empty constructor required for the HUpdateRule template.
99 
108  template<typename MatType>
109  inline static void Update(const MatType& V,
110  const arma::mat& W,
111  arma::mat& H)
112  {
113  // Simple implementation left in the header file.
114  arma::mat t1;
115  arma::colvec t2;
116 
117  t1 = W * H;
118  for (size_t i = 0; i < H.n_rows; i++)
119  {
120  for (size_t j = 0; j < H.n_cols; j++)
121  {
122  // Writing this as a single expression does not work as of Armadillo
123  // 3.920. This should be fixed in a future release, and then the code
124  // below can be fixed.
125  //t2 = W.col(i) % V.col(j) / t1.col(j);
126  t2.set_size(W.n_rows);
127  for (size_t k = 0; k < t2.n_elem; ++k)
128  {
129  t2(k) = W(k, i) * V(k, j) / t1(k, j);
130  }
131 
132  H(i,j) = H(i,j) * sum(t2) / sum(W.col(i));
133  }
134  }
135  }
136 };
137 
138 }; // namespace nmf
139 }; // namespace mlpack
140 
141 #endif