[mlpack-git] master: Code cleanup; better documentation. (a526a31)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 22:15:52 EST 2015


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

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

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

commit a526a31d00a9a8f883d80b734a4ca6064425d290
Author: Ryan Curtin <ryan at ratml.org>
Date:   Mon Feb 9 11:41:17 2015 -0500

    Code cleanup; better documentation.


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

a526a31d00a9a8f883d80b734a4ca6064425d290
 src/mlpack/methods/amf/init_rules/average_init.hpp | 53 ++++++++++++++--------
 1 file changed, 33 insertions(+), 20 deletions(-)

diff --git a/src/mlpack/methods/amf/init_rules/average_init.hpp b/src/mlpack/methods/amf/init_rules/average_init.hpp
index cf01ce4..7737668 100644
--- a/src/mlpack/methods/amf/init_rules/average_init.hpp
+++ b/src/mlpack/methods/amf/init_rules/average_init.hpp
@@ -13,11 +13,11 @@ namespace mlpack {
 namespace amf {
 
 /**
- * This initialization rule initializes matrix W and H to root of average of V 
- * with uniform noise. Uniform noise is generated by Armadillo's 'randu' function.
- * To have a better effect lower bound of the matrix is subtracted from average
- * before dividing it by the factorization rank. This computed value is added 
- * with the random noise.
+ * This initialization rule initializes matrix W and H to root of the average of
+ * V, perturbed with uniform noise. Uniform noise is generated by Armadillo's
+ * 'randu' function.  For better performance, the lowest element of the matrix
+ * is subtracted from the average before dividing it by the factorization rank.
+ * This computed value is added with the random noise.
  */
 class AverageInitialization
 {
@@ -25,39 +25,52 @@ class AverageInitialization
   // Empty constructor required for the InitializeRule template
   AverageInitialization() { }
 
+  /**
+   * Initialize the matrices W and H to the average value of V with uniform
+   * random noise added.
+   */
+   * @param V Input matrix.
+   * @param r Rank of matrix.
+   * @param W W matrix, to be initialized.
+   * @param H H matrix, to be initialized.
+   */
   template<typename MatType>
   inline static void Initialize(const MatType& V,
                                 const size_t r,
                                 arma::mat& W,
                                 arma::mat& H)
   {
-    size_t n = V.n_rows;
-    size_t m = V.n_cols;
+    const size_t n = V.n_rows;
+    const size_t m = V.n_cols;
 
-    double V_avg = 0;
+    double avgV = 0;
     size_t count = 0;
     double min = DBL_MAX;
-    for(typename MatType::const_row_col_iterator it = V.begin();it != V.end();it++)
+
+    // Iterate over all elements in the matrix (for sparse matrices, this only
+    // iterates over nonzeros).
+    for (typename MatType::const_row_col_iterator it = V.begin();
+        it != V.end(); ++it)
     {
-      if(*it != 0)
-      {
-        count++;
-        V_avg += *it;
-        if(*it < min) min = *it;
-      }
+      ++count;
+      avgV += *it;
+      // Track the minimum value.
+      if (*it < min)
+        min = *it;
     }
-    V_avg = sqrt(((V_avg / (n * m)) - min) / r);
+
+    V_avg = sqrt(((avgV / (n * m)) - min) / r);
 
     // Intialize to random values.
     W.randu(n, r);
     H.randu(r, m);
 
-    W = W + V_avg;
-    H = H + V_avg;
+    W = W + avgV;
+    H = H + avgV;
   }
 };
 
-}; // namespace amf
-}; // namespace mlpack
+} // namespace amf
+} // namespace mlpack
 
 #endif



More information about the mlpack-git mailing list