mlpack  master
average_init.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_AMF_AVERAGE_INIT_HPP
13 #define MLPACK_METHODS_AMF_AVERAGE_INIT_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace amf {
19 
28 {
29  public:
30  // Empty constructor required for the InitializeRule template
32 
42  template<typename MatType>
43  inline static void Initialize(const MatType& V,
44  const size_t r,
45  arma::mat& W,
46  arma::mat& H)
47  {
48  const size_t n = V.n_rows;
49  const size_t m = V.n_cols;
50 
51  double avgV = 0;
52  size_t count = 0;
53  double min = DBL_MAX;
54 
55  // Iterate over all elements in the matrix (for sparse matrices, this only
56  // iterates over nonzeros).
57  for (typename MatType::const_row_col_iterator it = V.begin();
58  it != V.end(); ++it)
59  {
60  ++count;
61  avgV += *it;
62  // Track the minimum value.
63  if (*it < min)
64  min = *it;
65  }
66 
67  avgV = sqrt(((avgV / (n * m)) - min) / r);
68 
69  // Initialize to random values.
70  W.randu(n, r);
71  H.randu(r, m);
72 
73  W = W + avgV;
74  H = H + avgV;
75  }
76 
78  template<typename Archive>
79  void Serialize(Archive& /* ar */, const unsigned int /* version */) { }
80 };
81 
82 } // namespace amf
83 } // namespace mlpack
84 
85 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
The core includes that mlpack expects; standard C++ includes and Armadillo.
This initialization rule initializes matrix W and H to root of the average of V, perturbed with unifo...
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...