mlpack  1.0.12
random_acol_init.hpp
Go to the documentation of this file.
1 
14 #ifndef __MLPACK_METHODS_LMF_RANDOM_ACOL_INIT_HPP
15 #define __MLPACK_METHODS_LMF_RANDOM_ACOL_INIT_HPP
16 
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 namespace amf {
21 
31 template<int p = 5>
33 {
34  public:
35  // Empty constructor required for the InitializeRule template
37  { }
38 
39  template<typename MatType>
40  inline static void Initialize(const MatType& V,
41  const size_t r,
42  arma::mat& W,
43  arma::mat& H)
44  {
45  const size_t n = V.n_rows;
46  const size_t m = V.n_cols;
47 
48  if (p > m)
49  {
50  Log::Warn << "Number of random columns is more than the number of columns"
51  << "available in the V matrix; weird results may ensue!" << std::endl;
52  }
53 
54  W.zeros(n, r);
55 
56  // Initialize W matrix with random columns.
57  for (size_t col = 0; col < r; col++)
58  {
59  for (size_t randCol = 0; randCol < p; randCol++)
60  {
61  // .col() does not work in this case, as of Armadillo 3.920.
62  W.unsafe_col(col) += V.col(math::RandInt(0, m));
63  }
64  }
65 
66  // Now divide by p.
67  W /= p;
68 
69  // Initialize H to random values.
70  H.randu(r, m);
71  }
72 }; // Class RandomAcolInitialization
73 
74 }; // namespace amf
75 }; // namespace mlpack
76 
77 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
static void Initialize(const MatType &V, const size_t r, arma::mat &W, arma::mat &H)
static util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:84
This class initializes the W matrix of the AMF algorithm by averaging p randomly chosen columns of V...
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:98