mlpack  1.0.12
average_init.hpp
Go to the documentation of this file.
1 
14 #ifndef __MLPACK_METHODS_AMF_AVERAGE_INIT_HPP
15 #define __MLPACK_METHODS_AMF_AVERAGE_INIT_HPP
16 
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 namespace amf {
21 
30 {
31  public:
32  // Empty constructor required for the InitializeRule template
34 
35  template<typename MatType>
36  inline static void Initialize(const MatType& V,
37  const size_t r,
38  arma::mat& W,
39  arma::mat& H)
40  {
41  size_t n = V.n_rows;
42  size_t m = V.n_cols;
43 
44  double V_avg = 0;
45  size_t count = 0;
46  double min = DBL_MAX;
47  for(typename MatType::const_row_col_iterator it = V.begin();it != V.end();it++)
48  {
49  if(*it != 0)
50  {
51  count++;
52  V_avg += *it;
53  if(*it < min) min = *it;
54  }
55  }
56  V_avg = sqrt(((V_avg / (n * m)) - min) / r);
57 
58  // Intialize to random values.
59  W.randu(n, r);
60  H.randu(r, m);
61 
62  W = W + V_avg;
63  H = H + V_avg;
64  }
65 };
66 
67 }; // namespace amf
68 }; // namespace mlpack
69 
70 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
This initialization rule initializes matrix W and H to root of average of V with uniform noise...
static void Initialize(const MatType &V, const size_t r, arma::mat &W, arma::mat &H)