[mlpack-svn] r15334 - mlpack/trunk/src/mlpack/core/optimizers/lbfgs
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Jun 26 11:45:31 EDT 2013
Author: rcurtin
Date: Wed Jun 26 11:45:03 2013
New Revision: 15334
Log:
Move constructor to top of file, and check the size of the s and y cubes before
optimizing (not just at construction time) because they can be changed (see
nca_main.cpp).
Modified:
mlpack/trunk/src/mlpack/core/optimizers/lbfgs/lbfgs_impl.hpp
Modified: mlpack/trunk/src/mlpack/core/optimizers/lbfgs/lbfgs_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/lbfgs/lbfgs_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/core/optimizers/lbfgs/lbfgs_impl.hpp Wed Jun 26 11:45:03 2013
@@ -12,6 +12,57 @@
namespace optimization {
/**
+ * Initialize the L_BFGS object. Copy the function we will be optimizing and
+ * set the size of the memory for the algorithm.
+ *
+ * @param function Instance of function to be optimized
+ * @param numBasis Number of memory points to be stored
+ * @param armijoConstant Controls the accuracy of the line search routine for
+ * determining the Armijo condition.
+ * @param wolfe Parameter for detecting the Wolfe condition.
+ * @param minGradientNorm Minimum gradient norm required to continue the
+ * optimization.
+ * @param maxLineSearchTrials The maximum number of trials for the line search
+ * (before giving up).
+ * @param minStep The minimum step of the line search.
+ * @param maxStep The maximum step of the line search.
+ */
+template<typename FunctionType>
+L_BFGS<FunctionType>::L_BFGS(FunctionType& function,
+ const size_t numBasis,
+ const size_t maxIterations,
+ const double armijoConstant,
+ const double wolfe,
+ const double minGradientNorm,
+ const size_t maxLineSearchTrials,
+ const double minStep,
+ const double maxStep) :
+ function(function),
+ numBasis(numBasis),
+ maxIterations(maxIterations),
+ armijoConstant(armijoConstant),
+ wolfe(wolfe),
+ minGradientNorm(minGradientNorm),
+ maxLineSearchTrials(maxLineSearchTrials),
+ minStep(minStep),
+ maxStep(maxStep)
+{
+ // Get the dimensions of the coordinates of the function; GetInitialPoint()
+ // might return an arma::vec, but that's okay because then n_cols will simply
+ // be 1.
+ const size_t rows = function.GetInitialPoint().n_rows;
+ const size_t cols = function.GetInitialPoint().n_cols;
+
+ newIterateTmp.set_size(rows, cols);
+ s.set_size(rows, cols, numBasis);
+ y.set_size(rows, cols, numBasis);
+
+ // Allocate the pair holding the min iterate information.
+ minPointIterate.first.zeros(rows, cols);
+ minPointIterate.second = std::numeric_limits<double>::max();
+}
+
+/**
* Evaluate the function at the given iterate point and store the result if
* it is a new minimum.
*
@@ -255,57 +306,6 @@
}
/**
- * Initialize the L_BFGS object. Copy the function we will be optimizing and
- * set the size of the memory for the algorithm.
- *
- * @param function Instance of function to be optimized
- * @param numBasis Number of memory points to be stored
- * @param armijoConstant Controls the accuracy of the line search routine for
- * determining the Armijo condition.
- * @param wolfe Parameter for detecting the Wolfe condition.
- * @param minGradientNorm Minimum gradient norm required to continue the
- * optimization.
- * @param maxLineSearchTrials The maximum number of trials for the line search
- * (before giving up).
- * @param minStep The minimum step of the line search.
- * @param maxStep The maximum step of the line search.
- */
-template<typename FunctionType>
-L_BFGS<FunctionType>::L_BFGS(FunctionType& function,
- const size_t numBasis,
- const size_t maxIterations,
- const double armijoConstant,
- const double wolfe,
- const double minGradientNorm,
- const size_t maxLineSearchTrials,
- const double minStep,
- const double maxStep) :
- function(function),
- numBasis(numBasis),
- maxIterations(maxIterations),
- armijoConstant(armijoConstant),
- wolfe(wolfe),
- minGradientNorm(minGradientNorm),
- maxLineSearchTrials(maxLineSearchTrials),
- minStep(minStep),
- maxStep(maxStep)
-{
- // Get the dimensions of the coordinates of the function; GetInitialPoint()
- // might return an arma::vec, but that's okay because then n_cols will simply
- // be 1.
- const size_t rows = function.GetInitialPoint().n_rows;
- const size_t cols = function.GetInitialPoint().n_cols;
-
- newIterateTmp.set_size(rows, cols);
- s.set_size(rows, cols, numBasis);
- y.set_size(rows, cols, numBasis);
-
- // Allocate the pair holding the min iterate information.
- minPointIterate.first.zeros(rows, cols);
- minPointIterate.second = std::numeric_limits<double>::max();
-}
-
-/**
* Return the point where the lowest function value has been found.
*
* @return arma::vec representing the point and a double with the function
@@ -337,6 +337,15 @@
double L_BFGS<FunctionType>::Optimize(arma::mat& iterate,
const size_t maxIterations)
{
+ // Ensure that the cubes holding past iterations' information are the right
+ // size. Also set the current best point value to the maximum.
+ const size_t rows = function.GetInitialPoint().n_rows;
+ const size_t cols = function.GetInitialPoint().n_cols;
+
+ s.set_size(rows, cols, numBasis);
+ y.set_size(rows, cols, numBasis);
+ minPointIterate.second = std::numeric_limits<double>::max();
+
// The old iterate to be saved.
arma::mat oldIterate;
oldIterate.zeros(iterate.n_rows, iterate.n_cols);
More information about the mlpack-svn
mailing list