[mlpack-git] master: Increase decomposition stability by initializing the mean parameter with a small non-negative value. (635e37c)

gitdub at mlpack.org gitdub at mlpack.org
Sat Sep 17 07:54:21 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/7e25083f4695d09a3d8c965976eec2b7056f16ac...635e37c4de03cdabfc4b493ad2ab5e9e51f9823e

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

commit 635e37c4de03cdabfc4b493ad2ab5e9e51f9823e
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date:   Sat Sep 17 13:54:21 2016 +0200

    Increase decomposition stability by initializing the mean parameter with a small non-negative value.


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

635e37c4de03cdabfc4b493ad2ab5e9e51f9823e
 .../methods/randomized_svd/randomized_svd.cpp      | 19 +++++++------
 .../methods/randomized_svd/randomized_svd.hpp      | 32 ++++++++++++++++------
 2 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/src/mlpack/methods/randomized_svd/randomized_svd.cpp b/src/mlpack/methods/randomized_svd/randomized_svd.cpp
index 64d62ee..72091ca 100644
--- a/src/mlpack/methods/randomized_svd/randomized_svd.cpp
+++ b/src/mlpack/methods/randomized_svd/randomized_svd.cpp
@@ -16,9 +16,11 @@ RandomizedSVD::RandomizedSVD(const arma::mat& data,
                              arma::mat& v,
                              const size_t iteratedPower,
                              const size_t maxIterations,
-                             const size_t rank) :
+                             const size_t rank,
+                             const double eps) :
     iteratedPower(iteratedPower),
-    maxIterations(maxIterations)
+    maxIterations(maxIterations),
+    eps(eps)
 {
   if (rank == 0)
   {
@@ -31,9 +33,11 @@ RandomizedSVD::RandomizedSVD(const arma::mat& data,
 }
 
 RandomizedSVD::RandomizedSVD(const size_t iteratedPower,
-                             const size_t maxIterations) :
+                             const size_t maxIterations,
+                             const double eps) :
     iteratedPower(iteratedPower),
-    maxIterations(maxIterations)
+    maxIterations(maxIterations),
+    eps(eps)
 {
   /* Nothing to do here */
 }
@@ -48,21 +52,20 @@ void RandomizedSVD::Apply(const arma::mat& data,
     iteratedPower = rank + 2;
 
   // Center the data into a temporary matrix.
-  arma::vec rowMean = arma::sum(data, 1) / data.n_cols;
+  arma::vec rowMean = arma::sum(data, 1) / data.n_cols + eps;
 
   arma::mat R, Q, Qdata;
-  ann::RandomInitialization randomInit;
 
   // Apply the centered data matrix to a random matrix, obtaining Q.
   if (data.n_cols >= data.n_rows)
   {
-    randomInit.Initialize(R, data.n_rows, iteratedPower);
+    R = arma::randn<arma::mat>(data.n_rows, iteratedPower);
     Q = (data.t() * R) - arma::repmat(arma::trans(R.t() * rowMean),
         data.n_cols, 1);
   }
   else
   {
-    randomInit.Initialize(R, data.n_cols, iteratedPower);
+    R = arma::randn<arma::mat>(data.n_cols, iteratedPower);
     Q = (data * R) - (rowMean * (arma::ones(1, data.n_cols) * R));
   }
 
diff --git a/src/mlpack/methods/randomized_svd/randomized_svd.hpp b/src/mlpack/methods/randomized_svd/randomized_svd.hpp
index c175fd2..ba159c0 100644
--- a/src/mlpack/methods/randomized_svd/randomized_svd.hpp
+++ b/src/mlpack/methods/randomized_svd/randomized_svd.hpp
@@ -9,7 +9,6 @@
 #define MLPACK_METHODS_RANDOMIZED_SVD_RANDOMIZED_SVD_HPP
 
 #include <mlpack/core.hpp>
-#include <mlpack/methods/ann/init_rules/random_init.hpp>
 
 namespace mlpack {
 namespace svd {
@@ -61,7 +60,7 @@ namespace svd {
  */
 class RandomizedSVD
 {
-  public:
+ public:
   /**
    * Create object for the randomized SVD method.
    *
@@ -74,6 +73,8 @@ class RandomizedSVD
    * @param maxIterations Number of iterations for the power method
    *        (Default: 2).
    * @param rank Rank of the approximation (Default: number of rows.)
+   * @param eps The eps coefficient to avoid division by zero (numerical
+   *        stability).
    */
   RandomizedSVD(const arma::mat& data,
                 arma::mat& u,
@@ -81,7 +82,8 @@ class RandomizedSVD
                 arma::mat& v,
                 const size_t iteratedPower = 0,
                 const size_t maxIterations = 2,
-                const size_t rank = 0);
+                const size_t rank = 0,
+                const double eps = 1e-7);
 
   /**
    * Create object for the randomized SVD method.
@@ -90,8 +92,12 @@ class RandomizedSVD
    *        (Default: rank + 2).
    * @param maxIterations Number of iterations for the power method
    *        (Default: 2).
+   * @param eps The eps coefficient to avoid division by zero (numerical
+   *        stability).
    */
-  RandomizedSVD(const size_t iteratedPower = 0, const size_t maxIterations = 2);
+  RandomizedSVD(const size_t iteratedPower = 0,
+                const size_t maxIterations = 2,
+                const double eps = 1e-7);
 
   /**
    * Apply Principal Component Analysis to the provided data set using the
@@ -119,12 +125,20 @@ class RandomizedSVD
   //! Modify the number of iterations for the power method.
   size_t& MaxIterations() { return maxIterations; }
 
-  private:
-    //! Locally stored size of the normalized power iterations.
-    size_t iteratedPower;
+  //! Get the value used for decomposition stability.
+  double Epsilon() const { return eps; }
+  //! Modify the value used for decomposition stability.
+  double& Epsilon() { return eps; }
+
+ private:
+  //! Locally stored size of the normalized power iterations.
+  size_t iteratedPower;
+
+  //! Locally stored number of iterations for the power method.
+  size_t maxIterations;
 
-    //! Locally stored number of iterations for the power method.
-    size_t maxIterations;
+  //! The value used for numerical stability.
+  double eps;
 };
 
 } // namespace svd




More information about the mlpack-git mailing list