MLPACK  1.0.11
average_init.hpp
Go to the documentation of this file.
1 
22 #ifndef __MLPACK_METHODS_AMF_AVERAGE_INIT_HPP
23 #define __MLPACK_METHODS_AMF_AVERAGE_INIT_HPP
24 
25 #include <mlpack/core.hpp>
26 
27 namespace mlpack {
28 namespace amf {
29 
38 {
39  public:
40  // Empty constructor required for the InitializeRule template
42 
43  template<typename MatType>
44  inline static void Initialize(const MatType& V,
45  const size_t r,
46  arma::mat& W,
47  arma::mat& H)
48  {
49  size_t n = V.n_rows;
50  size_t m = V.n_cols;
51 
52  double V_avg = 0;
53  size_t count = 0;
54  double min = DBL_MAX;
55  for(typename MatType::const_row_col_iterator it = V.begin();it != V.end();it++)
56  {
57  if(*it != 0)
58  {
59  count++;
60  V_avg += *it;
61  if(*it < min) min = *it;
62  }
63  }
64  V_avg = sqrt(((V_avg / (n * m)) - min) / r);
65 
66  // Intialize to random values.
67  W.randu(n, r);
68  H.randu(r, m);
69 
70  W = W + V_avg;
71  H = H + V_avg;
72  }
73 };
74 
75 }; // namespace amf
76 }; // namespace mlpack
77 
78 #endif