[mlpack-svn] r16213 - in mlpack/trunk/src/mlpack: methods/cf methods/emst methods/fastmks methods/gmm methods/kernel_pca methods/kmeans methods/lars methods/linear_regression methods/local_coordinate_coding methods/logistic_regression tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Feb 5 14:33:27 EST 2014


Author: birm
Date: Wed Feb  5 14:33:26 2014
New Revision: 16213

Log:
Added and Modified more ToString methods.


Modified:
   mlpack/trunk/src/mlpack/methods/cf/cf.cpp
   mlpack/trunk/src/mlpack/methods/cf/cf.hpp
   mlpack/trunk/src/mlpack/methods/emst/dtb_impl.hpp
   mlpack/trunk/src/mlpack/methods/fastmks/fastmks_impl.hpp
   mlpack/trunk/src/mlpack/methods/gmm/gmm_impl.hpp
   mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca.hpp
   mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp
   mlpack/trunk/src/mlpack/methods/kmeans/kmeans.hpp
   mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp
   mlpack/trunk/src/mlpack/methods/lars/lars.cpp
   mlpack/trunk/src/mlpack/methods/lars/lars.hpp
   mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp
   mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp
   mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.hpp
   mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc_impl.hpp
   mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression.hpp
   mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp
   mlpack/trunk/src/mlpack/tests/to_string_test.cpp

Modified: mlpack/trunk/src/mlpack/methods/cf/cf.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/cf/cf.cpp	(original)
+++ mlpack/trunk/src/mlpack/methods/cf/cf.cpp	Wed Feb  5 14:33:26 2014
@@ -260,6 +260,17 @@
   recommendations(pos, queryIndex) = neighbor;
 }
 
+// Return string of object.
+std::string CF::ToString() const
+{
+  std::ostringstream convert;
+  convert << "Collaborative Filtering [" << this << "]" << std::endl;
+  //convert << "  Number of Recommendations: " << numRecs << std::endl;
+  //convert << "  Number of Users for Similarity: " << numUsersForSimilarity;
+  //convert << std::endl;
+  //convert << "  Data: " << data.n_rows << "x" << data.n_cols << std::endl;
+  return convert.str();
+}
 
 }; // namespace mlpack
 }; // namespace cf

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	Wed Feb  5 14:33:26 2014
@@ -167,6 +167,11 @@
                           arma::Col<size_t>& users, size_t num,
                           size_t neighbours);
 
+  /**
+   * Returns a string representation of this object.
+   */
+  std::string ToString() const;
+
  private:
   //! Number of recommendations.
   size_t numRecs;

Modified: mlpack/trunk/src/mlpack/methods/emst/dtb_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/emst/dtb_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/emst/dtb_impl.hpp	Wed Feb  5 14:33:26 2014
@@ -300,10 +300,12 @@
 {
   std::ostringstream convert;
   convert << "Dual Tree Boruvka [" << this << "]" << std::endl;
-  //convert << "  Data: " << data.n_rows << "x" << data.n_cols <<std::endl;
-  //convert << "  Total Distance: " << totalDist <<std::endl;
-  //convert << "  Naive: " << naive << std::endl;
-  //convert << "  Metric" << metric << std::endl;
+  convert << "  Data: " << data.n_rows << "x" << data.n_cols <<std::endl;
+  convert << "  Total Distance: " << totalDist <<std::endl;
+  convert << "  Naive: " << naive << std::endl;
+  convert << "  Metric: " << std::endl;
+  convert << mlpack::util::Indent(mlpack::util::Indent(metric.ToString()));
+  convert << std::endl;
   return convert.str();
 }
 

Modified: mlpack/trunk/src/mlpack/methods/fastmks/fastmks_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/fastmks/fastmks_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/fastmks/fastmks_impl.hpp	Wed Feb  5 14:33:26 2014
@@ -308,7 +308,9 @@
   convert << "FastMKS [" << this << "]" << std::endl;
   convert << "  Naive: " << naive << std::endl;
   convert << "  Single: " << single << std::endl;
-  Log::Debug << "  Metric: " << metric << std::endl;
+  convert << "  Metric: " << std::endl;
+  convert << mlpack::util::Indent(mlpack::util::Indent(metric.ToString()));
+  convert << std::endl;
   return convert.str();
 }
 

