[mlpack-svn] r16977 - in mlpack/trunk/src/mlpack: core/arma_extend methods/cf tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Aug 6 17:11:20 EDT 2014


Author: sumedhghaisas
Date: Wed Aug  6 17:11:20 2014
New Revision: 16977

Log:
* modified PlainSVD module to return normalized frobenius norm
* modified PlainSVD tests
* added row_col_iterator operator-- tests


Modified:
   mlpack/trunk/src/mlpack/core/arma_extend/Mat_extra_meat.hpp
   mlpack/trunk/src/mlpack/methods/cf/plain_svd.cpp
   mlpack/trunk/src/mlpack/tests/arma_extend_test.cpp
   mlpack/trunk/src/mlpack/tests/plain_svd_test.cpp

Modified: mlpack/trunk/src/mlpack/core/arma_extend/Mat_extra_meat.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/arma_extend/Mat_extra_meat.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/arma_extend/Mat_extra_meat.hpp	Wed Aug  6 17:11:20 2014
@@ -88,12 +88,12 @@
 inline typename Mat<eT>::const_row_col_iterator&
 Mat<eT>::const_row_col_iterator::operator--()
     {
-  if(internal_row != 0)
+  if(internal_row > 0)
     {
     current_pos--;
     internal_row--;
     }
-  else if(internal_col != 0)
+  else if(internal_col > 0)
     {
     current_pos--;
     internal_col--;

Modified: mlpack/trunk/src/mlpack/methods/cf/plain_svd.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/cf/plain_svd.cpp	(original)
+++ mlpack/trunk/src/mlpack/methods/cf/plain_svd.cpp	Wed Aug  6 17:11:20 2014
@@ -18,20 +18,7 @@
 
   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));
+  return arma::norm(V - V_rec, "fro") / arma::norm(V, "fro");
 }
 
 double PlainSVD::Apply(const arma::mat& V,
@@ -60,18 +47,5 @@
 
   arma::mat V_rec = W * 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));
+  return arma::norm(V - V_rec, "fro") / arma::norm(V, "fro");
 }

Modified: mlpack/trunk/src/mlpack/tests/arma_extend_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/arma_extend_test.cpp	(original)
+++ mlpack/trunk/src/mlpack/tests/arma_extend_test.cpp	Wed Aug  6 17:11:20 2014
@@ -145,6 +145,29 @@
   it = X.begin_row(0);
 }
 
+/**
+ * Operator-- test for mat::row_col_iterator and mat::const_row_col_iterator
+ */
+BOOST_AUTO_TEST_CASE(MatRowColIteratorDecrementOperatorTest)
+{
+  mat test = ones<mat>(5, 5);
+  
+  mat::row_col_iterator it1 = test.begin_row_col();
+  mat::row_col_iterator it2 = it1;
+  
+  // check that postfix-- does not decrement the position when position is pointing
+  // to the begining
+  it2--;
+  BOOST_REQUIRE_EQUAL(it1.row(), it2.row());
+  BOOST_REQUIRE_EQUAL(it1.col(), it2.col());
+  
+  // check that prefix-- does not decrement the position when position is pointing
+  // to the begining
+  --it2;
+  BOOST_REQUIRE_EQUAL(it1.row(), it2.row());
+  BOOST_REQUIRE_EQUAL(it1.col(), it2.col());
+}
+
 // These tests don't work when the sparse iterators hold references and not
 // pointers internally because of the lack of default constructor.
 #if ARMA_VERSION_MAJOR > 4 || \

Modified: mlpack/trunk/src/mlpack/tests/plain_svd_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/plain_svd_test.cpp	(original)
+++ mlpack/trunk/src/mlpack/tests/plain_svd_test.cpp	Wed Aug  6 17:11:20 2014
@@ -16,29 +16,36 @@
  */
 BOOST_AUTO_TEST_CASE(PlainSVDNormalFactorizationTest)
 {
-  mlpack::math::RandomSeed(10);
-  mat test = randu<mat>(5,4);
+  mat test = randu<mat>(20, 20);
 
   PlainSVD svd;
   arma::mat W, H, sigma;
   double result = svd.Apply(test, W, sigma, H);
   
-  BOOST_REQUIRE_LT(result, 1e-15);
+  BOOST_REQUIRE_LT(result, 0.01);
+  
+  test = randu<mat>(50, 50);
+  result = svd.Apply(test, W, sigma, H);
+  
+  BOOST_REQUIRE_LT(result, 0.01);
 }
 
 /**
- * Test PlainSVD as wrapper for CF.
+ * Test PlainSVD for low rank matrix factorization
  */
-BOOST_AUTO_TEST_CASE(PlainSVDCFWrapperTest)
+BOOST_AUTO_TEST_CASE(PlainSVDLowRankFactorizationTest)
 {
-  mlpack::math::RandomSeed(10);
-  mat test = randu<mat>(5,4);
+  mat W_t = randu<mat>(30, 3);
+  mat H_t = randu<mat>(3, 40);
   
+  mat test = W_t * H_t;
+
   PlainSVD svd;
-  mat W, H;
+  arma::mat W, H;
   double result = svd.Apply(test, 3, W, H);
   
-  BOOST_REQUIRE_LT(result, 0.1);
+  BOOST_REQUIRE_LT(result, 0.01);
 }
 
+
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-svn mailing list