Backtracking Line Search with Armijo Condition
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `backtracking_line_search`, which returns a step size satisfying the Armijo condition `f(x + α·d) <= f(x) + c·α·(g·d)`. Start at `alpha0` and multiply by `rho` until accepted. Then implement `gradient_descent_armijo`, which uses it to run gradient descent and returns `(x, history)`.
```python
def backtracking_line_search(f, grad_x, x, direction=None, alpha0=1.0, rho=0.5, c=1e-4, max_iter=50):
...
def gradient_descent_armijo(f, grad, x0, alpha0=1.0, rho=0.5, c=1e-4, tol=1e-8, max_steps=1000):
...
```
Here `f` maps a NumPy vector to a scalar loss, `grad_x` is the gradient of `f` at `x`, and `grad` maps a vector to its gradient.
For example, with a quadratic loss on a poorly conditioned matrix:
```python
import numpy as np
A = np.diag([1.0, 10.0])
f = lambda x: 0.5 * x @ A @ x
grad = lambda x: A @ x
x = np.array([1.0, 1.0])
backtracking_line_search(f, A @ x, x) # 0.125
gradient_descent_armijo(f, grad, x, max_steps=1)
# x = [0.875, -0.25], history = [5.5, 0.6953125]
```
```hint Compute the slope once
The directional derivative `g·d` does not change between trial steps. Compute it before the loop: its sign tells you whether `d` is a descent direction, and it appears in the right-hand side of every Armijo test.
```
```hint Watch the loop boundaries
Count how many candidates you test and when you shrink. The fallback value after `max_iter` rejections and the number of entries in `history` both depend on getting the order of "test, then shrink" and "check tolerance, then step" right.
```
### Constraints & Assumptions
- `direction=None` means `d = -grad_x`. If `g·d > 0`, raise `ValueError` (a zero slope is allowed, so a zero gradient returns `alpha0`).
- Test at most `max_iter` candidates. If none is accepted, return `alpha0 * rho**max_iter`.
- The descent loop stops when `‖grad(x)‖ <= tol` or after `max_steps` steps. It does not mutate `x0`.
- `history` is a float `np.ndarray` holding `f(x0)` followed by the loss after each step.
### What a Strong Answer Covers
- The Armijo test written with the correct sign on the slope term, and the descent-direction check that rejects a positive slope but allows a zero one.
- The exact fallback when no candidate passes within `max_iter` trials.
- A descent loop that copies `x0`, checks the gradient norm before taking a step, and returns the loss history as a float array.
- Why an accepted step guarantees the loss goes down, and what each step costs in function and gradient evaluations.
### Follow-up Questions
- How would you add the curvature (Wolfe) condition?
- Why does gradient descent on `diag(1, 10)` zigzag, and how does line search help?
- How could you reduce the number of function evaluations per step?
Overview: Implement a backtracking line search that shrinks a trial step until the Armijo sufficient-decrease condition holds, then use it to drive a gradient descent loop that records the loss history. Tests descent-direction checks, exact loop boundaries, and why an accepted step keeps the loss going down.