mlpack  1.0.12
lbfgs.hpp
Go to the documentation of this file.
1 
15 #ifndef __MLPACK_CORE_OPTIMIZERS_LBFGS_LBFGS_HPP
16 #define __MLPACK_CORE_OPTIMIZERS_LBFGS_LBFGS_HPP
17 
18 #include <mlpack/core.hpp>
19 
20 namespace mlpack {
21 namespace optimization {
22 
35 template<typename FunctionType>
36 class L_BFGS
37 {
38  public:
59  L_BFGS(FunctionType& function,
60  const size_t numBasis = 5, /* entirely arbitrary */
61  const size_t maxIterations = 0, /* run forever */
62  const double armijoConstant = 1e-4,
63  const double wolfe = 0.9,
64  const double minGradientNorm = 1e-10,
65  const size_t maxLineSearchTrials = 50,
66  const double minStep = 1e-20,
67  const double maxStep = 1e20);
68 
75  const std::pair<arma::mat, double>& MinPointIterate() const;
76 
88  double Optimize(arma::mat& iterate);
89 
102  double Optimize(arma::mat& iterate, const size_t maxIterations);
103 
105  const FunctionType& Function() const { return function; }
107  FunctionType& Function() { return function; }
108 
110  size_t NumBasis() const { return numBasis; }
112  size_t& NumBasis() { return numBasis; }
113 
115  size_t MaxIterations() const { return maxIterations; }
117  size_t& MaxIterations() { return maxIterations; }
118 
120  double ArmijoConstant() const { return armijoConstant; }
122  double& ArmijoConstant() { return armijoConstant; }
123 
125  double Wolfe() const { return wolfe; }
127  double& Wolfe() { return wolfe; }
128 
130  double MinGradientNorm() const { return minGradientNorm; }
132  double& MinGradientNorm() { return minGradientNorm; }
133 
135  size_t MaxLineSearchTrials() const { return maxLineSearchTrials; }
138 
140  double MinStep() const { return minStep; }
142  double& MinStep() { return minStep; }
143 
145  double MaxStep() const { return maxStep; }
147  double& MaxStep() { return maxStep; }
148 
149  // convert the obkect into a string
150  std::string ToString() const;
151 
152  private:
154  FunctionType& function;
155 
157  arma::mat newIterateTmp;
159  arma::cube s;
161  arma::cube y;
162 
164  size_t numBasis;
170  double wolfe;
176  double minStep;
178  double maxStep;
179 
181  std::pair<arma::mat, double> minPointIterate;
182 
189  double Evaluate(const arma::mat& iterate);
190 
198  double ChooseScalingFactor(const size_t iterationNum,
199  const arma::mat& gradient);
200 
207  bool GradientNormTooSmall(const arma::mat& gradient);
208 
222  bool LineSearch(double& functionValue,
223  arma::mat& iterate,
224  arma::mat& gradient,
225  const arma::mat& searchDirection);
226 
235  void SearchDirection(const arma::mat& gradient,
236  const size_t iterationNum,
237  const double scalingFactor,
238  arma::mat& searchDirection);
239 
251  void UpdateBasisSet(const size_t iterationNum,
252  const arma::mat& iterate,
253  const arma::mat& oldIterate,
254  const arma::mat& gradient,
255  const arma::mat& oldGradient);
256 };
257 
258 }; // namespace optimization
259 }; // namespace mlpack
260 
261 #include "lbfgs_impl.hpp"
262 
263 #endif // __MLPACK_CORE_OPTIMIZERS_LBFGS_LBFGS_HPP
264 
double ArmijoConstant() const
Get the Armijo condition constant.
Definition: lbfgs.hpp:120
L_BFGS(FunctionType &function, const size_t numBasis=5, const size_t maxIterations=0, const double armijoConstant=1e-4, const double wolfe=0.9, const double minGradientNorm=1e-10, const size_t maxLineSearchTrials=50, const double minStep=1e-20, const double maxStep=1e20)
Initialize the L-BFGS object.
void SearchDirection(const arma::mat &gradient, const size_t iterationNum, const double scalingFactor, arma::mat &searchDirection)
Find the L-BFGS search direction.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
size_t & MaxIterations()
Modify the maximum number of iterations.
Definition: lbfgs.hpp:117
double Wolfe() const
Get the Wolfe parameter.
Definition: lbfgs.hpp:125
size_t maxLineSearchTrials
Maximum number of trials for the line search.
Definition: lbfgs.hpp:174
arma::cube y
Stores all the y matrices in memory.
Definition: lbfgs.hpp:161
size_t & NumBasis()
Modify the memory size.
Definition: lbfgs.hpp:112
size_t MaxIterations() const
Get the maximum number of iterations.
Definition: lbfgs.hpp:115
bool GradientNormTooSmall(const arma::mat &gradient)
Check to make sure that the norm of the gradient is not smaller than 1e-5.
double & ArmijoConstant()
Modify the Armijo condition constant.
Definition: lbfgs.hpp:122
double ChooseScalingFactor(const size_t iterationNum, const arma::mat &gradient)
Calculate the scaling factor, gamma, which is used to scale the Hessian approximation matrix...
double minGradientNorm
Minimum gradient norm required to continue the optimization.
Definition: lbfgs.hpp:172
double maxStep
Maximum step of the line search.
Definition: lbfgs.hpp:178
double minStep
Minimum step of the line search.
Definition: lbfgs.hpp:176
double & MinGradientNorm()
Modify the minimum gradient norm.
Definition: lbfgs.hpp:132
double armijoConstant
Parameter for determining the Armijo condition.
Definition: lbfgs.hpp:168
double & MaxStep()
Modify the maximum line search step size.
Definition: lbfgs.hpp:147
size_t & MaxLineSearchTrials()
Modify the maximum number of line search trials.
Definition: lbfgs.hpp:137
bool LineSearch(double &functionValue, arma::mat &iterate, arma::mat &gradient, const arma::mat &searchDirection)
Perform a back-tracking line search along the search direction to calculate a step size satisfying th...
FunctionType & Function()
Modify the function that is being optimized.
Definition: lbfgs.hpp:107
double & Wolfe()
Modify the Wolfe parameter.
Definition: lbfgs.hpp:127
const FunctionType & Function() const
Return the function that is being optimized.
Definition: lbfgs.hpp:105
arma::mat newIterateTmp
Position of the new iterate.
Definition: lbfgs.hpp:157
arma::cube s
Stores all the s matrices in memory.
Definition: lbfgs.hpp:159
double & MinStep()
Modify the minimum line search step size.
Definition: lbfgs.hpp:142
size_t maxIterations
Maximum number of iterations.
Definition: lbfgs.hpp:166
size_t NumBasis() const
Get the memory size.
Definition: lbfgs.hpp:110
size_t MaxLineSearchTrials() const
Get the maximum number of line search trials.
Definition: lbfgs.hpp:135
const std::pair< arma::mat, double > & MinPointIterate() const
Return the point where the lowest function value has been found.
double Evaluate(const arma::mat &iterate)
Evaluate the function at the given iterate point and store the result if it is a new minimum...
std::pair< arma::mat, double > minPointIterate
Best point found so far.
Definition: lbfgs.hpp:181
double Optimize(arma::mat &iterate)
Use L-BFGS to optimize the given function, starting at the given iterate point and finding the minimu...
The generic L-BFGS optimizer, which uses a back-tracking line search algorithm to minimize a function...
Definition: lbfgs.hpp:36
double wolfe
Parameter for detecting the Wolfe condition.
Definition: lbfgs.hpp:170
double MinStep() const
Return the minimum line search step size.
Definition: lbfgs.hpp:140
double MaxStep() const
Return the maximum line search step size.
Definition: lbfgs.hpp:145
std::string ToString() const
double MinGradientNorm() const
Get the minimum gradient norm.
Definition: lbfgs.hpp:130
void UpdateBasisSet(const size_t iterationNum, const arma::mat &iterate, const arma::mat &oldIterate, const arma::mat &gradient, const arma::mat &oldGradient)
Update the y and s matrices, which store the differences between the iterate and old iterate and the ...
size_t numBasis
Size of memory for this L-BFGS optimizer.
Definition: lbfgs.hpp:164