Modified: mlpack/trunk/src/mlpack/methods/gmm/gmm_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/gmm/gmm_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/gmm/gmm_impl.hpp	Wed Feb  5 14:33:26 2014
@@ -480,7 +480,24 @@
 std::string GMM<FittingType>::ToString() const
 {
   std::ostringstream convert;
+  std::ostringstream data;
   convert << "GMM [" << this << "]" << std::endl;
+  convert << "  Gaussians: " << gaussians << std::endl;
+  convert << "  Dimensionality: "<<dimensionality;
+  convert << std::endl;
+  // Secondary ostringstream so things can be indented properly.
+  for (size_t ind=0; ind < gaussians; ind++)
+  {
+    data << "Means of Gaussian " << ind << ": " << std::endl << means[ind]; 
+    data << std::endl;
+    data << "Covariances of Gaussian " << ind << ": " << std::endl ;
+    data << covariances[ind] << std::endl;
+    data << "Weight of Gaussian " << ind << ": " << std::endl ;
+    data << weights[ind] << std::endl;
+  }
+
+  convert << util::Indent(data.str());
+
   return convert.str();
 }
 

Modified: mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca.hpp	Wed Feb  5 14:33:26 2014
@@ -91,6 +91,10 @@
   //! Return whether or not the transformed data is centered.
   bool& CenterTransformedData() { return centerTransformedData; }
 
+   
+  // Returns a string representation of this object. 
+  std::string ToString() const;
+
  private:
   //! The instantiated kernel.
   KernelType kernel;

Modified: mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp	Wed Feb  5 14:33:26 2014
@@ -120,6 +120,19 @@
       kernelMatrix(i, j) = kernelMatrix(j, i);
 }
 
+// Returns a String of the Object
+template <typename KernelType>
+std::string KernelPCA<KernelType>::ToString() const
+{
+  std::ostringstream convert;
+  convert << "KernelPCA [" << this << "]" << std::endl;
+  convert << "  Center Transformed: " << centerTransformedData <<std::endl;
+  convert << "  Kernel Type: " << std::endl;
+  convert <<  mlpack::util::Indent(mlpack::util::Indent(kernel.ToString()));
+  convert << std::endl;
+  return convert.str();
+}
+
 }; // namespace mlpack
 }; // namespace kpca
 

Modified: mlpack/trunk/src/mlpack/methods/kmeans/kmeans.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/kmeans/kmeans.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/kmeans/kmeans.hpp	Wed Feb  5 14:33:26 2014
@@ -178,6 +178,9 @@
   //! Modify the empty cluster policy.
   EmptyClusterPolicy& EmptyClusterAction() { return emptyClusterAction; }
 
+  // Returns a string representation of this object. 
+  std::string ToString() const;
+
  private:
   //! Factor controlling how many clusters are actually found.
   double overclusteringFactor;

Modified: mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp	Wed Feb  5 14:33:26 2014
@@ -803,5 +803,22 @@
   }
 }
 
+template<typename MetricType,
+         typename InitialPartitionPolicy,
+         typename EmptyClusterPolicy>
+std::string KMeans<MetricType,
+    InitialPartitionPolicy,
+    EmptyClusterPolicy>::ToString() const
+{
+  std::ostringstream convert;
+  convert << "KMeans [" << this << "]" << std::endl;
+  convert << "  Overclustering Factor: " << overclusteringFactor <<std::endl;
+  convert << "  Max Iterations: " << maxIterations <<std::endl;
+  convert << "  Metric: " << std::endl;
+  convert << mlpack::util::Indent(mlpack::util::Indent(metric.ToString()));
+  convert << std::endl;
+  return convert.str();
+}
+
 }; // namespace kmeans
 }; // namespace mlpack

Modified: mlpack/trunk/src/mlpack/methods/lars/lars.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/lars/lars.cpp	(original)
+++ mlpack/trunk/src/mlpack/methods/lars/lars.cpp	Wed Feb  5 14:33:26 2014
@@ -477,3 +477,14 @@
     matUtriCholFactor.shed_row(n);
   }
 }
