MLPACK  1.0.11
simple_residue_termination.hpp
Go to the documentation of this file.
1 
22 #ifndef _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
23 #define _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
24 
25 #include <mlpack/core.hpp>
26 
27 namespace mlpack {
28 namespace amf {
29 
42 {
43  public:
52  SimpleResidueTermination(const double minResidue = 1e-10,
53  const size_t maxIterations = 10000)
55 
61  template<typename MatType>
62  void Initialize(const MatType& V)
63  {
64  // Initialize the things we keep track of.
65  residue = DBL_MAX;
66  iteration = 1;
67  nm = V.n_rows * V.n_cols;
68  // Remove history.
69  normOld = 0;
70  }
71 
78  bool IsConverged(arma::mat& W, arma::mat& H)
79  {
80  // Calculate the norm and compute the residue
81  const double norm = arma::norm(W * H, "fro");
82  residue = fabs(normOld - norm) / normOld;
83 
84  // Store the norm.
85  normOld = norm;
86 
87  // Increment iteration count
88  iteration++;
89 
90  // Check if termination criterion is met.
91  return (residue < minResidue || iteration > maxIterations);
92  }
93 
95  const double& Index() const { return residue; }
96 
98  const size_t& Iteration() const { return iteration; }
99 
101  const size_t& MaxIterations() const { return maxIterations; }
102  size_t& MaxIterations() { return maxIterations; }
103 
105  const double& MinResidue() const { return minResidue; }
106  double& MinResidue() { return minResidue; }
107 
108 public:
110  double minResidue;
113 
115  double residue;
117  size_t iteration;
119  double normOld;
120 
121  size_t nm;
122 }; // class SimpleResidueTermination
123 
124 }; // namespace amf
125 }; // namespace mlpack
126 
127 
128 #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:31
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.