mlpack  1.0.12
simple_residue_termination.hpp
Go to the documentation of this file.
1 
14 #ifndef _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
15 #define _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
16 
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 namespace amf {
21 
34 {
35  public:
44  SimpleResidueTermination(const double minResidue = 1e-10,
45  const size_t maxIterations = 10000)
47 
53  template<typename MatType>
54  void Initialize(const MatType& V)
55  {
56  // Initialize the things we keep track of.
57  residue = DBL_MAX;
58  iteration = 1;
59  nm = V.n_rows * V.n_cols;
60  // Remove history.
61  normOld = 0;
62  }
63 
70  bool IsConverged(arma::mat& W, arma::mat& H)
71  {
72  // Calculate the norm and compute the residue
73  const double norm = arma::norm(W * H, "fro");
74  residue = fabs(normOld - norm) / normOld;
75 
76  // Store the norm.
77  normOld = norm;
78 
79  // Increment iteration count
80  iteration++;
81 
82  // Check if termination criterion is met.
83  return (residue < minResidue || iteration > maxIterations);
84  }
85 
87  const double& Index() const { return residue; }
88 
90  const size_t& Iteration() const { return iteration; }
91 
93  const size_t& MaxIterations() const { return maxIterations; }
94  size_t& MaxIterations() { return maxIterations; }
95 
97  const double& MinResidue() const { return minResidue; }
98  double& MinResidue() { return minResidue; }
99 
100 public:
102  double minResidue;
105 
107  double residue;
109  size_t iteration;
111  double normOld;
112 
113  size_t nm;
114 }; // class SimpleResidueTermination
115 
116 }; // namespace amf
117 }; // namespace mlpack
118 
119 
120 #endif // _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
const double & Index() const
Get current value of residue.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
const double & MinResidue() const
Access minimum residue value.
This class implements a simple residue-based termination policy.
const size_t & Iteration() const
Get current iteration count.
bool IsConverged(arma::mat &W, arma::mat &H)
Check if termination criterion is met.
const size_t & MaxIterations() const
Access max iteration count.
SimpleResidueTermination(const double minResidue=1e-10, const size_t maxIterations=10000)
Construct the SimpleResidueTermination object with the given minimum residue (or the default) and the...
void Initialize(const MatType &V)
Initializes the termination policy before stating the factorization.