[mlpack-git] mlpack-1.0.x: Merge r17387. (2376cd1)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Wed Jan 7 11:58:06 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : mlpack-1.0.x
Link       : https://github.com/mlpack/mlpack/compare/0000000000000000000000000000000000000000...904762495c039e345beba14c1142fd719b3bd50e

>---------------------------------------------------------------

commit 2376cd1b20157e881ce0be75ef58f8e523beae51
Author: Ryan Curtin <ryan at ratml.org>
Date:   Sun Dec 7 19:36:41 2014 +0000

    Merge r17387.


>---------------------------------------------------------------

2376cd1b20157e881ce0be75ef58f8e523beae51
 .../simple_residue_termination.hpp                 | 63 ++++++++++++++++------
 1 file changed, 47 insertions(+), 16 deletions(-)

diff --git a/src/mlpack/methods/amf/termination_policies/simple_residue_termination.hpp b/src/mlpack/methods/amf/termination_policies/simple_residue_termination.hpp
index ea21c77..bffe8ea 100644
--- a/src/mlpack/methods/amf/termination_policies/simple_residue_termination.hpp
+++ b/src/mlpack/methods/amf/termination_policies/simple_residue_termination.hpp
@@ -25,17 +25,37 @@
 namespace mlpack {
 namespace amf {
 
+/**
+ * This class implements a simple residue-based termination policy. The
+ * termination decision depends on two factors: the value of the residue (the
+ * difference between the norm of WH this iteration and the previous iteration),
+ * and the number of iterations.  If the current value of residue drops below
+ * the threshold or the number of iterations goes above the iteration limit,
+ * IsConverged() will return true.  This class is meant for use with the AMF
+ * (alternating matrix factorization) class.
+ *
+ * @see AMF
+ */
 class SimpleResidueTermination
 {
  public:
+  /**
+   * Construct the SimpleResidueTermination object with the given minimum
+   * residue (or the default) and the given maximum number of iterations (or the
+   * default).  0 indicates no iteration limit.
+   *
+   * @param minResidue Minimum residue for termination.
+   * @param maxIterations Maximum number of iterations.
+   */
   SimpleResidueTermination(const double minResidue = 1e-10,
                            const size_t maxIterations = 10000)
-        : minResidue(minResidue), maxIterations(maxIterations) { }
+      : minResidue(minResidue), maxIterations(maxIterations) { }
 
   template<typename MatType>
   void Initialize(const MatType& V)
   {
-    residue = minResidue;
+    // Initialize the things we keep track of.
+    residue = DBL_MAX;
     iteration = 1;
     normOld = 0;
 
@@ -45,33 +65,44 @@ class SimpleResidueTermination
     nm = n * m;
   }
 
+  /**
+   * Check if termination criterion is met.
+   *
+   * @param W Basis matrix of output.
+   * @param H Encoding matrix of output.
+   */
   bool IsConverged(arma::mat& W, arma::mat& H)
   {
-    // Calculate norm of WH after each iteration.
-    arma::mat WH;
-
-    WH = W * H;
-    double norm = sqrt(accu(WH % WH) / nm);
-
-    if (iteration != 0)
-    {
-      residue = fabs(normOld - norm);
-      residue /= normOld;
-    }
+    // Calculate the norm and compute the residue
+    const double norm = arma::norm(W * H, "fro");
+    residue = fabs(normOld - norm) / normOld;
 
+    // Store the norm.
     normOld = norm;
 
+    // Increment iteration count
     iteration++;
 
-    if(residue < minResidue || iteration > maxIterations) return true;
-    else return false;
+    // Check if termination criterion is met.
+    return (residue < minResidue || iteration > maxIterations);
   }
 
   const double& Index() { return residue; }
   const size_t& Iteration() { return iteration; }
   const size_t& MaxIterations() { return maxIterations; }
 
-public:
+  //! Get current iteration count
+  const size_t& Iteration() const { return iteration; }
+
+  //! Access max iteration count
+  const size_t& MaxIterations() const { return maxIterations; }
+  size_t& MaxIterations() { return maxIterations; }
+
+  //! Access minimum residue value
+  const double& MinResidue() const { return minResidue; }
+  double& MinResidue() { return minResidue; }
+
+ public:
   double minResidue;
   size_t maxIterations;
 



More information about the mlpack-git mailing list