mlpack  master
simple_residue_termination.hpp
Go to the documentation of this file.
1 
12 #ifndef _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
13 #define _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace amf {
19 
32 {
33  public:
42  SimpleResidueTermination(const double minResidue = 1e-5,
43  const size_t maxIterations = 10000)
45 
51  template<typename MatType>
52  void Initialize(const MatType& V)
53  {
54  // Initialize the things we keep track of.
55  residue = DBL_MAX;
56  iteration = 1;
57  nm = V.n_rows * V.n_cols;
58  // Remove history.
59  normOld = 0;
60  }
61 
68  bool IsConverged(arma::mat& W, arma::mat& H)
69  {
70  // Calculate the norm and compute the residue, but do it by hand, so as to
71  // avoid calculating (W*H), which may be very large.
72  double norm = 0.0;
73  for (size_t j = 0; j < H.n_cols; ++j)
74  norm += arma::norm(W * H.col(j), "fro");
75  residue = fabs(normOld - norm) / normOld;
76 
77  // Store the norm.
78  normOld = norm;
79 
80  // Increment iteration count
81  iteration++;
82  Log::Info << "Iteration " << iteration << "; residue " << residue << ".\n";
83 
84  // Check if termination criterion is met.
85  return (residue < minResidue || iteration > maxIterations);
86  }
87 
89  const double& Index() const { return residue; }
90 
92  const size_t& Iteration() const { return iteration; }
93 
95  const size_t& MaxIterations() const { return maxIterations; }
96  size_t& MaxIterations() { return maxIterations; }
97 
99  const double& MinResidue() const { return minResidue; }
100  double& MinResidue() { return minResidue; }
101 
102 public:
104  double minResidue;
107 
109  double residue;
111  size_t iteration;
113  double normOld;
114 
115  size_t nm;
116 }; // class SimpleResidueTermination
117 
118 } // namespace amf
119 } // namespace mlpack
120 
121 
122 #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: binarize.hpp:18
The core includes that mlpack expects; standard C++ includes and Armadillo.
const double & MinResidue() const
Access minimum residue value.
SimpleResidueTermination(const double minResidue=1e-5, const size_t maxIterations=10000)
Construct the SimpleResidueTermination object with the given minimum residue (or the default) and the...
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.
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
const size_t & MaxIterations() const
Access max iteration count.
void Initialize(const MatType &V)
Initializes the termination policy before stating the factorization.