mlpack  master
lbfgs.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_OPTIMIZERS_LBFGS_LBFGS_HPP
14 #define MLPACK_CORE_OPTIMIZERS_LBFGS_LBFGS_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace optimization {
20 
33 template<typename FunctionType>
34 class L_BFGS
35 {
36  public:
57  L_BFGS(FunctionType& function,
58  const size_t numBasis = 10, /* same default as scipy */
59  const size_t maxIterations = 10000, /* many but not infinite */
60  const double armijoConstant = 1e-4,
61  const double wolfe = 0.9,
62  const double minGradientNorm = 1e-6,
63  const double factr = 1e-15,
64  const size_t maxLineSearchTrials = 50,
65  const double minStep = 1e-20,
66  const double maxStep = 1e20);
67 
74  const std::pair<arma::mat, double>& MinPointIterate() const;
75 
87  double Optimize(arma::mat& iterate);
88 
101  double Optimize(arma::mat& iterate, const size_t maxIterations);
102 
104  const FunctionType& Function() const { return function; }
106  FunctionType& Function() { return function; }
107 
109  size_t NumBasis() const { return numBasis; }
111  size_t& NumBasis() { return numBasis; }
112 
114  size_t MaxIterations() const { return maxIterations; }
116  size_t& MaxIterations() { return maxIterations; }
117 
119  double ArmijoConstant() const { return armijoConstant; }
121  double& ArmijoConstant() { return armijoConstant; }
122 
124  double Wolfe() const { return wolfe; }
126  double& Wolfe() { return wolfe; }
127 
129  double MinGradientNorm() const { return minGradientNorm; }
131  double& MinGradientNorm() { return minGradientNorm; }
132 
134  double Factr() const { return factr; }
136  double& Factr() { return factr; }
137 
139  size_t MaxLineSearchTrials() const { return maxLineSearchTrials; }
142 
144  double MinStep() const { return minStep; }
146  double& MinStep() { return minStep; }
147 
149  double MaxStep() const { return maxStep; }
151  double& MaxStep() { return maxStep; }
152 
153  private:
155  FunctionType& function;
156 
158  arma::mat newIterateTmp;
160  arma::cube s;
162  arma::cube y;
163 
165  size_t numBasis;
171  double wolfe;
175  double factr;
179  double minStep;
181  double maxStep;
182 
184  std::pair<arma::mat, double> minPointIterate;
185 
192  double Evaluate(const arma::mat& iterate);
193 
201  double ChooseScalingFactor(const size_t iterationNum,
202  const arma::mat& gradient);
203 
210  bool GradientNormTooSmall(const arma::mat& gradient);
211 
225  bool LineSearch(double& functionValue,
226  arma::mat& iterate,
227  arma::mat& gradient,
228  const arma::mat& searchDirection);
229 
238  void SearchDirection(const arma::mat& gradient,
239  const size_t iterationNum,
240  const double scalingFactor,
241  arma::mat& searchDirection);
242 
254  void UpdateBasisSet(const size_t iterationNum,
255  const arma::mat& iterate,
256  const arma::mat& oldIterate,
257  const arma::mat& gradient,
258  const arma::mat& oldGradient);
259 };
260 
261 } // namespace optimization
262 } // namespace mlpack
263 
264 #include "lbfgs_impl.hpp"
265 
266 #endif // MLPACK_CORE_OPTIMIZERS_LBFGS_LBFGS_HPP
267 
double factr
Minimum relative function value decrease to continue the optimization.
Definition: lbfgs.hpp:175
double ArmijoConstant() const
Get the Armijo condition constant.
Definition: lbfgs.hpp:119
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: binarize.hpp:18
double Factr() const
Get the factr value.
Definition: lbfgs.hpp:134
size_t & MaxIterations()
Modify the maximum number of iterations.
Definition: lbfgs.hpp:116
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Wolfe() const
Get the Wolfe parameter.
Definition: lbfgs.hpp:124
size_t maxLineSearchTrials
Maximum number of trials for the line search.
Definition: lbfgs.hpp:177
arma::cube y
Stores all the y matrices in memory.
Definition: lbfgs.hpp:162
size_t & NumBasis()
Modify the memory size.
Definition: lbfgs.hpp:111
size_t MaxIterations() const
Get the maximum number of iterations.
Definition: lbfgs.hpp:114
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:121
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:173
double maxStep
Maximum step of the line search.
Definition: lbfgs.hpp:181
double minStep
Minimum step of the line search.
Definition: lbfgs.hpp:179
double & MinGradientNorm()
Modify the minimum gradient norm.
Definition: lbfgs.hpp:131
double armijoConstant
Parameter for determining the Armijo condition.
Definition: lbfgs.hpp:169
double & MaxStep()
Modify the maximum line search step size.
Definition: lbfgs.hpp:151
size_t & MaxLineSearchTrials()
Modify the maximum number of line search trials.
Definition: lbfgs.hpp:141
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:106
double & Wolfe()
Modify the Wolfe parameter.
Definition: lbfgs.hpp:126
const FunctionType & Function() const
Return the function that is being optimized.
Definition: lbfgs.hpp:104
arma::mat newIterateTmp
Position of the new iterate.
Definition: lbfgs.hpp:158
arma::cube s
Stores all the s matrices in memory.
Definition: lbfgs.hpp:160
double & MinStep()
Modify the minimum line search step size.
Definition: lbfgs.hpp:146
size_t maxIterations
Maximum number of iterations.
Definition: lbfgs.hpp:167
size_t NumBasis() const
Get the memory size.
Definition: lbfgs.hpp:109
size_t MaxLineSearchTrials() const
Get the maximum number of line search trials.
Definition: lbfgs.hpp:139
const std::pair< arma::mat, double > & MinPointIterate() const
Return the point where the lowest function value has been found.
L_BFGS(FunctionType &function, const size_t numBasis=10, const size_t maxIterations=10000, const double armijoConstant=1e-4, const double wolfe=0.9, const double minGradientNorm=1e-6, const double factr=1e-15, const size_t maxLineSearchTrials=50, const double minStep=1e-20, const double maxStep=1e20)
Initialize the L-BFGS object.
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:184
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:34
double wolfe
Parameter for detecting the Wolfe condition.
Definition: lbfgs.hpp:171
double MinStep() const
Return the minimum line search step size.
Definition: lbfgs.hpp:144
double & Factr()
Modify the factr value.
Definition: lbfgs.hpp:136
double MaxStep() const
Return the maximum line search step size.
Definition: lbfgs.hpp:149
double MinGradientNorm() const
Get the minimum gradient norm.
Definition: lbfgs.hpp:129
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:165