[mlpack-svn] r16939 - in mlpack/trunk/src/mlpack: core/arma_extend methods/amf/termination_policies methods/cf

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Aug 4 17:43:24 EDT 2014


Author: sumedhghaisas
Date: Mon Aug  4 17:43:23 2014
New Revision: 16939

Log:
* added plain SVD factorization - wrapper of arma::svd for CF module


Added:
   mlpack/trunk/src/mlpack/methods/cf/plain_svd.cpp
   mlpack/trunk/src/mlpack/methods/cf/plain_svd.hpp
Modified:
   mlpack/trunk/src/mlpack/core/arma_extend/SpMat_extra_bones.hpp
   mlpack/trunk/src/mlpack/core/arma_extend/SpMat_extra_meat.hpp
   mlpack/trunk/src/mlpack/methods/amf/termination_policies/complete_incremental_termination.hpp
   mlpack/trunk/src/mlpack/methods/cf/CMakeLists.txt

Modified: mlpack/trunk/src/mlpack/core/arma_extend/SpMat_extra_bones.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/arma_extend/SpMat_extra_bones.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/arma_extend/SpMat_extra_bones.hpp	Mon Aug  4 17:43:23 2014
@@ -17,3 +17,18 @@
     const uword n_cols,
     const bool sort_locations = true);
 #endif
+
+/*
+ * Extra functions for SpMat<eT> 
+ * Adding definition of row_col_iterator to generalize with Mat<eT>::row_col_iterator
+ */
+typedef iterator row_col_iterator;
+typedef const_iterator const_row_col_iterator;
+
+// begin for iterator row_col_iterator
+inline const_row_col_iterator begin_row_col() const;
+inline row_col_iterator begin_row_col();
+
+// end for iterator row_col_iterator
+inline const_row_col_iterator end_row_col() const;
+inline row_col_iterator end_row_col();

Modified: mlpack/trunk/src/mlpack/core/arma_extend/SpMat_extra_meat.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/arma_extend/SpMat_extra_meat.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/arma_extend/SpMat_extra_meat.hpp	Mon Aug  4 17:43:23 2014
@@ -249,3 +249,31 @@
   }
 
 #endif
+
+template<typename eT>
+inline typename SpMat<eT>::const_row_col_iterator 
+SpMat<eT>::begin_row_col() const
+  {
+  return begin();
+  }
+  
+template<typename eT>
+inline typename SpMat<eT>::row_col_iterator
+SpMat<eT>::begin_row_col()
+  {
+  return begin();
+  }
+  
+template<typename eT>
+inline typename SpMat<eT>::const_row_col_iterator
+SpMat<eT>::end_row_col() const
+  {
+  return end();
+  }
+  
+template<typename eT>
+inline typename SpMat<eT>::row_col_iterator
+SpMat<eT>::end_row_col()
+  {
+  return end();
+  }

