[mlpack-svn] r13792 - mlpack/trunk/src/mlpack/core/optimizers/sgd
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Oct 30 18:47:55 EDT 2012
Author: rcurtin
Date: 2012-10-30 18:47:54 -0400 (Tue, 30 Oct 2012)
New Revision: 13792
Added:
mlpack/trunk/src/mlpack/core/optimizers/sgd/test_function.cpp
mlpack/trunk/src/mlpack/core/optimizers/sgd/test_function.hpp
Modified:
mlpack/trunk/src/mlpack/core/optimizers/sgd/CMakeLists.txt
mlpack/trunk/src/mlpack/core/optimizers/sgd/sgd.hpp
mlpack/trunk/src/mlpack/core/optimizers/sgd/sgd_impl.hpp
Log:
Add a test function and make it work right.
Modified: mlpack/trunk/src/mlpack/core/optimizers/sgd/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/sgd/CMakeLists.txt 2012-10-30 22:40:48 UTC (rev 13791)
+++ mlpack/trunk/src/mlpack/core/optimizers/sgd/CMakeLists.txt 2012-10-30 22:47:54 UTC (rev 13792)
@@ -1,6 +1,8 @@
set(SOURCES
sgd.hpp
sgd_impl.hpp
+ test_function.hpp
+ test_function.cpp
)
set(DIR_SRCS)
Modified: mlpack/trunk/src/mlpack/core/optimizers/sgd/sgd.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/sgd/sgd.hpp 2012-10-30 22:40:48 UTC (rev 13791)
+++ mlpack/trunk/src/mlpack/core/optimizers/sgd/sgd.hpp 2012-10-30 22:47:54 UTC (rev 13792)
@@ -137,7 +137,7 @@
//! Controls whether or not the individual functions are shuffled when
//! iterating.
bool shuffle;
-}
+};
}; // namespace optimization
}; // namespace mlpack
Modified: mlpack/trunk/src/mlpack/core/optimizers/sgd/sgd_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/sgd/sgd_impl.hpp 2012-10-30 22:40:48 UTC (rev 13791)
+++ mlpack/trunk/src/mlpack/core/optimizers/sgd/sgd_impl.hpp 2012-10-30 22:47:54 UTC (rev 13792)
@@ -36,7 +36,8 @@
// This is used only if shuffle is true.
arma::vec visitationOrder;
if (shuffle)
- visitationOrder = shuffle(linspace(0, (numFunctions - 1), numFunctions));
+ visitationOrder = arma::shuffle(arma::linspace(0, (numFunctions - 1),
+ numFunctions));
// To keep track of where we are and how things are going.
size_t currentFunction = 0;
@@ -58,6 +59,13 @@
Log::Info << "SGD: iteration " << i << ", objective " << overallObjective
<< "." << std::endl;
+ if (overallObjective != overallObjective)
+ {
+ Log::Warn << "SGD: converged to " << overallObjective << "; terminating"
+ << " with failure. Try a smaller step size?" << std::endl;
+ return overallObjective;
+ }
+
if (std::abs(lastObjective - overallObjective) < tolerance)
{
Log::Info << "SGD: minimized within tolerance " << tolerance << "; "
@@ -71,7 +79,7 @@
currentFunction = 0;
if (shuffle) // Determine order of visitation.
- visitationOrder = shuffle(visitationOrder);
+ visitationOrder = arma::shuffle(visitationOrder);
}
// Evaluate the gradient for this iteration.
Added: mlpack/trunk/src/mlpack/core/optimizers/sgd/test_function.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/sgd/test_function.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/core/optimizers/sgd/test_function.cpp 2012-10-30 22:47:54 UTC (rev 13792)
@@ -0,0 +1,55 @@
+/**
+ * @file test_function.cpp
+ * @author Ryan Curtin
+ *
+ * Implementation of very simple test function for stochastic gradient descent
+ * (SGD).
+ */
+#include "test_function.hpp"
+
+using namespace mlpack;
+using namespace mlpack::optimization;
+using namespace mlpack::optimization::test;
+
+double SGDTestFunction::Evaluate(const arma::mat& coordinates, const size_t i)
+ const
+{
+ switch (i)
+ {
+ case 0:
+ return -std::exp(-std::abs(coordinates[0]));
+
+ case 1:
+ return std::pow(coordinates[1], 2);
+
+ case 2:
+ return std::pow(coordinates[2], 4) + 3 * std::pow(coordinates[2], 2);
+
+ default:
+ return 0;
+ }
+}
+
+void SGDTestFunction::Gradient(const arma::mat& coordinates,
+ const size_t i,
+ arma::mat& gradient) const
+{
+ gradient.zeros(3);
+ switch (i)
+ {
+ case 0:
+ if (coordinates[0] >= 0)
+ gradient[0] = std::exp(-coordinates[0]);
+ else
+ gradient[0] = -std::exp(coordinates[1]);
+ break;
+
+ case 1:
+ gradient[1] = 2 * coordinates[1];
+ break;
+
+ case 2:
+ gradient[2] = 4 * std::pow(coordinates[2], 3) + 6 * coordinates[2];
+ break;
+ }
+}
Added: mlpack/trunk/src/mlpack/core/optimizers/sgd/test_function.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/sgd/test_function.hpp (rev 0)
+++ mlpack/trunk/src/mlpack/core/optimizers/sgd/test_function.hpp 2012-10-30 22:47:54 UTC (rev 13792)
@@ -0,0 +1,45 @@
+/**
+ * @file test_function.hpp
+ * @author Ryan Curtin
+ *
+ * Very simple test function for SGD.
+ */
+#ifndef __MLPACK_CORE_OPTIMIZERS_SGD_TEST_FUNCTION_HPP
+#define __MLPACK_CORE_OPTIMIZERS_SGD_TEST_FUNCTION_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace optimization {
+namespace test {
+
+//! Very, very simple test function which is the composite of three other
+//! functions. It turns out that although this function is very simple,
+//! optimizing it fully can take a very long time. It seems to take in excess
+//! of 10 million iterations with a step size of 0.0005.
+class SGDTestFunction
+{
+ public:
+ //! Nothing to do for the constructor.
+ SGDTestFunction() { }
+
+ //! Return 3 (the number of functions).
+ size_t NumFunctions() const { return 3; }
+
+ //! Get the starting point.
+ arma::mat GetInitialPoint() const { return arma::mat("6; -45.6; 6.2"); }
+
+ //! Evaluate a function.
+ double Evaluate(const arma::mat& coordinates, const size_t i) const;
+
+ //! Evaluate the gradient of a function.
+ void Gradient(const arma::mat& coordinates,
+ const size_t i,
+ arma::mat& gradient) const;
+};
+
+}; // namespace test
+}; // namespace optimization
+}; // namespace mlpack
+
+#endif
More information about the mlpack-svn
mailing list