mlpack  2.0.1
simple_tolerance_termination.hpp
Go to the documentation of this file.
1 
14 #ifndef _MLPACK_METHODS_AMF_SIMPLE_TOLERANCE_TERMINATION_HPP_INCLUDED
15 #define _MLPACK_METHODS_AMF_SIMPLE_TOLERANCE_TERMINATION_HPP_INCLUDED
16 
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 namespace amf {
21 
32 template <class MatType>
34 {
35  public:
38  const size_t maxIterations = 10000,
39  const size_t reverseStepTolerance = 3)
43 
49  void Initialize(const MatType& V)
50  {
51  residueOld = DBL_MAX;
52  iteration = 1;
53  residue = DBL_MIN;
54  reverseStepCount = 0;
55  isCopy = false;
56 
57  this->V = &V;
58 
59  c_index = 0;
60  c_indexOld = 0;
61 
62  reverseStepCount = 0;
63  }
64 
71  bool IsConverged(arma::mat& W, arma::mat& H)
72  {
73  arma::mat WH;
74 
75  WH = W * H;
76 
77  // compute residue
79  size_t n = V->n_rows;
80  size_t m = V->n_cols;
81  double sum = 0;
82  size_t count = 0;
83  for(size_t i = 0;i < n;i++)
84  {
85  for(size_t j = 0;j < m;j++)
86  {
87  double temp = 0;
88  if((temp = (*V)(i,j)) != 0)
89  {
90  temp = (temp - WH(i, j));
91  temp = temp * temp;
92  sum += temp;
93  count++;
94  }
95  }
96  }
97  residue = sum / count;
98  residue = sqrt(residue);
99 
100  // increment iteration count
101  iteration++;
102  Log::Info << "Iteration " << iteration << "; residue "
103  << ((residueOld - residue) / residueOld) << ".\n";
104 
105  // if residue tolerance is not satisfied
106  if ((residueOld - residue) / residueOld < tolerance && iteration > 4)
107  {
108  // check if this is a first of successive drops
109  if (reverseStepCount == 0 && isCopy == false)
110  {
111  // store a copy of W and H matrix
112  isCopy = true;
113  this->W = W;
114  this->H = H;
115  // store residue values
116  c_index = residue;
118  }
119  // increase successive drop count
121  }
122  // if tolerance is satisfied
123  else
124  {
125  // initialize successive drop count
126  reverseStepCount = 0;
127  // if residue is droped below minimum scrap stored values
128  if(residue <= c_indexOld && isCopy == true)
129  {
130  isCopy = false;
131  }
132  }
133 
134  // check if termination criterion is met
136  {
137  // if stored values are present replace them with current value as they
138  // represent the minimum residue point
139  if(isCopy)
140  {
141  W = this->W;
142  H = this->H;
143  residue = c_index;
144  }
145  return true;
146  }
147  else return false;
148  }
149 
151  const double& Index() const { return residue; }
152 
154  const size_t& Iteration() const { return iteration; }
155 
157  const size_t& MaxIterations() const { return maxIterations; }
158  size_t& MaxIterations() { return maxIterations; }
159 
161  const double& Tolerance() const { return tolerance; }
162  double& Tolerance() { return tolerance; }
163 
164  private:
166  double tolerance;
169 
171  const MatType* V;
172 
174  size_t iteration;
175 
177  double residueOld;
178  double residue;
179  double normOld;
180 
185 
188  bool isCopy;
189 
191  arma::mat W;
192  arma::mat H;
193  double c_indexOld;
194  double c_index;
195 }; // class SimpleToleranceTermination
196 
197 } // namespace amf
198 } // namespace mlpack
199 
200 #endif // _MLPACK_METHODS_AMF_SIMPLE_TOLERANCE_TERMINATION_HPP_INCLUDED
201 
const size_t & MaxIterations() const
Access upper limit of iteration count.
Linear algebra utility functions, generally performed on matrices or vectors.
SimpleToleranceTermination(const double tolerance=1e-5, const size_t maxIterations=10000, const size_t reverseStepTolerance=3)
empty constructor
const MatType * V
pointer to matrix being factorized
const double & Tolerance() const
Access tolerance value.
const size_t & Iteration() const
Get current iteration count.
bool isCopy
indicates whether a copy of information is available which corresponds to minimum residue point ...
arma::mat W
variables to store information of minimum residue poi
size_t reverseStepTolerance
tolerance on successive residue drops
const double & Index() const
Get current value of residue.
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...
This class implements residue tolerance termination policy.
void Initialize(const MatType &V)
Initializes the termination policy before stating the factorization.
bool IsConverged(arma::mat &W, arma::mat &H)
Check if termination criterio is met.