+
+std::string LARS::ToString() const
+{
+  std::ostringstream convert;
+  convert << "LARS [" << this << "]" << std::endl;
+  convert << "  Gram Matrix: " << matGram.n_rows << "x" << matGram.n_cols;
+  convert << std::endl;
+  convert << "  Tolerance: " << tolerance << std::endl;
+  return convert.str();
+}
+

Modified: mlpack/trunk/src/mlpack/methods/lars/lars.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/lars/lars.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/lars/lars.hpp	Wed Feb  5 14:33:26 2014
@@ -150,6 +150,9 @@
 
   //! Access the upper triangular cholesky factor
   const arma::mat& MatUtriCholFactor() const { return matUtriCholFactor; }
+
+  // Returns a string representation of this object. 
+  std::string ToString() const;
   
  private:
   //! Gram matrix.

Modified: mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp	(original)
+++ mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.cpp	Wed Feb  5 14:33:26 2014
@@ -120,3 +120,11 @@
 
   return cost;
 }
+
+std::string LinearRegression::ToString() const
+{
+  std::ostringstream convert;
+  convert << "Linear Regression [" << this << "]" << std::endl;
+  convert << "  Lambda: " << lambda << std::endl;
+  return convert.str();
+}

Modified: mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/linear_regression/linear_regression.hpp	Wed Feb  5 14:33:26 2014
@@ -87,6 +87,9 @@
   //! Modify the Tikhonov regularization parameter for ridge regression.
   double& Lambda() { return lambda; }
 
+  // Returns a string representation of this object. 
+  std::string ToString() const;
+
  private:
   /**
    * The calculated B.

Modified: mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.hpp	Wed Feb  5 14:33:26 2014
@@ -130,6 +130,9 @@
   //! Modify the codes.
   arma::mat& Codes() { return codes; }
 
+  // Returns a string representation of this object. 
+  std::string ToString() const;
+
  private:
   //! Number of atoms in dictionary.
   size_t atoms;

Modified: mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc_impl.hpp	Wed Feb  5 14:33:26 2014
@@ -307,6 +307,16 @@
   double froNormResidual = norm(data - dictionary * codes, "fro");
   return std::pow(froNormResidual, 2.0) + lambda * weightedL1NormZ;
 }
+template<typename DictionaryInitializer>
+std::string LocalCoordinateCoding<DictionaryInitializer>::ToString() const
+{
+  std::ostringstream convert;
+  convert << "Local Coordinate Coding [" << this << "]" << std::endl;
+  convert << "  Number of Atoms: " << atoms << std::endl;
+  convert << "  Data: " << data.n_rows << "x" << data.n_cols << std::endl;
+  convert << "  Lambda: " << lambda << std::endl;
+  return convert.str();
+}
 
 }; // namespace lcc
 }; // namespace mlpack

Modified: mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression.hpp	Wed Feb  5 14:33:26 2014
@@ -130,6 +130,9 @@
   double ComputeError(const arma::mat& predictors,
                       const arma::vec& responses) const;
 
+  // Returns a string representation of this object. 
+  std::string ToString() const;
+
  private:
   //! Vector of trained parameters.
   arma::vec parameters;

Modified: mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp	Wed Feb  5 14:33:26 2014
@@ -124,6 +124,16 @@
   return (double) (count * 100) / responses.n_rows;
 }
 
+template <template<typename> class OptimizerType>
+std::string LogisticRegression<OptimizerType>::ToString() const
+{
+  std::ostringstream convert;
+  convert << "Logistic Regression [" << this << "]" << std::endl;
+  convert << "  Parameters: " << parameters.n_rows << std::endl;
+  convert << "  Lambda: " << lambda << std::endl;
+  return convert.str();
+}
+
 }; // namespace regression
 }; // namespace mlpack
 

Modified: mlpack/trunk/src/mlpack/tests/to_string_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/to_string_test.cpp	(original)
+++ mlpack/trunk/src/mlpack/tests/to_string_test.cpp	Wed Feb  5 14:33:26 2014
@@ -38,6 +38,13 @@
 #include <mlpack/methods/emst/dtb.hpp>
 #include <mlpack/methods/fastmks/fastmks.hpp>
 #include <mlpack/methods/gmm/gmm.hpp>
+#include <mlpack/methods/hmm/hmm.hpp>
+#include <mlpack/methods/kernel_pca/kernel_pca.hpp>
+#include <mlpack/methods/kmeans/kmeans.hpp>
+#include <mlpack/methods/lars/lars.hpp>
+#include <mlpack/methods/linear_regression/linear_regression.hpp>
+#include <mlpack/methods/local_coordinate_coding/lcc.hpp>
+#include <mlpack/methods/logistic_regression/logistic_regression.hpp>
 
 using namespace mlpack;
 using namespace mlpack::kernel;
@@ -236,13 +243,24 @@
 /**
 BOOST_AUTO_TEST_CASE(CFString)
 { 
-  arma::mat c(10,10);
-  mlpack::cf::CF d(c);
+  size_t a = 1 ;
+  arma::mat c(3,3);
+  c(0, 0) = 0;
+  c(0, 1) = 1;
+  c(0, 2) = 1.5;
+  c(1, 0) = 1;
+  c(1, 1) = 2;
+  c(1, 2) = 2.0;
+  c(2, 0) = 0;
+  c(2, 1) = 2;
+  c(2, 2) = 0.7;
+  mlpack::cf::CF d(a,a,c);
   Log::Debug << d;
   std::string s = d.ToString();
   BOOST_REQUIRE_NE(s, "");
 }
 **/
