mlpack 3.4.2
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
18namespace mlpack {
19namespace amf {
20
28{
29 public:
30 // Empty constructor required for the InitializeRule template.
31 GivenInitialization() : wIsGiven(false), hIsGiven(false) { }
32
33 // Initialize the GivenInitialization object with the given matrices.
34 GivenInitialization(const arma::mat& w, const arma::mat& h) :
35 w(w), h(h), wIsGiven(true), hIsGiven(true) { }
36
37 // Initialize the GivenInitialization object, taking control of the given
38 // matrices.
39 GivenInitialization(const arma::mat&& w, const arma::mat&& h) :
40 w(std::move(w)),
41 h(std::move(h)),
42 wIsGiven(true),
43 hIsGiven(true)
44 { }
45
46 // Initialize either H or W with the given matrix.
47 GivenInitialization(const arma::mat& m, const bool whichMatrix = true)
48 {
49 if (whichMatrix)
50 {
51 w = m;
52 wIsGiven = true;
53 hIsGiven = false;
54 }
55 else
56 {
57 h = m;
58 wIsGiven = false;
59 hIsGiven = true;
60 }
61 }
62
63 // Initialize either H or W, taking control of the given matrix.
64 GivenInitialization(const arma::mat&& m, const bool whichMatrix = true)
65 {
66 if (whichMatrix)
67 {
68 w = std::move(m);
69 wIsGiven = true;
70 hIsGiven = false;
71 }
72 else
73 {
74 h = std::move(m);
75 wIsGiven = false;
76 hIsGiven = true;
77 }
78 }
79
88 template<typename MatType>
89 inline void Initialize(const MatType& V,
90 const size_t r,
91 arma::mat& W,
92 arma::mat& H)
93 {
94 // Make sure the initial W, H matrices are given
95 if (!wIsGiven)
96 {
97 Log::Fatal << "Initial W matrix is not given!" << std::endl;
98 }
99 if (!hIsGiven)
100 {
101 Log::Fatal << "Initial H matrix is not given!" << std::endl;
102 }
103
104 // Make sure the initial W, H matrices have correct size.
105 if (w.n_rows != V.n_rows)
106 {
107 Log::Fatal << "The number of rows in given W (" << w.n_rows
108 << ") doesn't equal the number of rows in V (" << V.n_rows
109 << ") !" << std::endl;
110 }
111 if (w.n_cols != r)
112 {
113 Log::Fatal << "The number of columns in given W (" << w.n_cols
114 << ") doesn't equal the rank of factorization (" << r
115 << ") !" << std::endl;
116 }
117 if (h.n_cols != V.n_cols)
118 {
119 Log::Fatal << "The number of columns in given H (" << h.n_cols
120 << ") doesn't equal the number of columns in V (" << V.n_cols
121 << ") !" << std::endl;
122 }
123 if (h.n_rows != r)
124 {
125 Log::Fatal << "The number of rows in given H (" << h.n_rows
126 << ") doesn't equal the rank of factorization (" << r
127 << ") !"<< std::endl;
128 }
129
130 // Initialize to the given matrices.
131 W = w;
132 H = h;
133 }
134
143 template<typename MatType>
144 inline void InitializeOne(const MatType& V,
145 const size_t r,
146 arma::mat& M,
147 const bool whichMatrix = true)
148 {
149 if (whichMatrix)
150 {
151 // Make sure the initial W matrix is given.
152 if (!wIsGiven)
153 {
154 Log::Fatal << "Initial W matrix is not given!" << std::endl;
155 }
156
157 // Make sure the initial W matrix has correct size.
158 if (w.n_rows != V.n_rows)
159 {
160 Log::Fatal << "The number of rows in given W (" << w.n_rows
161 << ") doesn't equal the number of rows in V (" << V.n_rows
162 << ") !" << std::endl;
163 }
164 if (w.n_cols != r)
165 {
166 Log::Fatal << "The number of columns in given W (" << w.n_cols
167 << ") doesn't equal the rank of factorization (" << r
168 << ") !" << std::endl;
169 }
170
171 // Initialize W to the given matrix.
172 M = w;
173 }
174 else
175 {
176 // Make sure the initial H matrix is given.
177 if (!hIsGiven)
178 {
179 Log::Fatal << "Initial H matrix is not given!" << std::endl;
180 }
181
182 // Make sure the initial H matrix has correct size.
183 if (h.n_cols != V.n_cols)
184 {
185 Log::Fatal << "The number of columns in given H (" << h.n_cols
186 << ") doesn't equal the number of columns in V (" << V.n_cols
187 << ") !" << std::endl;
188 }
189 if (h.n_rows != r)
190 {
191 Log::Fatal << "The number of rows in given H (" << h.n_rows
192 << ") doesn't equal the rank of factorization (" << r
193 << ") !"<< std::endl;
194 }
195
196 // Initialize H to the given matrix.
197 M = h;
198 }
199 }
200
202 template<typename Archive>
203 void serialize(Archive& ar, const unsigned int /* version */)
204 {
205 ar & BOOST_SERIALIZATION_NVP(w);
206 ar & BOOST_SERIALIZATION_NVP(h);
207 }
208
209 private:
211 arma::mat w;
213 arma::mat h;
215 bool wIsGiven;
217 bool hIsGiven;
218};
219
220} // namespace amf
221} // namespace mlpack
222
223#endif
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:28
GivenInitialization(const arma::mat &w, const arma::mat &h)
Definition: given_init.hpp:34
GivenInitialization(const arma::mat &&m, const bool whichMatrix=true)
Definition: given_init.hpp:64
GivenInitialization(const arma::mat &&w, const arma::mat &&h)
Definition: given_init.hpp:39
void InitializeOne(const MatType &V, const size_t r, arma::mat &M, const bool whichMatrix=true)
Fill W or H with given matrix.
Definition: given_init.hpp:144
GivenInitialization(const arma::mat &m, const bool whichMatrix=true)
Definition: given_init.hpp:47
void serialize(Archive &ar, const unsigned int)
Serialize the object (in this case, there is nothing to serialize).
Definition: given_init.hpp:203
void Initialize(const MatType &V, const size_t r, arma::mat &W, arma::mat &H)
Fill W and H with given matrices.
Definition: given_init.hpp:89
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Definition: prereqs.hpp:67
The core includes that mlpack expects; standard C++ includes and Armadillo.