[mlpack] 18/44: Merge r17387.

Barak A. Pearlmutter barak+git at pearlmutter.net
Mon Feb 15 19:35:53 UTC 2016


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to tag mlpack-1.0.11
in repository mlpack.

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

    Merge r17387.
---
 .../simple_residue_termination.hpp                 | 65 ++++++++++++++++------
 1 file changed, 48 insertions(+), 17 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;
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list