[mlpack-svn] r17044 - in mlpack/trunk/src/mlpack/methods: cf regularized_svd

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Sat Aug 16 14:53:32 EDT 2014


Author: siddharth.950
Date: Sat Aug 16 14:53:31 2014
New Revision: 17044

Log:
Made Reg SVD work with CF.

Modified:
   mlpack/trunk/src/mlpack/methods/cf/cf.hpp
   mlpack/trunk/src/mlpack/methods/cf/cf_impl.hpp
   mlpack/trunk/src/mlpack/methods/regularized_svd/regularized_svd.hpp
   mlpack/trunk/src/mlpack/methods/regularized_svd/regularized_svd_impl.hpp

Modified: mlpack/trunk/src/mlpack/methods/cf/cf.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/cf/cf.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/cf/cf.hpp	Sat Aug 16 14:53:31 2014
@@ -24,6 +24,24 @@
 namespace cf /** Collaborative filtering. */ {
 
 /**
+ * Template class for factorizer traits. This stores the default values for the
+ * variables to be assumed for a given factorizer. If any of the factorizers
+ * needs to have a different value for the traits, a template specialization has
+ * be wriiten for that factorizer. An example can be found in the module for
+ * Regularized SVD.
+ */
+template<typename FactorizerType>
+class FactorizerTraits
+{
+ public:
+  /**
+   * If true, then the passed data matrix is used for factorizer.Apply().
+   * Otherwise, it is modified into a form suitable for factorization.
+   */
+  static const bool IsCleaned = false;
+};
+
+/**
  * This class implements Collaborative Filtering (CF). This implementation
  * presently supports Alternating Least Squares (ALS) for collaborative
  * filtering.
@@ -73,6 +91,29 @@
   CF(arma::mat& data,
      const size_t numUsersForSimilarity = 5,
      const size_t rank = 0);
+  
+  /**
+   * Initialize the CF object using an instantiated factorizer. Store a
+   * reference to the data that we will be using. There are parameters that can
+   * be set; default values are provided for each of them. If the rank is left
+   * unset (or is set to 0), a simple density-based heuristic will be used to
+   * choose a rank.
+   *
+   * @param data Initial (user, item, rating) matrix.
+   * @param factorizer Instantiated factorizer object.
+   * @param numUsersForSimilarity Size of the neighborhood.
+   * @param rank Rank parameter for matrix factorization.
+   */
+  CF(arma::mat& data,
+     FactorizerType& factorizer,
+     const size_t numUsersForSimilarity = 5,
+     const size_t rank = 0);
+   
+  /*void ApplyFactorizer(arma::mat& data, const typename boost::enable_if_c<
+      FactorizerTraits<FactorizerType>::IsCleaned == false, int*>::type);
+      
+  void ApplyFactorizer(arma::mat& data, const typename boost::enable_if_c<
+      FactorizerTraits<FactorizerType>::IsCleaned == true, int*>::type);*/
 
   //! Sets number of users for calculating similarity.
   void NumUsersForSimilarity(const size_t num)

Modified: mlpack/trunk/src/mlpack/methods/cf/cf_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/cf/cf_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/cf/cf_impl.hpp	Sat Aug 16 14:53:31 2014
@@ -12,6 +12,32 @@
 namespace mlpack {
 namespace cf {
 
+template<typename FactorizerType>
+void ApplyFactorizer(arma::mat& data,
+    arma::sp_mat& cleanedData,
+    FactorizerType& factorizer,
+    const size_t rank,
+    arma::mat& w,
+    arma::mat& h,
+    const typename boost::enable_if_c<
+    FactorizerTraits<FactorizerType>::IsCleaned == false, int*>::type = 0)
+{
+  factorizer.Apply(cleanedData, rank, w, h);
+}
+
+template<typename FactorizerType>
+void ApplyFactorizer(arma::mat& data,
+    arma::sp_mat& cleanedData,
+    FactorizerType& factorizer,
+    const size_t rank,
+    arma::mat& w,
+    arma::mat& h,
+    const typename boost::enable_if_c<
+    FactorizerTraits<FactorizerType>::IsCleaned == true, int*>::type = 0)
+{
+  factorizer.Apply(data, rank, w, h);
+}
+
 /**
  * Construct the CF object.
  */
@@ -24,7 +50,50 @@
     factorizer()
 {
   // Validate neighbourhood size.
-  if (numUsersForSimilarity < 1)
+  if(numUsersForSimilarity < 1)
+  {
+    Log::Warn << "CF::CF(): neighbourhood size should be > 0("
+        << numUsersForSimilarity << " given). Setting value to 5.\n";
+    //Setting Default Value of 5
+    this->numUsersForSimilarity = 5;
+  }
+
+  CleanData(data);
+
+  // Check if the user wanted us to choose a rank for them.
+  if(rank == 0)
+  {
+    // This is a simple heuristic that picks a rank based on the density of the
+    // dataset between 5 and 105.
+    const double density = (cleanedData.n_nonzero * 100.0) / cleanedData.n_elem;
+    const size_t rankEstimate = size_t(density) + 5;
+
+    // Set to heuristic value.
+    Log::Info << "No rank given for decomposition; using rank of "
+        << rankEstimate << " calculated by density-based heuristic."
+        << std::endl;
+    this->rank = rankEstimate;
+  }
+
+  // Operations independent of the query:
+  // Decompose the sparse data matrix to user and data matrices.
+  ApplyFactorizer<FactorizerType>(data, cleanedData, factorizer, this->rank, w, h);
+}
+
+/**
+ * Construct the CF object using an instantiated factorizer.
+ */
+template<typename FactorizerType>
+CF<FactorizerType>::CF(arma::mat& data,
+                       FactorizerType& factorizer,
+                       const size_t numUsersForSimilarity,
+                       const size_t rank) :
+    numUsersForSimilarity(numUsersForSimilarity),
+    rank(rank),
+    factorizer(factorizer)
+{
+  // Validate neighbourhood size.
+  if(numUsersForSimilarity < 1)
   {
     Log::Warn << "CF::CF(): neighbourhood size should be > 0("
         << numUsersForSimilarity << " given). Setting value to 5.\n";
@@ -35,7 +104,7 @@
   CleanData(data);
 
   // Check if the user wanted us to choose a rank for them.
-  if (rank == 0)
+  if(rank == 0)
   {
     // This is a simple heuristic that picks a rank based on the density of the
     // dataset between 5 and 105.
@@ -51,7 +120,7 @@
 
   // Operations independent of the query:
   // Decompose the sparse data matrix to user and data matrices.
-  factorizer.Apply(cleanedData, this->rank, w, h);
+  ApplyFactorizer<FactorizerType>(data, cleanedData, factorizer, this->rank, w, h);
 }
 
 template<typename FactorizerType>

Modified: mlpack/trunk/src/mlpack/methods/regularized_svd/regularized_svd.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/regularized_svd/regularized_svd.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/regularized_svd/regularized_svd.hpp	Sat Aug 16 14:53:31 2014
@@ -10,6 +10,7 @@
 
 #include <mlpack/core.hpp>
 #include <mlpack/core/optimizers/sgd/sgd.hpp>
+#include <mlpack/methods/cf/cf.hpp>
 
 #include "regularized_svd_function.hpp"
 
@@ -29,41 +30,54 @@
    * RegularizedSVDFunction for optimization. It uses the SGD optimizer by
    * default. The optimizer uses a template specialization of Optimize().
    *
-   * @param data Dataset for which SVD is calculated.
-   * @param u User matrix in the matrix decomposition.
-   * @param v Item matrix in the matrix decomposition.
-   * @param rank Rank used for matrix factorization.
    * @param iterations Number of optimization iterations.
+   * @param alpha Learning rate for the SGD optimizer.
    * @param lambda Regularization parameter for the optimization.
    */
-  RegularizedSVD(const arma::mat& data,
-                 arma::mat& u,
-                 arma::mat& v,
-                 const size_t rank,
-                 const size_t iterations = 10,
+  RegularizedSVD(const size_t iterations = 10,
                  const double alpha = 0.01,
                  const double lambda = 0.02);
+  
+  /**
+   * Obtains the user and item matrices using the provided data and rank.
+   *
+   * @param data Rating data matrix.
+   * @param rank Rank parameter to be used for optimization.
+   * @param u Item matrix obtained on decomposition.
+   * @param v User matrix obtained on decomposition.
+   */
+  void Apply(const arma::mat& data,
+             const size_t rank,
+             arma::mat& u,
+             arma::mat& v);
                  
  private:
-  //! Rating data.
-  const arma::mat& data;
-  //! Rank used for matrix factorization.
-  size_t rank;
   //! Number of optimization iterations.
   size_t iterations;
   //! Learning rate for the SGD optimizer.
   double alpha;
   //! Regularization parameter for the optimization.
   double lambda;
-  //! Function that will be held by the optimizer.
-  RegularizedSVDFunction rSVDFunc;
-  //! Default SGD optimizer for the class.
-  mlpack::optimization::SGD<RegularizedSVDFunction> optimizer;
 };
 
 }; // namespace svd
 }; // namespace mlpack
 
+namespace mlpack {
+namespace cf {
+
+//! Factorizer traits of Regularized SVD.
+template<>
+class FactorizerTraits<mlpack::svd::RegularizedSVD<> >
+{
+ public:
+  //! Data provided to RegularizedSVD need not be cleaned.
+  static const bool IsCleaned = true;
+};
+
+}; // namespace cf
+}; // namespace mlpack
+
 // Include implementation.
 #include "regularized_svd_impl.hpp"
 

Modified: mlpack/trunk/src/mlpack/methods/regularized_svd/regularized_svd_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/regularized_svd/regularized_svd_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/regularized_svd/regularized_svd_impl.hpp	Sat Aug 16 14:53:31 2014
@@ -12,33 +12,38 @@
 namespace svd {
 
 template<template<typename> class OptimizerType>
-RegularizedSVD<OptimizerType>::RegularizedSVD(const arma::mat& data,
-                                              arma::mat& u,
-                                              arma::mat& v,
-                                              const size_t rank,
-                                              const size_t iterations,
+RegularizedSVD<OptimizerType>::RegularizedSVD(const size_t iterations,
                                               const double alpha,
                                               const double lambda) :
-    data(data),
-    rank(rank),
     iterations(iterations),
     alpha(alpha),
-    lambda(lambda),
-    rSVDFunc(data, rank, lambda),
-    optimizer(rSVDFunc, alpha, iterations * data.n_cols)
+    lambda(lambda)
 {
-  arma::mat parameters = rSVDFunc.GetInitialPoint();
+  // Nothing to do.
+}
 
-  // Train the model.
-  Timer::Start("regularized_svd_optimization");
-  const double out = optimizer.Optimize(parameters);
-  Timer::Stop("regularized_svd_optimization");
+template<template<typename> class OptimizerType>
+void RegularizedSVD<OptimizerType>::Apply(const arma::mat& data,
+                                          const size_t rank,
+                                          arma::mat& u,
+                                          arma::mat& v)
+{
+  // Make the optimizer object using a RegularizedSVDFunction object.
+  RegularizedSVDFunction rSVDFunc(data, rank, lambda);
+  mlpack::optimization::SGD<RegularizedSVDFunction> optimizer(rSVDFunc, alpha,
+      iterations * data.n_cols);
+  
+  // Get optimized parameters.
+  arma::mat parameters = rSVDFunc.GetInitialPoint();
+  optimizer.Optimize(parameters);
   
+  // Constants for extracting user and item matrices.
   const size_t numUsers = max(data.row(0)) + 1;
   const size_t numItems = max(data.row(1)) + 1;
   
-  u = parameters.submat(0, 0, rank - 1, numUsers - 1);
-  v = parameters.submat(0, numUsers, rank - 1, numUsers + numItems - 1);
+  // Extract user and item matrices from the optimized parameters.
+  u = parameters.submat(0, numUsers, rank - 1, numUsers + numItems - 1).t();
+  v = parameters.submat(0, 0, rank - 1, numUsers - 1);
 }
 
 }; // namespace svd



More information about the mlpack-svn mailing list