[mlpack] 52/324: * Added momentum to SVD batch learning * AMF now calls Initialize on update rule before starting the optimization * Every update rule should now implement Initialize accepting data matrix and rank

Barak A. Pearlmutter barak+git at cs.nuim.ie
Sun Aug 17 08:21:55 UTC 2014


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

bap pushed a commit to branch svn-trunk
in repository mlpack.

commit 383e30bd042c939abd0ed3b5fbe1864122bd3460
Author: sumedhghaisas <sumedhghaisas at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date:   Wed Jun 11 15:17:04 2014 +0000

    * Added momentum to SVD batch learning
    * AMF now calls Initialize on update rule before starting the optimization
    * Every update rule should now implement Initialize accepting data matrix
      and rank
    
    
    git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@16681 9d5b8971-822b-0410-80eb-d18c1038ef23
---
 src/mlpack/methods/amf/amf.hpp                     |  2 +-
 src/mlpack/methods/amf/amf_impl.hpp                |  6 ++--
 src/mlpack/methods/amf/update_rules/nmf_als.hpp    |  7 +++++
 .../methods/amf/update_rules/nmf_mult_dist.hpp     |  7 +++++
 .../methods/amf/update_rules/nmf_mult_div.hpp      | 11 ++++++--
 .../methods/amf/update_rules/svd_batchlearning.hpp | 32 ++++++++++++++++++----
 6 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/src/mlpack/methods/amf/amf.hpp b/src/mlpack/methods/amf/amf.hpp
index 64ef615..d505c88 100644
--- a/src/mlpack/methods/amf/amf.hpp
+++ b/src/mlpack/methods/amf/amf.hpp
@@ -83,7 +83,7 @@ class AMF
   double Apply(const MatType& V,
              const size_t r,
              arma::mat& W,
-             arma::mat& H) const;
+             arma::mat& H);
 
  private:
   //! The maximum number of iterations allowed before giving up.
