MLPACK  1.0.10
simple_residue_termination.hpp
Go to the documentation of this file.
1 
20 #ifndef _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
21 #define _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
22 
23 #include <mlpack/core.hpp>
24 
25 namespace mlpack {
26 namespace amf {
27 
29 {
30  public:
31  SimpleResidueTermination(const double minResidue = 1e-10,
32  const size_t maxIterations = 10000)
34 
35  template<typename MatType>
36  void Initialize(const MatType& V)
37  {
39  iteration = 1;
40  normOld = 0;
41 
42  const size_t n = V.n_rows;
43  const size_t m = V.n_cols;
44 
45  nm = n * m;
46  }
47 
48  bool IsConverged(arma::mat& W, arma::mat& H)
49  {
50  // Calculate norm of WH after each iteration.
51  arma::mat WH;
52 
53  WH = W * H;
54  double norm = sqrt(accu(WH % WH) / nm);
55 
56  if (iteration != 0)
57  {
58  residue = fabs(normOld - norm);
59  residue /= normOld;
60  }
61 
62  normOld = norm;
63 
64  iteration++;
65 
66  if(residue < minResidue || iteration > maxIterations) return true;
67  else return false;
68  }
69 
70  const double& Index() { return residue; }
71  const size_t& Iteration() { return iteration; }
72  const size_t& MaxIterations() { return maxIterations; }
73 
74 public:
75  double minResidue;
76  size_t maxIterations;
77 
78  double residue;
79  size_t iteration;
80  double normOld;
81 
82  size_t nm;
83 }; // class SimpleResidueTermination
84 
85 }; // namespace amf
86 }; // namespace mlpack
87 
88 
89 #endif // _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
bool IsConverged(arma::mat &W, arma::mat &H)
SimpleResidueTermination(const double minResidue=1e-10, const size_t maxIterations=10000)