L-BFGS, the Limited-memory Broyden-Fletcher-Goldfarb-Shanno algorithm, is a quasi-Newton method for unconstrained optimization that builds an implicit approximation of the inverse Hessian from only the last few gradient differences. In artificial intelligence and machine learning, it is a standard solver for smooth, deterministic objective functions such as logistic regression, conditional random fields and full-batch model fitting.
The method keeps the fast convergence of BFGS while reducing memory from quadratic to linear in the number of parameters. That trade makes it practical for problems with millions of variables, where storing a dense Hessian or its inverse would be impossible.
How L-BFGS approximates curvature
L-BFGS approximates second-order information by storing m pairs of vectors instead of a full matrix. Each pair contains the step s = x(k+1) – x(k) and the gradient change y = g(k+1) – g(k), which together describe how the gradient responded to the most recent move.
At every iteration, the algorithm computes a search direction with the two-loop recursion. The first loop runs backward through the stored pairs, the second runs forward, and between them an initial diagonal scaling, usually gamma = s’y / y’y, sets the step scale. The result equals the product of an implicit inverse Hessian approximation with the gradient, obtained in roughly 4mn floating-point operations for n parameters.
After the direction is found, a line search chooses the step length. Most implementations require the strong Wolfe conditions, which guarantee sufficient decrease and enforce positive curvature (s’y > 0). That curvature condition keeps the approximation positive definite, so every computed direction remains a descent direction.
The main stages of one iteration are summarized below, together with their purpose and approximate cost.
| Stage | Purpose | Approximate cost |
|---|---|---|
| Gradient evaluation | Measure the local slope | One forward and backward pass |
| Two-loop recursion | Turn gradient into a curvature-aware direction | About 4mn operations |
| Line search | Pick a step satisfying Wolfe conditions | One to a few function evaluations |
| Memory update | Add the newest pair, discard the oldest | About 2n stored values |
Key parameters and their effects
The behavior of L-BFGS depends on a small set of parameters, and the memory size m matters most. Values between 3 and 20 are typical, and m = 10 is a common default; larger values capture more curvature but rarely help beyond about 20 on ordinary problems.
The following table lists the parameters that practitioners adjust most often and the effect of each one.
| Parameter | Typical value | Effect |
|---|---|---|
| Memory size m | 5 to 20 | More curvature information, more memory |
| Gradient tolerance | 1e-5 to 1e-8 | Controls when to stop |
| Wolfe constant c1 | 1e-4 | Strictness of sufficient decrease |
| Wolfe constant c2 | 0.9 | Strictness of the curvature condition |
| Maximum iterations | 100 to 10,000 | Caps total work on hard problems |
Two widely used variants extend the basic method. L-BFGS-B adds simple bound constraints on each variable through a projected gradient and a generalized Cauchy point, and OWL-QN handles L1 regularization by working within orthants where the absolute-value term is differentiable.
How L-BFGS compares with related optimizers
L-BFGS sits between first-order methods and the full Newton’s method. It uses only gradients, like gradient descent, yet it adapts the step to local curvature, which gives superlinear-like progress on many smooth problems without forming the Hessian matrix.
The comparison below places L-BFGS next to the neighboring methods of the optimization toolbox.
| Method | Information used | Memory | Best suited to |
|---|---|---|---|
| Gradient descent | Gradient only | O(n) | Simple or very noisy problems |
| Newton’s method | Exact Hessian | O(n squared) | Small, highly accurate problems |
| Full BFGS | Dense inverse Hessian estimate | O(n squared) | Medium problems up to thousands of variables |
| L-BFGS | Last m gradient pairs | O(mn) | Large smooth deterministic problems |
| Conjugate gradient | Previous direction | O(n) | Large problems with very tight memory |
| Adam | Running gradient moments | O(n) | Stochastic minibatch deep learning |
Against the conjugate gradient method, L-BFGS usually needs fewer function evaluations for a modest increase in memory. Against Adam and other adaptive methods, it converges faster on full-batch objectives but is poorly suited to noisy minibatch gradients, which corrupt the stored curvature pairs.
Where L-BFGS is used in machine learning
L-BFGS is the default choice for medium and large smooth problems whose full gradient can be computed exactly. Common examples include multinomial logistic regression, maximum entropy models, Gaussian process hyperparameter fitting and the training of conditional random fields.
Scientific computing and deep learning also use it in narrower roles. Physics-informed neural networks often switch to L-BFGS after an Adam warm-up phase, neural style transfer optimizes image pixels with it, and many statistical libraries rely on it for maximum likelihood estimation.
The decision table below shows when the method is a strong candidate and when another optimizer is more appropriate.
| Situation | Recommended choice | Reason |
|---|---|---|
| Smooth convex loss, full batch | L-BFGS | Fast, reliable convergence |
| Box constraints on variables | L-BFGS-B | Handles bounds natively |
| L1-regularized sparse model | OWL-QN | Copes with nondifferentiable terms |
| Minibatch deep network training | Adam or SGD | Noise breaks curvature estimates |
| Very few variables, high accuracy | Newton’s method | Exact curvature is affordable |
Limitations and common mistakes
The method assumes a reasonably smooth objective with reliable gradients. When those assumptions fail, the curvature model becomes misleading, and the optimizer may stall, take tiny steps or terminate early with a line search failure.
- Stochastic gradients produce inconsistent y vectors; stochastic L-BFGS variants exist but require careful averaging or large batches.
- Skipping the curvature check allows s’y to become nonpositive and destroys positive definiteness.
- Poorly scaled features slow convergence; standardizing inputs usually helps more than enlarging m.
- Nonsmooth terms such as hinge losses or L1 penalties need a specialized variant rather than plain L-BFGS.
- Non-convex objectives may lead the method to a local optimum or a saddle region, as with any local optimizer.
A frequent diagnostic error is to treat an abnormal line search termination as convergence. Checking the final gradient norm, rather than trusting the exit message alone, separates a genuine optimum from a numerical failure.
In everyday language
Imagine walking down a foggy hillside to reach the lowest point. You can feel the slope under your feet. You cannot see the whole valley. L-BFGS remembers your last few steps and how the slope changed after each one.
From that short memory, it guesses the shape of the ground ahead. It then takes longer steps on gentle slopes and shorter steps where the ground curves sharply. Because it keeps only a handful of notes, it stays light even on enormous problems. The key idea is simple: a little memory of the past makes each new step smarter.
Seen in the real world
A data science team at an online retailer trains a multinomial logistic regression model to route customer support tickets into 40 categories. The model has about 2 million sparse features from word and phrase counts, and the full training set of 600,000 tickets fits in memory.
Plain gradient descent needed several thousand iterations and careful learning rate tuning. The team switched to L-BFGS with memory size 10 and a gradient tolerance of 1e-6. The solver reached the same validation accuracy in about 180 iterations, and training time fell from roughly two hours to under 15 minutes on the same server.
Later the team added L1 regularization to shrink the feature set and moved to OWL-QN. The final model kept fewer than 15 percent of the features with no measurable loss in accuracy.