[mlpack-svn] r17057 - in mlpack/trunk/src/mlpack: methods/amf/init_rules methods/cf tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Sun Aug 17 13:34:42 EDT 2014


Author: sumedhghaisas
Date: Sun Aug 17 13:34:41 2014
New Revision: 17057

Log:
* added AverageInitialization to AMF


Added:
   mlpack/trunk/src/mlpack/methods/amf/init_rules/average_init.hpp
Modified:
   mlpack/trunk/src/mlpack/methods/amf/init_rules/CMakeLists.txt
   mlpack/trunk/src/mlpack/methods/cf/svd_wrapper.hpp
   mlpack/trunk/src/mlpack/methods/cf/svd_wrapper_impl.hpp
   mlpack/trunk/src/mlpack/tests/svd_batch_test.cpp

Modified: mlpack/trunk/src/mlpack/methods/amf/init_rules/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/methods/amf/init_rules/CMakeLists.txt	(original)
+++ mlpack/trunk/src/mlpack/methods/amf/init_rules/CMakeLists.txt	Sun Aug 17 13:34:41 2014
@@ -3,6 +3,7 @@
 set(SOURCES
   random_init.hpp
   random_acol_init.hpp
+  average_init.hpp
 )
 
 # Add directory name to sources.

Added: mlpack/trunk/src/mlpack/methods/amf/init_rules/average_init.hpp
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/methods/amf/init_rules/average_init.hpp	Sun Aug 17 13:34:41 2014
@@ -0,0 +1,63 @@
+/**
+ * @file averge_init.hpp
+ * @author Sumedh Ghaisas
+ *
+ * Intialization rule for Alternating Matrix Factorization.
+ */
+#ifndef __MLPACK_METHODS_AMF_AVERAGE_INIT_HPP
+#define __MLPACK_METHODS_AMF_AVERAGE_INIT_HPP
+
+#include <mlpack/core.hpp>
+
+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.
+ */ 
+class AverageInitialization
+{
+ public:
+  // Empty constructor required for the InitializeRule template
+  AverageInitialization() { }
+
+  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;
+  
+    double V_avg = 0;
+    size_t count = 0;
+    double min = DBL_MAX;
+    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;
+      }
+    }
+    V_avg = sqrt(((V_avg / (n * m)) - min) / r);
+
+    // Intialize to random values.
+    W.randu(n, r);
+    H.randu(r, m);
+    
+    W = W + V_avg;
+    H = H + V_avg;
+  }
+};
+
+}; // namespace amf
+}; // namespace mlpack
+
+#endif

Modified: mlpack/trunk/src/mlpack/methods/cf/svd_wrapper.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/cf/svd_wrapper.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/cf/svd_wrapper.hpp	Sun Aug 17 13:34:41 2014
@@ -15,12 +15,22 @@
 {
 
 /**
- *
- * @see CF
+ * This class acts as a dummy class for passing as template parameter. Passing 
+ * this class as a template parameter to class SVDWrapper will force SVDWrapper
+ * to use Armadillo's SVD implementation.
  */
-
 class DummyClass {}; 
- 
+
+/**
+ * This class acts as the wrapper for all SVD factorizers which are incompatible 
+ * with CF module. Normally SVD factrorizers implement Apply method which takes 
+ * matrix V and factorizes it into P, sigma and Q where V = P * sigma * trans(Q).
+ * But CF module requires factrorization to be V = W * H. This class multiplies 
+ * P and sigma and takes the first 'r' eigenvectors out where 'r' is the rank
+ * of factorization. Q matrix is transposed and trimmed to support the rank 
+ * of factorization. The Factroizer class should implement Apply which takes 
+ * matrices P, sigma, Q and V as their parameter respectively. 
+ */
 template<class Factorizer = DummyClass>
 class SVDWrapper
 {

Modified: mlpack/trunk/src/mlpack/methods/cf/svd_wrapper_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/cf/svd_wrapper_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/cf/svd_wrapper_impl.hpp	Sun Aug 17 13:34:41 2014
@@ -4,7 +4,6 @@
  *
  * Implementation of the SVD wrapper class.
  */
-
 template<class Factorizer>
 double mlpack::cf::SVDWrapper<Factorizer>::Apply(const arma::mat& V,
                          arma::mat& W,

Modified: mlpack/trunk/src/mlpack/tests/svd_batch_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/svd_batch_test.cpp	(original)
+++ mlpack/trunk/src/mlpack/tests/svd_batch_test.cpp	Sun Aug 17 13:34:41 2014
@@ -2,6 +2,7 @@
 #include <mlpack/methods/amf/amf.hpp>
 #include <mlpack/methods/amf/update_rules/svd_batch_learning.hpp>
 #include <mlpack/methods/amf/init_rules/random_init.hpp>
+#include <mlpack/methods/amf/init_rules/average_init.hpp>
 #include <mlpack/methods/amf/termination_policies/validation_RMSE_termination.hpp>
 #include <mlpack/methods/amf/termination_policies/simple_tolerance_termination.hpp>
 
@@ -24,7 +25,7 @@
   sp_mat data;
   data.sprandn(1000, 1000, 0.2);
   AMF<SimpleToleranceTermination<sp_mat>, 
-      RandomInitialization, 
+      AverageInitialization, 
       SVDBatchLearning> amf;
   mat m1,m2;
   amf.Apply(data, 2, m1, m2);



More information about the mlpack-svn mailing list