mlpack  2.0.1
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-5,
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, but do it by hand, so as to
73  // avoid calculating (W*H), which may be very large.
74  double norm = 0.0;
75  for (size_t j = 0; j < H.n_cols; ++j)
76  norm += arma::norm(W * H.col(j), "fro");
77  residue = fabs(normOld - norm) / normOld;
78 
79  // Store the norm.
80  normOld = norm;
81 
82  // Increment iteration count
83  iteration++;
84  Log::Info << "Iteration " << iteration << "; residue " << residue << ".\n";
85 
86  // Check if termination criterion is met.
87  return (residue < minResidue || iteration > maxIterations);
88  }
89 
91  const double& Index() const { return residue; }
92 
94  const size_t& Iteration() const { return iteration; }
95 
97  const size_t& MaxIterations() const { return maxIterations; }
98  size_t& MaxIterations() { return maxIterations; }
99 
101  const double& MinResidue() const { return minResidue; }
102  double& MinResidue() { return minResidue; }
103 
104 public:
106  double minResidue;
109 
111  double residue;
113  size_t iteration;
115  double normOld;
116 
117  size_t nm;
118 }; // class SimpleResidueTermination
119 
120 } // namespace amf
121 } // namespace mlpack
122 
123 
124 #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.
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.
static util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:81
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
bool IsConverged(arma::mat &W, arma::mat &H)
Check if termination criterion is met.
const size_t & MaxIterations() const
Access max iteration count.
void Initialize(const MatType &V)
Initializes the termination policy before stating the factorization.