Modified: mlpack/trunk/src/mlpack/methods/amf/termination_policies/complete_incremental_termination.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/amf/termination_policies/complete_incremental_termination.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/amf/termination_policies/complete_incremental_termination.hpp	Mon Aug  4 17:43:23 2014
@@ -1,5 +1,14 @@
-#ifndef COMPLETE_INCREMENTAL_TERMINATION_HPP_INCLUDED
-#define COMPLETE_INCREMENTAL_TERMINATION_HPP_INCLUDED
+/**
+ * @file cf.hpp
+ * @author Sumedh Ghaisas
+ *
+ * Collaborative filtering.
+ *
+ * Defines the CF class to perform collaborative filtering on the specified data
+ * set using alternating least squares (ALS).
+ */
+#ifndef _MLPACK_METHODS_AMF_COMPLETE_INCREMENTAL_TERMINATION_HPP_INCLUDED
+#define _MLPACK_METHODS_AMF_COMPLETE_INCREMENTAL_TERMINATION_HPP_INCLUDED
 
 namespace mlpack
 {

Modified: mlpack/trunk/src/mlpack/methods/cf/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/methods/cf/CMakeLists.txt	(original)
+++ mlpack/trunk/src/mlpack/methods/cf/CMakeLists.txt	Mon Aug  4 17:43:23 2014
@@ -3,6 +3,8 @@
 set(SOURCES
   cf.hpp
   cf_impl.hpp
+  plain_svd.hpp
+  plain_svd.cpp
 )
 
 # Add directory name to sources.

Added: mlpack/trunk/src/mlpack/methods/cf/plain_svd.cpp
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/methods/cf/plain_svd.cpp	Mon Aug  4 17:43:23 2014
@@ -0,0 +1,75 @@
+#include "plain_svd.hpp"
+
+using namespace mlpack;
+using namespace mlpack::svd;
+
+double PlainSVD::Apply(const arma::mat& V,
+                       arma::mat& W,
+                       arma::mat& sigma,
+                       arma::mat& H) const
+{
+  arma::vec E;
+  arma::svd(W, E, H, V);
+
+  sigma.zeros(V.n_rows, V.n_cols);
+
+  for(size_t i = 0;i < sigma.n_rows && i < sigma.n_cols;i++)
+    sigma(i, i) = E(i, 0);
+
+  arma::mat V_rec = W * sigma * arma::trans(H);
+
+  size_t n = V.n_rows;
+  size_t m = V.n_cols;
+  double sum = 0;
+  for(size_t i = 0;i < n;i++)
+  {
+    for(size_t j = 0;j < m;j++)
+    {
+      double temp = V(i, j);
+      temp = (temp - V_rec(i, j));
+      temp = temp * temp;
+      sum += temp;
+    }
+  }
+  return sqrt(sum / (n * m));
+}
+
+double PlainSVD::Apply(const arma::mat& V,
+                       size_t r,
+                       arma::mat& W,
+                       arma::mat& H) const
+{
+  if(r > V.n_rows || r > V.n_cols)
+  {
+    Log::Info << "Rank " << r << ", given for decomposition is invalid." << std::endl;
+    r = (V.n_rows > V.n_cols) ? V.n_cols : V.n_rows;
+    Log::Info << "Setting decomposition rank to " << r << std::endl;
+  }
+
+  arma::vec sigma;
+  arma::svd(W, sigma, H, V);
+
+  W = W.submat(0, 0, W.n_rows - 1, r - 1);
+  H = H.submat(0, 0, H.n_cols - 1, r - 1);
+
+  sigma = sigma.subvec(0, r - 1);
+
+  W = W * arma::diagmat(sigma);
+
+  arma::mat V_rec = W * arma::trans(H);
+
+  size_t n = V.n_rows;
+  size_t m = V.n_cols;
+  double sum = 0;
+  for(size_t i = 0;i < n;i++)
+  {
+    for(size_t j = 0;j < m;j++)
+    {
+      double temp = V(i, j);
+      temp = (temp - V_rec(i, j));
+      temp = temp * temp;
+      sum += temp;
+    }
+  }
+  return sqrt(sum / (n * m));
+}

Added: mlpack/trunk/src/mlpack/methods/cf/plain_svd.hpp
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/methods/cf/plain_svd.hpp	Mon Aug  4 17:43:23 2014
@@ -0,0 +1,30 @@
+#ifndef __MLPACK_METHODS_PLAIN_SVD_HPP
+#define __MLPACK_METHODS_PLAIN_SVD_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack
+{
+namespace svd
+{
+
+class PlainSVD
+{
+ public:
+  PlainSVD() {};
+
+  double Apply(const arma::mat& V,
+               arma::mat& W,
+               arma::mat& sigma,
+               arma::mat& H) const;
+
+  double Apply(const arma::mat& V,
+               size_t r,
+               arma::mat& W,
+               arma::mat& H) const;
+};
+
+};
+};
+
+#endif



More information about the mlpack-svn mailing list