diff --git a/src/mlpack/methods/amf/amf_impl.hpp b/src/mlpack/methods/amf/amf_impl.hpp
index b601596..2bd0ee9 100644
--- a/src/mlpack/methods/amf/amf_impl.hpp
+++ b/src/mlpack/methods/amf/amf_impl.hpp
@@ -43,7 +43,7 @@ double AMF<InitializationRule, UpdateRule>::Apply(
     const MatType& V,
     const size_t r,
     arma::mat& W,
-    arma::mat& H) const
+    arma::mat& H)
 {
   const size_t n = V.n_rows;
   const size_t m = V.n_cols;
@@ -61,7 +61,7 @@ double AMF<InitializationRule, UpdateRule>::Apply(
   double norm = 0;
   arma::mat WH;
 
-  std::cout << tolerance << std::endl;
+  update.Initialize(V, r);
 
   while (((oldResidue - residue) / oldResidue >= tolerance || iteration < 4) && iteration != maxIterations)
   {
@@ -84,8 +84,6 @@ double AMF<InitializationRule, UpdateRule>::Apply(
     normOld = norm;
 
     iteration++;
-
-    std::cout << residue << std::endl;
   }
 
   Log::Info << "AMF converged to residue of " << sqrt(residue) << " in "
diff --git a/src/mlpack/methods/amf/update_rules/nmf_als.hpp b/src/mlpack/methods/amf/update_rules/nmf_als.hpp
index 2ad3606..a3269b4 100644
--- a/src/mlpack/methods/amf/update_rules/nmf_als.hpp
+++ b/src/mlpack/methods/amf/update_rules/nmf_als.hpp
@@ -27,6 +27,13 @@ class NMFALSUpdate
   // Empty constructor required for the UpdateRule template.
   NMFALSUpdate() { }
 
+  template<typename MatType>
+  void Initialize(const MatType& dataset, const size_t rank)
+  {
+      (void)dataset;
+      (void)rank;
+  }
+
   /**
    * The update rule for the basis matrix W. The formula used is
    * \f[
diff --git a/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp b/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp
index 90c0744..c0db131 100644
--- a/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp
+++ b/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp
@@ -26,6 +26,13 @@ class NMFMultiplicativeDistanceUpdate
   // Empty constructor required for the UpdateRule template.
   NMFMultiplicativeDistanceUpdate() { }
 
+  template<typename MatType>
+  void Initialize(const MatType& dataset, const size_t rank)
+  {
+        (void)dataset;
+        (void)rank;
+  }
+
   /**
    * The update rule for the basis matrix W. The formula used is
    * \f[
diff --git a/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp b/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp
index 4890643..b4d7037 100644
--- a/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp
+++ b/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp
@@ -25,13 +25,20 @@ class NMFMultiplicativeDivergenceUpdate
   // Empty constructor required for the WUpdateRule template.
   NMFMultiplicativeDivergenceUpdate() { }
 
+  template<typename MatType>
+  void Initialize(const MatType& dataset, const size_t rank)
+  {
+    (void)dataset;
+    (void)rank;
+  }
+
   /**
    * The update rule for the basis matrix W. The formula used is
    * \f[
    * W_{ia} \leftarrow W_{ia} \frac{\sum_{\mu} H_{a\mu} V_{i\mu}/(WH)_{i\mu}}
    * {\sum_{\nu} H_{a\nu}}
    * \f]
-   * The function takes in all the matrices and only changes the 
+   * The function takes in all the matrices and only changes the
    * value of the W matrix.
    *
    * @param V Input matrix to be factorized.
@@ -73,7 +80,7 @@ class NMFMultiplicativeDivergenceUpdate
    * H_{a\mu} \leftarrow H_{a\mu} \frac{\sum_{i} W_{ia} V_{i\mu}/(WH)_{i\mu}}
    * {\sum_{k} H_{ka}}
    * \f]
-   * The function takes in all the matrices and only changes the value 
+   * The function takes in all the matrices and only changes the value
    * of the H matrix.
    *
    * @param V Input matrix to be factorized.
diff --git a/src/mlpack/methods/amf/update_rules/svd_batchlearning.hpp b/src/mlpack/methods/amf/update_rules/svd_batchlearning.hpp
index 3cd6055..c452895 100644
--- a/src/mlpack/methods/amf/update_rules/svd_batchlearning.hpp
+++ b/src/mlpack/methods/amf/update_rules/svd_batchlearning.hpp
@@ -13,9 +13,21 @@ public:
     SVDBatchLearning(double u = 0.000001,
                      double kw = 0,
                      double kh = 0,
+                     double momentum = 0.2,
                      double min = -DBL_MIN,
                      double max = DBL_MAX)
-        : u(u), kw(kw), kh(kh), min(min), max(max) {}
+        : u(u), kw(kw), kh(kh), min(min), max(max), momentum(momentum)
+    {}
+
+    template<typename MatType>
+    void Initialize(const MatType& dataset, const size_t rank)
+    {
+        const size_t n = dataset.n_rows;
+        const size_t m = dataset.n_cols;
+
+        mW.zeros(n, rank);
+        mH.zeros(rank, m);
+    }
 
     /**
     * The update rule for the basis matrix W.
@@ -29,13 +41,15 @@ public:
     template<typename MatType>
     inline void WUpdate(const MatType& V,
                                arma::mat& W,
-                               const arma::mat& H) const
+                               const arma::mat& H)
     {
         size_t n = V.n_rows;
         size_t m = V.n_cols;
 
         size_t r = W.n_cols;
 
+        mW = momentum * mW;
+
         arma::mat deltaW(n, r);
         deltaW.zeros();
 
@@ -46,7 +60,8 @@ public:
             deltaW.row(i) -= kw * W.row(i);
         }
 
-        W += u * deltaW;
+        mW += u * deltaW;
+        W += mW;
     }
 
     /**
@@ -61,13 +76,15 @@ public:
     template<typename MatType>
     inline void HUpdate(const MatType& V,
                                const arma::mat& W,
-                               arma::mat& H) const
+                               arma::mat& H)
     {
         size_t n = V.n_rows;
         size_t m = V.n_cols;
 
         size_t r = W.n_cols;
 
+        mH = momentum * mH;
+
         arma::mat deltaH(r, m);
         deltaH.zeros();
 
@@ -78,7 +95,8 @@ public:
             deltaH.col(j) -= kh * H.col(j);
         }
 
-        H += u*deltaH;
+        mH += u*deltaH;
+        H += mH;
     }
 private:
 
@@ -94,6 +112,10 @@ private:
     double kh;
     double min;
     double max;
+    double momentum;
+
+    arma::mat mW;
+    arma::mat mH;
 };
 } // namespace amf
 } // namespace mlpack

-- 
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