mlpack  2.0.1
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 
44  template<typename MatType>
45  inline static void Initialize(const MatType& V,
46  const size_t r,
47  arma::mat& W,
48  arma::mat& H)
49  {
50  const size_t n = V.n_rows;
51  const size_t m = V.n_cols;
52 
53  double avgV = 0;
54  size_t count = 0;
55  double min = DBL_MAX;
56 
57  // Iterate over all elements in the matrix (for sparse matrices, this only
58  // iterates over nonzeros).
59  for (typename MatType::const_row_col_iterator it = V.begin();
60  it != V.end(); ++it)
61  {
62  ++count;
63  avgV += *it;
64  // Track the minimum value.
65  if (*it < min)
66  min = *it;
67  }
68 
69  avgV = sqrt(((avgV / (n * m)) - min) / r);
70 
71  // Initialize to random values.
72  W.randu(n, r);
73  H.randu(r, m);
74 
75  W = W + avgV;
76  H = H + avgV;
77  }
78 
80  template<typename Archive>
81  void Serialize(Archive& /* ar */, const unsigned int /* version */) { }
82 };
83 
84 } // namespace amf
85 } // namespace mlpack
86 
87 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
This initialization rule initializes matrix W and H to root of the average of V, perturbed with unifo...
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
void Serialize(Archive &, const unsigned int)
Serialize the object (in this case, there is nothing to do).
static void Initialize(const MatType &V, const size_t r, arma::mat &W, arma::mat &H)
Initialize the matrices W and H to the average value of V with uniform random noise added...