L-BFGS with Two-Loop Recursion

Quick Overview

Implement L-BFGS in NumPy: the two-loop recursion for the search direction, a curvature-filtered history of the last m pairs, Armijo backtracking, and the driver loop. It tests quasi-Newton mechanics, descent-direction safeguards, and the per-iteration cost compared with dense BFGS.

L-BFGS with Two-Loop Recursion

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Implement four NumPy functions that together minimize a differentiable function `f` with L-BFGS, given its gradient function `grad`. The curvature histories `s_list` and `y_list` are Python lists stored oldest-first, so the newest pair is the last element. Vector arguments may be plain lists or NumPy arrays. ```python def two_loop_direction(g, s_list, y_list): ... def update_history(s_list, y_list, s, y, m, eps=1e-10): ... def backtracking_line_search(f, x, fx, g, d, alpha0=1.0, c1=1e-4, rho=0.5, max_iter=50): ... def lbfgs(f, grad, x0, m=5, max_iter=100, tol=1e-8, c1=1e-4, rho=0.5): ... ``` The functions must meet these requirements: - `two_loop_direction` returns $d = -Hg$ computed with the two-loop recursion, where $H$ is the implicit inverse-Hessian approximation built from the history. The initial scaling is `gamma = (s @ y) / (y @ y)` from the newest pair, or `gamma = 1` when the history is empty, which makes the result `-g`. - `update_history` appends copies of `s` and `y` to the lists in place only if `s @ y > eps`, then drops the oldest pairs until at most `m` remain. It returns `True` if the pair was stored and `False` otherwise. - `backtracking_line_search` tries `alpha0 * rho**k` for `k = 0, 1, ..., max_iter - 1` and returns the first step size that satisfies the Armijo condition `f(x + alpha*d) <= fx + c1*alpha*(g @ d)`. If no trial passes, it returns the last one tried. - `lbfgs` returns `(x, f_history)`. Before each step it stops if `norm(g) <= tol`. It uses `-g` as the direction whenever the two-loop direction has `g @ d >= 0`, runs the line search with `alpha0=1`, and stores the pair `s = alpha*d`, `y = g_new - g`. - `f_history` starts with `f(x0)` and gets `f(x)` appended after every step. `x0` must not be mutated. Example: ```python two_loop_direction([1., 1.], [[1., 0.]], [[2., 0.]]) # -> [-0.5, -0.5] f = lambda x: x @ x; grad = lambda x: 2 * x backtracking_line_search(f, [1.], 1.0, [2.], [-2.]) # -> 0.5 lbfgs(f, grad, np.array([1., 2.]), max_iter=1) # -> ([0., 0.], [5.0, 0.0]) ``` ```hint Two passes over the history The two loops run in opposite orders over the pairs. Decide which per-pair scalars the first pass has to keep so the second pass can use them. ``` ```hint Check the first example by hand With one pair `s = [1, 0]`, `y = [2, 0]` and `g = [1, 1]`, work the recursion on paper. If you do not land on `[-0.5, -0.5]`, look at where `gamma` comes from and at the order of each loop. ``` ```hint In-place history `update_history` must change the caller's lists. Check that trimming to `m` pairs changes those list objects (slicing builds a new list), and that a stored array cannot be changed later by the caller. ``` ### What a Strong Answer Covers - Runs the two loops in the right orders, keeps the per-pair coefficients between them, and takes the initial scaling from the newest pair. - Filters pairs by the curvature condition and keeps at most `m` of them, storing copies in the caller's lists. - Implements Armijo backtracking with the required fallback, and guards the driver with the descent check and the `-g` fallback. - Gets the driver's bookkeeping exact: the tolerance check before each step, the length of `f_history`, and no mutation of `x0`. - Explains why positive curvature keeps the direction a descent direction, and states the per-iteration time and memory cost. ### Follow-up Questions - Why can the two-loop result fail to be a descent direction, and is the `-g` fallback still needed? - Why prefer a strong Wolfe line search over Armijo backtracking here? - What is the time and memory cost per iteration, and how does it compare to dense BFGS?

Overview: Implement L-BFGS in NumPy: the two-loop recursion for the search direction, a curvature-filtered history of the last m pairs, Armijo backtracking, and the driver loop. It tests quasi-Newton mechanics, descent-direction safeguards, and the per-iteration cost compared with dense BFGS.

PracHub logo
General
Nov 25, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

Implement four NumPy functions that together minimize a differentiable function f with L-BFGS, given its gradient function grad. The curvature histories s_list and y_list are Python lists stored oldest-first, so the newest pair is the last element. Vector arguments may be plain lists or NumPy arrays.

def two_loop_direction(g, s_list, y_list): ...
def update_history(s_list, y_list, s, y, m, eps=1e-10): ...
def backtracking_line_search(f, x, fx, g, d, alpha0=1.0, c1=1e-4, rho=0.5, max_iter=50): ...
def lbfgs(f, grad, x0, m=5, max_iter=100, tol=1e-8, c1=1e-4, rho=0.5): ...

The functions must meet these requirements:

  • two_loop_direction returns d=−Hgd = -Hg computed with the two-loop recursion, where HH is the implicit inverse-Hessian approximation built from the history. The initial scaling is gamma = (s @ y) / (y @ y) from the newest pair, or gamma = 1 when the history is empty, which makes the result -g .
  • update_history appends copies of s and y to the lists in place only if s @ y > eps , then drops the oldest pairs until at most m remain. It returns True if the pair was stored and False otherwise.
  • backtracking_line_search tries alpha0 * rho**k for k = 0, 1, ..., max_iter - 1 and returns the first step size that satisfies the Armijo condition f(x + alpha*d) <= fx + c1*alpha*(g @ d) . If no trial passes, it returns the last one tried.
  • lbfgs returns (x, f_history) . Before each step it stops if norm(g) <= tol . It uses -g as the direction whenever the two-loop direction has g @ d >= 0 , runs the line search with alpha0=1 , and stores the pair s = alpha*d , y = g_new - g .
  • f_history starts with f(x0) and gets f(x) appended after every step. x0 must not be mutated.

Example:

two_loop_direction([1., 1.], [[1., 0.]], [[2., 0.]])  # -> [-0.5, -0.5]

f = lambda x: x @ x; grad = lambda x: 2 * x
backtracking_line_search(f, [1.], 1.0, [2.], [-2.])   # -> 0.5
lbfgs(f, grad, np.array([1., 2.]), max_iter=1)        # -> ([0., 0.], [5.0, 0.0])

What a Strong Answer Covers Guidance

  • Runs the two loops in the right orders, keeps the per-pair coefficients between them, and takes the initial scaling from the newest pair.
  • Filters pairs by the curvature condition and keeps at most m of them, storing copies in the caller's lists.
  • Implements Armijo backtracking with the required fallback, and guards the driver with the descent check and the -g fallback.
  • Gets the driver's bookkeeping exact: the tolerance check before each step, the length of f_history , and no mutation of x0 .
  • Explains why positive curvature keeps the direction a descent direction, and states the per-iteration time and memory cost.

Follow-up Questions Guidance

  • Why can the two-loop result fail to be a descent direction, and is the -g fallback still needed?
  • Why prefer a strong Wolfe line search over Armijo backtracking here?
  • What is the time and memory cost per iteration, and how does it compare to dense BFGS?