random_acol_init.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_LMF_RANDOM_ACOL_INIT_HPP
13 #define MLPACK_METHODS_LMF_RANDOM_ACOL_INIT_HPP
14 
15 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace amf {
20 
43 template<size_t columnsToAverage = 5>
45 {
46  public:
47  // Empty constructor required for the InitializeRule template
49  { }
50 
51  template<typename MatType>
52  inline static void Initialize(const MatType& V,
53  const size_t r,
54  arma::mat& W,
55  arma::mat& H)
56  {
57  const size_t n = V.n_rows;
58  const size_t m = V.n_cols;
59 
60  if (columnsToAverage > m)
61  {
62  Log::Warn << "Number of random columns (columnsToAverage) is more than "
63  << "the number of columns available in the V matrix; weird results "
64  << "may ensue!" << std::endl;
65  }
66 
67  W.zeros(n, r);
68 
69  // Initialize W matrix with random columns.
70  for (size_t col = 0; col < r; col++)
71  {
72  for (size_t randCol = 0; randCol < columnsToAverage; randCol++)
73  {
74  // .col() does not work in this case, as of Armadillo 3.920.
75  W.unsafe_col(col) += V.col(math::RandInt(0, m));
76  }
77  }
78 
79  // Now divide by p.
80  W /= columnsToAverage;
81 
82  // Initialize H to random values.
83  H.randu(r, m);
84  }
85 
87  template<typename Archive>
88  void serialize(Archive& /* ar */, const unsigned int /* version */) { }
89 };
90 
91 } // namespace amf
92 } // namespace mlpack
93 
94 #endif
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
Miscellaneous math random-related routines.
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:105
static MLPACK_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:87
static void Initialize(const MatType &V, const size_t r, arma::mat &W, arma::mat &H)
void serialize(Archive &, const unsigned int)
Serialize the object (in this case, there is nothing to serialize).