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  double min = DBL_MAX;
53 
54  // Iterate over all elements in the matrix (for sparse matrices, this only
55  // iterates over nonzeros).
56  for (typename MatType::const_row_col_iterator it = V.begin();
57  it != V.end(); ++it)
58  {
59  avgV += *it;
60  // Track the minimum value.
61  if (*it < min)
62  min = *it;
63  }
64 
65  avgV = sqrt(((avgV / (n * m)) - min) / r);
66 
67  // Initialize to random values.
68  W.randu(n, r);
69  H.randu(r, m);
70 
71  W += avgV;
72  H += + avgV;
73  }
74 
85  template<typename MatType>
86  inline static void InitializeOne(const MatType& V,
87  const size_t r,
88  arma::mat& M,
89  const bool whichMatrix = true)
90  {
91  const size_t n = V.n_rows;
92  const size_t m = V.n_cols;
93 
94  double avgV = 0;
95  double min = DBL_MAX;
96 
97  // Iterate over all elements in the matrix (for sparse matrices, this only
98  // iterates over nonzeros).
99  for (typename MatType::const_row_col_iterator it = V.begin();
100  it != V.end(); ++it)
101  {
102  avgV += *it;
103  // Track the minimum value.
104  if (*it < min)
105  min = *it;
106  }
107  if (whichMatrix)
108  {
109  // Initialize W to random values
110  M.randu(n, r);
111  }
112  else
113  {
114  // Initialize H to random values
115  M.randu(r, m);
116  }
117  M += sqrt(((avgV / (n * m)) - min) / r);
118  }
119 
121  template<typename Archive>
122  void serialize(Archive& /* ar */, const uint32_t /* version */) { }
123 };
124 
125 } // namespace amf
126 } // namespace mlpack
127 
128 #endif
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...
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
static void InitializeOne(const MatType &V, const size_t r, arma::mat &M, const bool whichMatrix=true)
Initialize the matrix W or H to the average value of V with uniform random noise added.
This initialization rule initializes matrix W and H to root of the average of V, perturbed with unifo...
void serialize(Archive &, const uint32_t)
Serialize the object (in this case, there is nothing to do).