[mlpack-git] master: Refactor to use SFINAE for both constructors. (362b6b0)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Tue May 26 11:12:22 EDT 2015


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

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/d2f2976c7a43f8ab9139064ae33304bcf9f4f884...29ab461472f64f72cfbdb93b0d9045024050cc95

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

commit 362b6b0a138aa5c2e65fa5e5b5685ace1f361ff2
Author: Ryan Curtin <ryan at ratml.org>
Date:   Sat May 23 01:00:27 2015 +0000

    Refactor to use SFINAE for both constructors.


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

362b6b0a138aa5c2e65fa5e5b5685ace1f361ff2
 src/mlpack/methods/cf/cf.hpp      | 35 +++++++++++++----------
 src/mlpack/methods/cf/cf_impl.hpp | 59 +++++++--------------------------------
 2 files changed, 30 insertions(+), 64 deletions(-)

diff --git a/src/mlpack/methods/cf/cf.hpp b/src/mlpack/methods/cf/cf.hpp
index 1a063f6..3f510eb 100644
--- a/src/mlpack/methods/cf/cf.hpp
+++ b/src/mlpack/methods/cf/cf.hpp
@@ -82,13 +82,21 @@ class CF
    * 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. 
+   * choose a rank.  This overload of the constructor will only be available if
+   * the factorizer does not use a corodinate list (i.e. if UsesCoordinateList
+   * is false).
+   *
+   * The U and T template parameters are for SFINAE, so that this overload is
+   * only available when the FactorizerType does not use a coordinate list.
    *
    * @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.
    */
+  template<typename U = FactorizerType,
+           typename T = typename boost::enable_if_c<
+               FactorizerTraits<U>::UsesCoordinateList>::type*>
   CF(arma::mat& data,
      FactorizerType factorizer = FactorizerType(),
      const size_t numUsersForSimilarity = 5,
@@ -100,9 +108,12 @@ class CF
    * 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. Data will be considered in the format of items vs. users and
-   * will be passed directly to the factorizer without cleaning. This overload 
-   * of constructor will only be available if the factorizer does not require
-   * coordinate list.
+   * will be passed directly to the factorizer without cleaning.  This overload
+   * of the constructor will only be available if the factorizer does not use a
+   * coordinate list (i.e. if UsesCoordinateList is false).
+   *
+   * The U and T template parameters are for SFINAE, so that this overload is
+   * only available when the FactorizerType uses a coordinate list.
    *
    * @param data Initial (user, item, rating) matrix.
    * @param factorizer Instantiated factorizer object.
@@ -110,21 +121,15 @@ class CF
    * @param rank Rank parameter for matrix factorization.
    * @param isCleaned If the data passed is cleaned for CF
    */
-  template<typename U = FactorizerType, 
-           class = typename boost::enable_if_c<
-                   !FactorizerTraits<U>::UsesCoordinateList,
-                   int*>::type>
-  CF(const arma::sp_mat& data,
+  template<typename MatType,
+           typename U = FactorizerType,
+           typename T = typename boost::disable_if_c<
+               FactorizerTraits<U>::UsesCoordinateList>::type*>
+  CF(const MatType& data,
      FactorizerType factorizer = FactorizerType(),
      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)
   {
diff --git a/src/mlpack/methods/cf/cf_impl.hpp b/src/mlpack/methods/cf/cf_impl.hpp
index c2bd2d6..4c78331 100644
--- a/src/mlpack/methods/cf/cf_impl.hpp
+++ b/src/mlpack/methods/cf/cf_impl.hpp
@@ -18,48 +18,10 @@ namespace mlpack {
 namespace cf {
 
 /**
- * This function is used to factorize the rating matrix into the user and item
- * matrices, when UsesCoordinateList of property of the factorizer is set to
- * false. It uses the cleaned rating matrix instead of a (user, item, rating)
- * list.
- */
-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>::UsesCoordinateList == false,
-        int*>::type = 0)
-{
-  factorizer.Apply(cleanedData, rank, w, h);
-}
-
-/**
- * This function is used to factorize the rating matrix into the user and item
- * matrices, when UsesCoordinateList of property of the factorizer is set to
- * true. It uses the (user, item, rating) list for factorization.
- */
-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>::UsesCoordinateList == true,
-        int*>::type = 0)
-{
-  factorizer.Apply(data, rank, w, h);
-}
-
-/**
  * Construct the CF object using an instantiated factorizer.
  */
 template<typename FactorizerType>
+template<typename U, typename T>
 CF<FactorizerType>::CF(arma::mat& data,
                        FactorizerType factorizer,
                        const size_t numUsersForSimilarity,
@@ -73,7 +35,7 @@ CF<FactorizerType>::CF(arma::mat& data,
   {
     Log::Warn << "CF::CF(): neighbourhood size should be > 0("
         << numUsersForSimilarity << " given). Setting value to 5.\n";
-    //Setting Default Value of 5
+    // Set default value of 5.
     this->numUsersForSimilarity = 5;
   }
 
@@ -94,21 +56,20 @@ CF<FactorizerType>::CF(arma::mat& data,
     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);
+  // Decompose the data matrix (which is in coordinate list form) to user and
+  // data matrices.
+  factorizer.Apply(data, this->rank, w, h);
 }
 
 /**
  * Construct the CF object using an instantiated factorizer.
  */
 template<typename FactorizerType>
-template<typename U, class>
-CF<FactorizerType>::CF(const arma::sp_mat& data,
-            FactorizerType factorizer,
-            const size_t numUsersForSimilarity,
-            const size_t rank) :
+template<typename MatType, typename U, typename T>
+CF<FactorizerType>::CF(const MatType& data,
+                       FactorizerType factorizer,
+                       const size_t numUsersForSimilarity,
+                       const size_t rank) :
     numUsersForSimilarity(numUsersForSimilarity),
     rank(rank),
     factorizer(factorizer)



More information about the mlpack-git mailing list