[mlpack-svn] r13788 - in mlpack/trunk/src/mlpack/core/optimizers: . lbfgs

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Oct 30 16:56:24 EDT 2012


Author: rcurtin
Date: 2012-10-30 16:56:23 -0400 (Tue, 30 Oct 2012)
New Revision: 13788

Modified:
   mlpack/trunk/src/mlpack/core/optimizers/CMakeLists.txt
   mlpack/trunk/src/mlpack/core/optimizers/lbfgs/test_functions.cpp
   mlpack/trunk/src/mlpack/core/optimizers/lbfgs/test_functions.hpp
Log:
Add SGD, and add functionality to LBFGS test functions (which will probably be
removed later).


Modified: mlpack/trunk/src/mlpack/core/optimizers/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/CMakeLists.txt	2012-10-30 20:56:00 UTC (rev 13787)
+++ mlpack/trunk/src/mlpack/core/optimizers/CMakeLists.txt	2012-10-30 20:56:23 UTC (rev 13788)
@@ -1,6 +1,7 @@
 set(DIRS
   aug_lagrangian
   lbfgs
+  sgd
 )
 
 foreach(dir ${DIRS})

Modified: mlpack/trunk/src/mlpack/core/optimizers/lbfgs/test_functions.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/lbfgs/test_functions.cpp	2012-10-30 20:56:00 UTC (rev 13787)
+++ mlpack/trunk/src/mlpack/core/optimizers/lbfgs/test_functions.cpp	2012-10-30 20:56:23 UTC (rev 13788)
@@ -139,6 +139,7 @@
  * Calculate the objective function.
  */
 double GeneralizedRosenbrockFunction::Evaluate(const arma::mat& coordinates)
+    const
 {
   double fval = 0;
   for (int i = 0; i < (n - 1); i++)
@@ -154,7 +155,7 @@
  * Calculate the gradient.
  */
 void GeneralizedRosenbrockFunction::Gradient(const arma::mat& coordinates,
-                                             arma::mat& gradient)
+                                             arma::mat& gradient) const
 {
   gradient.set_size(n);
   for (int i = 0; i < (n - 1); i++)
@@ -170,6 +171,26 @@
       std::pow(coordinates[n - 2], 2));
 }
 
+//! Calculate the objective function of one of the individual functions.
+double GeneralizedRosenbrockFunction::Evaluate(const arma::mat& coordinates,
+                                               const size_t i) const
+{
+  return 100 * std::pow(std::pow(coordinates[i], 3) - coordinates[i] *
+      coordinates[i + 1]) + 2 * (coordinates[i] - 1);
+}
+
+//! Calculate the gradient of one of the individual functions.
+void GeneralizedRosenbrockFunction::Gradient(const arma::mat& coordinates,
+                                             const size_t i,
+                                             arma::mat& gradient) const
+{
+  gradient.zeros(n);
+
+  gradient[i] = 400 * std::pow(coordinates[i], 3) + (2 + coordinates[i + 1]) *
+      coordinates[i] - 2;
+  gradient[i + 1] = 200 * (std::pow(coordinates[i], 2) + coordinates[i + 1]);
+}
+
 const arma::mat& GeneralizedRosenbrockFunction::GetInitialPoint() const
 {
   return initialPoint;

Modified: mlpack/trunk/src/mlpack/core/optimizers/lbfgs/test_functions.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/lbfgs/test_functions.hpp	2012-10-30 20:56:00 UTC (rev 13787)
+++ mlpack/trunk/src/mlpack/core/optimizers/lbfgs/test_functions.hpp	2012-10-30 20:56:23 UTC (rev 13788)
@@ -97,6 +97,11 @@
  *
  * This should optimize to f(x) = 0, at x = [1, 1, 1, 1, ...].
  *
+ * This function can also be used for stochastic gradient descent (SGD) as a
+ * decomposable function (DecomposableFunctionType), so there are other
+ * overloads of Evaluate() and Gradient() implemented, as well as
+ * NumFunctions().
+ *
  * "An analysis of the behavior of a glass of genetic adaptive systems."
  *   K.A. De Jong.  Ph.D. thesis, University of Michigan, 1975.
  */
@@ -110,9 +115,15 @@
    */
   GeneralizedRosenbrockFunction(int n);
 
-  double Evaluate(const arma::mat& coordinates);
-  void Gradient(const arma::mat& coordinates, arma::mat& gradient);
+  double Evaluate(const arma::mat& coordinates) const;
+  void Gradient(const arma::mat& coordinates, arma::mat& gradient) const;
 
+  size_t NumFunctions() const { return n - 1; }
+  double Evaluate(const arma::mat& coordinates, const size_t i) const;
+  void Gradient(const arma::mat& coordinates,
+                const size_t i,
+                arma::mat& gradient) const;
+
   const arma::mat& GetInitialPoint() const;
 
  private:




More information about the mlpack-svn mailing list