mlpack 3.4.2
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
17namespace mlpack {
18namespace amf {
19
32{
33 public:
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 = 0;
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 // If maxIterations == 0, there is no iteration limit.
87 }
88
90 const double& Index() const { return residue; }
91
93 const size_t& Iteration() const { return iteration; }
94
96 const size_t& MaxIterations() const { return maxIterations; }
97 size_t& MaxIterations() { return maxIterations; }
98
100 const double& MinResidue() const { return minResidue; }
101 double& MinResidue() { return minResidue; }
102
103 public:
108
110 double residue;
112 size_t iteration;
114 double normOld;
115
116 size_t nm;
117}; // class SimpleResidueTermination
118
119} // namespace amf
120} // namespace mlpack
121
122
123#endif // _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
This class implements a simple residue-based termination policy.
bool IsConverged(arma::mat &W, arma::mat &H)
Check if termination criterion is met.
const double & Index() const
Get current value of residue.
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...
void Initialize(const MatType &V)
Initializes the termination policy before stating the factorization.
const size_t & Iteration() const
Get current iteration count.
const double & MinResidue() const
Access minimum residue value.
const size_t & MaxIterations() const
Access max iteration count.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.