[mlpack-git] master, mlpack-1.0.x: Updated Test and pspectrum_string_kernel (9c5ddc0)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:43:29 EST 2015


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

On branches: master,mlpack-1.0.x
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

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

commit 9c5ddc0adc106e7858f19de0c94708d48f0ca2a1
Author: birm <birm at gatech.edu>
Date:   Mon Feb 10 17:12:51 2014 +0000

    Updated Test and pspectrum_string_kernel


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

9c5ddc0adc106e7858f19de0c94708d48f0ca2a1
 .../core/kernels/pspectrum_string_kernel.hpp       |   6 ++
 src/mlpack/core/optimizers/lrsdp/lrsdp.cpp         |   5 +-
 src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp    | 111 +++++++++++++++++++++
 src/mlpack/tests/to_string_test.cpp                |  13 ++-
 4 files changed, 131 insertions(+), 4 deletions(-)

diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp
index 0df22ba..2b4cdfa 100644
--- a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp
+++ b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp
@@ -101,6 +101,12 @@ class PSpectrumStringKernel
   std::string ToString() const{
     std::ostringstream convert;
     convert << "PSpectrumStringKernel [" << this << "]" << std::endl;
+    convert << "  p used: " << p << std::endl;
+    convert << "  Dataset:" << datasets.size() << std::endl;
+    std::ostringstream convertb;
+    for (size_t ind=0; ind < datasets.size(); ind++)
+      convertb << datasets[ind].size();
+    convert << mlpack::util::Indent(convertb.str(),2);
     return convert.str();
   }
  private:
diff --git a/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp b/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp
index be24921..b945caa 100644
--- a/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp
+++ b/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp
@@ -84,10 +84,9 @@ std::string LRSDP::ToString() const
 {
   std::ostringstream convert;
   convert << "LRSDP [" << this << "]" << std::endl;
-  convert << "Matrix Size: " << c.n_rows << "x" << c.n_cols << std::endl;
-  convert << "Initial point Size : " << initialPoint.n_rows << "x"
+  convert << "  Matrix Size: " << c.n_rows << "x" << c.n_cols << std::endl;
+  convert << "  Initial point Size : " << initialPoint.n_rows << "x"
       << initialPoint.n_cols << std::endl;
-  //convert << "AugLagrangian Info: " << std::endl << augLag;
   return convert.str();
 }
 
