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) :
46  residue(0.0),
47  iteration(0),
48  normOld(0),
49  nm(0)
50  {
51  // Nothing to do here.
52  }
53 
59  template<typename MatType>
60  void Initialize(const MatType& V)
61  {
62  // Initialize the things we keep track of.
63  residue = DBL_MAX;
64  iteration = 0;
65  nm = V.n_rows * V.n_cols;
66  // Remove history.
67  normOld = 0;
68  }
69 
76  bool IsConverged(arma::mat& W, arma::mat& H)
77  {
78  // Calculate the norm and compute the residue, but do it by hand, so as to
79  // avoid calculating (W*H), which may be very large.
80  double norm = 0.0;
81  for (size_t j = 0; j < H.n_cols; ++j)
82  norm += arma::norm(W * H.col(j), "fro");
83  residue = fabs(normOld - norm) / normOld;
84 
85  // Store the norm.
86  normOld = norm;
87 
88  // Increment iteration count
89  iteration++;
90  Log::Info << "Iteration " << iteration << "; residue " << residue << ".\n";
91 
92  // Check if termination criterion is met.
93  // If maxIterations == 0, there is no iteration limit.
94  return (residue < minResidue || iteration == maxIterations);
95  }
96 
98  const double& Index() const { return residue; }
99 
101  const size_t& Iteration() const { return iteration; }
102 
104  const size_t& MaxIterations() const { return maxIterations; }
105  size_t& MaxIterations() { return maxIterations; }
106 
108  const double& MinResidue() const { return minResidue; }
109  double& MinResidue() { return minResidue; }
110 
111  public:
113  double minResidue;
116 
118  double residue;
120  size_t iteration;
122  double normOld;
123 
124  size_t nm;
125 }; // class SimpleResidueTermination
126 
127 } // namespace amf
128 } // namespace mlpack
129 
130 
131 #endif // _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
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...
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
This class implements a simple residue-based termination policy.
void Initialize(const MatType &V)
Initializes the termination policy before stating the factorization.
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
bool IsConverged(arma::mat &W, arma::mat &H)
Check if termination criterion is met.
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.