mlpack  1.0.12
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 
103  // if residue tolerance is not satisfied
104  if ((residueOld - residue) / residueOld < tolerance && iteration > 4)
105  {
106  // check if this is a first of successive drops
107  if (reverseStepCount == 0 && isCopy == false)
108  {
109  // store a copy of W and H matrix
110  isCopy = true;
111  this->W = W;
112  this->H = H;
113  // store residue values
114  c_index = residue;
116  }
117  // increase successive drop count
119  }
120  // if tolerance is satisfied
121  else
122  {
123  // initialize successive drop count
124  reverseStepCount = 0;
125  // if residue is droped below minimum scrap stored values
126  if(residue <= c_indexOld && isCopy == true)
127  {
128  isCopy = false;
129  }
130  }
131 
132  // check if termination criterion is met
134  {
135  // if stored values are present replace them with current value as they
136  // represent the minimum residue point
137  if(isCopy)
138  {
139  W = this->W;
140  H = this->H;
141  residue = c_index;
142  }
143  return true;
144  }
145  else return false;
146  }
147 
149  const double& Index() const { return residue; }
150 
152  const size_t& Iteration() const { return iteration; }
153 
155  const size_t& MaxIterations() const { return maxIterations; }
156  size_t& MaxIterations() { return maxIterations; }
157 
159  const double& Tolerance() const { return tolerance; }
160  double& Tolerance() { return tolerance; }
161 
162  private:
164  double tolerance;
167 
169  const MatType* V;
170 
172  size_t iteration;
173 
175  double residueOld;
176  double residue;
177  double normOld;
178 
183 
186  bool isCopy;
187 
189  arma::mat W;
190  arma::mat H;
191  double c_indexOld;
192  double c_index;
193 }; // class SimpleToleranceTermination
194 
195 }; // namespace amf
196 }; // namespace mlpack
197 
198 #endif // _MLPACK_METHODS_AMF_SIMPLE_TOLERANCE_TERMINATION_HPP_INCLUDED
199 
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
const size_t & Iteration() const
Get current iteration count.
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
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.
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.
const double & Tolerance() const
Access tolerance value.
const size_t & MaxIterations() const
Access upper limit of iteration count.