diff --git a/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp b/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp
new file mode 100644
index 0000000..af910f2
--- /dev/null
+++ b/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp
@@ -0,0 +1,111 @@
+/**
+ * @file lrsdp_impl.hpp
+ * @author Ryan Curtin
+ *
+ * An implementation of Monteiro and Burer's formulation of low-rank
+ * semidefinite programs (LR-SDP).
+ */
+#ifndef __MLPACK_CORE_OPTIMIZERS_LRSDP_LRSDP_IMPL_HPP
+#define __MLPACK_CORE_OPTIMIZERS_LRSDP_LRSDP_IMPL_HPP
+
+// In case it hasn't already been included.
+#include "lrsdp.hpp"
+
+namespace mlpack {
+namespace optimization {
+
+
+// Custom specializations of the AugmentedLagrangianFunction for the LRSDP case.
+template<>
+double AugLagrangianFunction<LRSDP>::Evaluate(const arma::mat& coordinates)
+    const
+{
+  // We can calculate the entire objective in a smart way.
+  // L(R, y, s) = Tr(C * (R R^T)) -
+  //     sum_{i = 1}^{m} (y_i (Tr(A_i * (R R^T)) - b_i)) +
+  //     (sigma / 2) * sum_{i = 1}^{m} (Tr(A_i * (R R^T)) - b_i)^2
+
+  // Let's start with the objective: Tr(C * (R R^T)).
+  // Simple, possibly slow solution.
+  arma::mat rrt = coordinates * trans(coordinates);
+  double objective = trace(function.C() * rrt);
+
+  // Now each constraint.
+  for (size_t i = 0; i < function.B().n_elem; ++i)
+  {
+    // Take the trace subtracted by the b_i.
+    double constraint = -function.B()[i];
+
+    if (function.AModes()[i] == 0)
+    {
+      constraint += trace(function.A()[i] * rrt);
+    }
+    else
+    {
+      for (size_t j = 0; j < function.A()[i].n_cols; ++j)
+      {
+        constraint += function.A()[i](2, j) *
+            rrt(function.A()[i](0, j), function.A()[i](1, j));
+      }
+    }
+
+    objective -= (lambda[i] * constraint);
+    objective += (sigma / 2) * std::pow(constraint, 2.0);
+  }
+
+  return objective;
+}
+
+template<>
+void AugLagrangianFunction<LRSDP>::Gradient(const arma::mat& coordinates,
+                                            arma::mat& gradient) const
+{
+  // We can calculate the gradient in a smart way.
+  // L'(R, y, s) = 2 * S' * R
+  //   with
+  // S' = C - sum_{i = 1}^{m} y'_i A_i
+  // y'_i = y_i - sigma * (Trace(A_i * (R R^T)) - b_i)
+  arma::mat rrt = coordinates * trans(coordinates);
+  arma::mat s = function.C();
+
+  for (size_t i = 0; i < function.B().n_elem; ++i)
+  {
+    double constraint = -function.B()[i];
+
+    if (function.AModes()[i] == 0)
+    {
+      constraint += trace(function.A()[i] * rrt);
+    }
+    else
+    {
+      for (size_t j = 0; j < function.A()[i].n_cols; ++j)
+      {
+        constraint += function.A()[i](2, j) *
+            rrt(function.A()[i](0, j), function.A()[i](1, j));
+      }
+    }
+
+    double y = lambda[i] - sigma * constraint;
+
+    if (function.AModes()[i] == 0)
+    {
+      s -= (y * function.A()[i]);
+    }
+    else
+    {
+      // We only need to subtract the entries which could be modified.
+      for (size_t j = 0; j < function.A()[i].n_cols; ++j)
+      {
+        s(function.A()[i](0, j), function.A()[i](1, j)) -= y;
+      }
+    }
+  }
+
+  gradient = 2 * s * coordinates;
+}
+
+}; // namespace optimization
+}; // namespace mlpack
+
+#endif
+
diff --git a/src/mlpack/tests/to_string_test.cpp b/src/mlpack/tests/to_string_test.cpp
index 70dad30..c123f2a 100644
--- a/src/mlpack/tests/to_string_test.cpp
+++ b/src/mlpack/tests/to_string_test.cpp
@@ -21,7 +21,7 @@
 
 #include <mlpack/core/optimizers/aug_lagrangian/aug_lagrangian.hpp>
 #include <mlpack/core/optimizers/lbfgs/lbfgs.hpp>
-//#include <mlpack/core/optimizers/lrsdp/lrsdp.hpp>
+#include <mlpack/core/optimizers/lrsdp/lrsdp.hpp>
 #include <mlpack/core/optimizers/sgd/sgd.hpp>
 #include <mlpack/methods/nca/nca_softmax_error_function.hpp>
 #include <mlpack/core/optimizers/aug_lagrangian/aug_lagrangian_test_functions.hpp>
@@ -229,6 +229,17 @@ BOOST_AUTO_TEST_CASE(AugLagString)
   BOOST_REQUIRE_NE(s, "");
 }
 
+BOOST_AUTO_TEST_CASE(LRSDPString)
+{
+  arma::mat c(40, 40);
+  c.randn();
+  const size_t b=3;
+  mlpack::optimization::LRSDP d(b,c);
+  Log::Debug << d;
+  std::string s = d.ToString();
+  BOOST_REQUIRE_NE(s, "");
+}
+
 BOOST_AUTO_TEST_CASE(BallBoundString)
 {
   BallBound<> d(3.5, "1.0 2.0");



More information about the mlpack-git mailing list