given_init.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_AMF_INIT_RULES_GIVEN_INIT_HPP
14 #define MLPACK_METHODS_AMF_INIT_RULES_GIVEN_INIT_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace amf {
20 
28 {
29  public:
30  // Empty constructor required for the InitializeRule template.
32 
33  // Initialize the GivenInitialization object with the given matrices.
34  GivenInitialization(const arma::mat& w, const arma::mat& h) : w(w), h(h) { }
35 
36  // Initialize the GivenInitialization object, taking control of the given
37  // matrices.
38  GivenInitialization(const arma::mat&& w, const arma::mat&& h) :
39  w(std::move(w)),
40  h(std::move(h))
41  { }
42 
51  template<typename MatType>
52  inline void Initialize(const MatType& V,
53  const size_t r,
54  arma::mat& W,
55  arma::mat& H)
56  {
57  // Make sure the initial W, H matrices have correct size.
58  if (w.n_rows != V.n_rows)
59  {
60  Log::Fatal << "The number of rows in given W (" << w.n_rows
61  << ") doesn't equal the number of rows in V (" << V.n_rows
62  << ") !" << std::endl;
63  }
64  if (w.n_cols != r)
65  {
66  Log::Fatal << "The number of columns in given W (" << w.n_cols
67  << ") doesn't equal the rank of factorization (" << r
68  << ") !" << std::endl;
69  }
70  if (h.n_cols != V.n_cols)
71  {
72  Log::Fatal << "The number of columns in given H (" << h.n_cols
73  << ") doesn't equal the number of columns in V (" << V.n_cols
74  << ") !" << std::endl;
75  }
76  if (h.n_rows != r)
77  {
78  Log::Fatal << "The number of rows in given H (" << h.n_rows
79  << ") doesn't equal the rank of factorization (" << r
80  << ") !"<< std::endl;
81  }
82 
83  // Initialize to the given matrices.
84  W = w;
85  H = h;
86  }
87 
89  template<typename Archive>
90  void serialize(Archive& ar, const unsigned int /* version */)
91  {
92  ar & BOOST_SERIALIZATION_NVP(w);
93  ar & BOOST_SERIALIZATION_NVP(h);
94  }
95 
96  private:
98  arma::mat w;
100  arma::mat h;
101 };
102 
103 } // namespace amf
104 } // namespace mlpack
105 
106 #endif
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
Definition: prereqs.hpp:55
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
This initialization rule for AMF simply fills the W and H matrices with the matrices given to the con...
Definition: given_init.hpp:27
void Initialize(const MatType &V, const size_t r, arma::mat &W, arma::mat &H)
Fill W and H with random uniform noise.
Definition: given_init.hpp:52
GivenInitialization(const arma::mat &&w, const arma::mat &&h)
Definition: given_init.hpp:38
GivenInitialization(const arma::mat &w, const arma::mat &h)
Definition: given_init.hpp:34
void serialize(Archive &ar, const unsigned int)
Serialize the object (in this case, there is nothing to serialize).
Definition: given_init.hpp:90