+
 BOOST_AUTO_TEST_CASE(DetString)
 { 
   arma::mat c(4,4);
@@ -254,9 +272,9 @@
 }
 
 BOOST_AUTO_TEST_CASE(EmstString)
-{ 
+{   
   arma::mat c(4,4);
-  c.randn(); 
+  c.randu();
   mlpack::emst::DualTreeBoruvka<> d(c);
   Log::Debug << d;
   std::string s = d.ToString();
@@ -283,4 +301,73 @@
   BOOST_REQUIRE_NE(s, "");
 }
 
+BOOST_AUTO_TEST_CASE(HMMString)
+{ 
+  mlpack::hmm::HMM<> d(5,4);
+  Log::Debug << d;
+  std::string s = d.ToString();
+  BOOST_REQUIRE_NE(s, "");
+}
+
+BOOST_AUTO_TEST_CASE(KPCAString)
+{ 
+  LinearKernel k;
+  mlpack::kpca::KernelPCA<LinearKernel> d(k,false);
+  Log::Debug << d;
+  std::string s = d.ToString();
+  BOOST_REQUIRE_NE(s, "");
+}
+
+BOOST_AUTO_TEST_CASE(KMeansString)
+{ 
+  mlpack::kmeans::KMeans<metric::ManhattanDistance> d(100, 4.0);
+  Log::Debug << d;
+  std::string s = d.ToString();
+  BOOST_REQUIRE_NE(s, "");
+}
+
+BOOST_AUTO_TEST_CASE(LarsString)
+{ 
+  mlpack::regression::LARS::LARS d(false);
+  Log::Debug << d;
+  std::string s = d.ToString();
+  BOOST_REQUIRE_NE(s, "");
+}
+
+BOOST_AUTO_TEST_CASE(LinRegString)
+{ 
+  arma::mat c(40,40);
+  arma::mat b(40,1);
+  c.randn();
+  b.randn();
+  mlpack::regression::LinearRegression::LinearRegression d(c,b);
+  Log::Debug << d;
+  std::string s = d.ToString();
+  BOOST_REQUIRE_NE(s, "");
+}
+
+BOOST_AUTO_TEST_CASE(LCCString)
+{ 
+  arma::mat c(40,40);
+  const size_t b=3;
+  const double a=1;
+  c.randn();
+  mlpack::lcc::LocalCoordinateCoding<> d(c,b,a);
+  Log::Debug << d;
+  std::string s = d.ToString();
+  BOOST_REQUIRE_NE(s, "");
+}
+
+BOOST_AUTO_TEST_CASE(LogRegString)
+{ 
+  arma::mat c(40,40);
+  arma::mat b(40,1);
+  c.randn();
+  b.randn();
+  mlpack::regression::LogisticRegression<> d(c,b);
+  Log::Debug << d;
+  std::string s = d.ToString();
+  BOOST_REQUIRE_NE(s, "");
+}
+
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-svn mailing list