Skip to content

16.1 Convexity, smoothness, gradient descent, and Armijo line search

The first decision you make when you train a model is the learning rate, and the first ways it fails are dramatic. In Training loop and the autograd bigram from AI Engineering, the bigram stops being a table of counts and becomes 65536 weights trained by gradient descent. Pick the learning rate too large and the loss jumps to inf, then NaN, within a few steps. Pick it too small and the loss crawls for thousands of steps toward the negative log-likelihood that the count model of Byte bigram: counts, logits, safetensors v0 reached instantly.

Concepts from other sections (6)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Both symptoms have exact explanations on a quadratic, which is the local model of every smooth loss. The step must stay under 2/L2/L, where LL measures the steepest curvature, and the number of steps scales with the condition number κ\kappa. This section derives both facts, then builds plain gradient descent and a backtracking line search that chooses a step without knowing LL at all. The update you build here is exactly what the next section, The Optimizer protocol: SGD, momentum, Nesterov, weight decay, generalizes.

You will need gradients, from Partial derivatives, gradients, and gradcheck, and the eigenvalues of a symmetric matrix, from Eigenvalues, power iteration, and the spectral radius. The reference solution is course/solutions/python/tinyllm/optim/gd.py, and its interface is fixed by the contract course/contracts/py/tinyllm/optim/gd.pyi.

Learning objectives

  • Derive the step-size limit 0<η<2/L0 < \eta < 2/L for gradient descent on a quadratic from its eigen-decomposition.
  • Prove that gradient descent with step 1/L1/L shrinks the optimality gap by at least 1−1/κ1 - 1/\kappa per step on an LL-smooth, μ\mu-strongly convex function.
  • State the Armijo sufficient-decrease condition and explain why backtracking terminates on a smooth function.
  • Implement gradient_descent and armijo_step so that divergence, NaN trial points, and non-descent directions are handled correctly.

The section uses a small set of symbols throughout; this table collects them with the type each takes in code.

SymbolMeaningType / shape
f:Rn→Rf: \mathbb{R}^n \to \mathbb{R}the function to minimize (a loss)callable
∇f(x)\nabla f(x)its gradient at xxfloat64[n]
x⋆x^\star, f⋆f^\stara minimizer and the minimum valuefloat64[n], float
η\eta (lr)the learning rate (step size)float >0> 0
xtx_tthe iterate after tt stepsfloat64[n]
LLsmoothness constant: ∥∇f(x)−∇f(y)∥≤L∥x−y∥\lVert\nabla f(x) - \nabla f(y)\rVert \le L \lVert x - y\rVertfloat
μ\mustrong convexity constantfloat >0> 0
κ=L/μ\kappa = L/\muthe condition numberfloat ≥1\ge 1
HHa symmetric positive definite matrix; for a quadratic, the Hessianfloat64[n, n]
λi\lambda_ieigenvalues of HH, with μ=λmin⁡\mu = \lambda_{\min} and L=λmax⁡L = \lambda_{\max}floats
dda search directionfloat64[n]
α\alpha, α0\alpha_0, ρ\rho, cctrial step, first trial, shrink factor, sufficient-decrease constantfloats

Convexity, smoothness, and the condition number

Section titled “Convexity, smoothness, and the condition number”

A function ff is convex if the chord between any two points of its graph lies above the graph: f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y)f(\theta x + (1-\theta) y) \le \theta f(x) + (1-\theta) f(y) for θ∈[0,1]\theta \in [0, 1]. For differentiable ff this is equivalent to every tangent plane lying below the graph:

f(y)≥f(x)+∇f(x)⊤(y−x).f(y) \ge f(x) + \nabla f(x)^\top (y - x).

Then ∇f(x⋆)=0\nabla f(x^\star) = 0 implies f(y)≥f(x⋆)f(y) \ge f(x^\star) for all yy: every stationary point is a global minimum. Neural network losses are not convex, but near a minimum they look like convex quadratics, and that is where the rates below describe what you actually see.

Convexity says the function lies above its tangents. To say anything about rates you also need to sandwich it between two parabolas. Smoothness supplies the upper one. If the gradient is LL-Lipschitz, the function never curves up faster than a parabola of curvature LL; this is the descent lemma:

f(y)≤f(x)+∇f(x)⊤(y−x)+L2∥y−x∥2.f(y) \le f(x) + \nabla f(x)^\top (y - x) + \tfrac L2 \lVert y - x\rVert^2.

Strong convexity supplies the lower one. ff is μ\mu-strongly convex if f(y)≥f(x)+∇f(x)⊤(y−x)+μ2∥y−x∥2f(y) \ge f(x) + \nabla f(x)^\top (y - x) + \tfrac\mu2 \lVert y - x\rVert^2: it curves up at least as fast as a parabola of curvature μ\mu. For a quadratic f(x)=12x⊤Hxf(x) = \tfrac12 x^\top H x, the gradient is HxHx, and the best constants are the extreme eigenvalues of HH: L=λmax⁡L = \lambda_{\max} and μ=λmin⁡\mu = \lambda_{\min}.

The ratio of the two curvatures is the condition number, κ=L/μ\kappa = L/\mu. It measures how stretched the level sets are: κ=1\kappa = 1 is a round bowl, κ=100\kappa = 100 is a long narrow valley. In Error analysis, condition numbers, and tolerance budgets you will meet the condition number of a matrix, ∥A∥∥A−1∥\lVert A\rVert \lVert A^{-1}\rVert; for a symmetric positive definite HH it is the same λmax⁡/λmin⁡\lambda_{\max}/\lambda_{\min}.

With the two bounds in hand, the rate of gradient descent follows in three lines. The update is xt+1=xt−η∇f(xt)x_{t+1} = x_t - \eta \nabla f(x_t). Put y=xt+1y = x_{t+1} in the descent lemma with η=1/L\eta = 1/L:

f(xt+1)≤f(xt)−12L∥∇f(xt)∥2.f(x_{t+1}) \le f(x_t) - \tfrac{1}{2L} \lVert\nabla f(x_t)\rVert^2.

Strong convexity implies ∥∇f(x)∥2≥2μ (f(x)−f⋆)\lVert\nabla f(x)\rVert^2 \ge 2\mu\,(f(x) - f^\star); you get this by minimizing both sides of its defining inequality over yy. Combining the two,

f(xt+1)−f⋆≤(1−μL)(f(xt)−f⋆)⇒f(xt)−f⋆≤(1−1κ)t(f(x0)−f⋆).f(x_{t+1}) - f^\star \le \left(1 - \frac{\mu}{L}\right)\left(f(x_t) - f^\star\right) \quad\Rightarrow\quad f(x_t) - f^\star \le \left(1 - \frac1\kappa\right)^t \left(f(x_0) - f^\star\right).

To shrink the gap by a factor ε\varepsilon takes about κln⁡(1/ε)\kappa \ln(1/\varepsilon) steps: ten times the condition number, ten times the steps.

On a quadratic you can do better than a bound and see the dynamics exactly. For f=12x⊤Hxf = \tfrac12 x^\top H x, write xx in the eigenvectors of HH. Gradient descent then acts independently on each coordinate:

xt+1,i=(1−ηλi) xt,i.x_{t+1, i} = (1 - \eta \lambda_i)\, x_{t, i}.

The coordinate shrinks when ∣1−ηλi∣<1\lvert 1 - \eta\lambda_i\rvert < 1, that is, 0<η<2/λi0 < \eta < 2/\lambda_i. For all coordinates at once the condition is 0<η<2/L0 < \eta < 2/L. Above 2/L2/L the stiffest direction grows by ∣1−ηL∣>1\lvert 1 - \eta L\rvert > 1 per step, and that is the loss that jumps to inf. At η=1/L\eta = 1/L the stiffest direction is solved in one step and the flattest shrinks by 1−1/κ1 - 1/\kappa, so the bound above is attained.

The analysis assumes you know LL, and real losses do not announce it. A line search picks the step adaptively. Given a direction dd with ∇f(x)⊤d<0\nabla f(x)^\top d < 0 (a descent direction, for example d=−∇f(x)d = -\nabla f(x)), try α=α0\alpha = \alpha_0 and accept it if the Armijo condition holds:

f(x+αd)≤f(x)+c α ∇f(x)⊤d,0<c<1.f(x + \alpha d) \le f(x) + c\,\alpha\, \nabla f(x)^\top d, \qquad 0 < c < 1.

The right side is a line through f(x)f(x) with a fraction cc of the initial slope, and the step must land below it. If it does not, set α←ρα\alpha \leftarrow \rho\alpha and try again. For an LL-smooth ff and d=−∇fd = -\nabla f, the descent lemma shows every α≤2(1−c)/L\alpha \le 2(1-c)/L is accepted, so backtracking stops after a bounded number of reductions with a step of at least 2ρ(1−c)/L2\rho(1-c)/L. If dd is not a descent direction, no small step helps, and the search must refuse instead of halving forever.

One numerical detail turns out to matter as much as the math. A trial point outside the function’s domain (a log of a negative number, an overflow) gives nan, and every comparison with NaN is false. Written as if f(x + a d) <= bound: return a, a NaN fails the test and the step shrinks. Written as while f(x + a d) > bound: a *= rho, the NaN makes the loop condition false and the bad step is accepted. The two forms are the same math with opposite behavior, so NaN must be treated as a rejection by construction.

Run gradient descent on f(x)=12(x12+10x22)f(x) = \tfrac12(x_1^2 + 10 x_2^2). Here H=diag(1,10)H = \mathrm{diag}(1, 10), so μ=1\mu = 1, L=10L = 10, κ=10\kappa = 10, and ∇f=(x1,10x2)\nabla f = (x_1, 10 x_2). Start at x0=(10,1)x_0 = (10, 1) with η=1/L=0.1\eta = 1/L = 0.1; the first two steps are:

ttxtx_t∇f(xt)\nabla f(x_t)f(xt)f(x_t)f(xt)/f(xt−1)f(x_t)/f(x_{t-1})
0(10,1)(10, 1)(10,10)(10, 10)55
1(9,0)(9, 0)(9,0)(9, 0)40.50.736
2(8.1,0)(8.1, 0)(8.1,0)(8.1, 0)32.8050.81

The stiff coordinate is multiplied by 1−0.1⋅10=01 - 0.1 \cdot 10 = 0 and is done in one step. The flat one is multiplied by 1−0.1⋅1=0.91 - 0.1 \cdot 1 = 0.9, so its share of ff shrinks by 0.810.81 per step, inside the bound 1−1/κ=0.91 - 1/\kappa = 0.9. With η=0.21\eta = 0.21, just over 2/L=0.22/L = 0.2, the stiff coordinate is multiplied by 1−2.1=−1.11 - 2.1 = -1.1 per step: it alternates sign and grows without bound.

Now run one Armijo search on f(x)=x2f(x) = x^2 at x=1x = 1 along d=−f′(1)=−2d = -f'(1) = -2, with α0=1\alpha_0 = 1, c=10−4c = 10^{-4}, ρ=0.5\rho = 0.5. The slope is f′(1) d=−4f'(1)\,d = -4, and the trials go like this:

α\alphax+αdx + \alpha dffbound 1+c α (−4)1 + c\,\alpha\,(-4)accept?
1−1-110.9996no
0.5000.9998yes

The full step overshoots to the mirror point with the same value; half of it lands on the minimum. The search returns 0.5. These two hand computations are the first things a correct implementation must reproduce.

The two functions have small signatures:

python/tinyllm/optim/gd.py
def gradient_descent(f, grad, x0, lr: float, steps: int) -> list[NDArray] # [x_0, ..., x_steps]
def armijo_step(f, grad, x, d, alpha0: float = 1.0, c: float = 1e-4, rho: float = 0.5) -> float

gradient_descent returns steps + 1 independent float64 arrays and never modifies x0. It evaluates f at each iterate only to detect divergence: a non-finite value raises FloatingPointError naming the step. A learning rate η≤0\eta \le 0 or a negative step count is a ValueError. armijo_step raises ValueError for a non-descent direction or for parameters outside 0<c<10 < c < 1, 0<ρ<10 < \rho < 1, α0>0\alpha_0 > 0, and RuntimeError after 60 reductions without success.

Those rules are what the tests hold the code to. Beyond the hand-worked trajectory and search, the gap must satisfy ≤(1−1/κ)t\le (1 - 1/\kappa)^t at every step on quadratics with κ\kappa = 2, 10, and 100, and a float trajectory must match an exact fractions.Fraction recomputation to rounding. On the same quadratic with L=10L = 10, η=3\eta = 3 must raise FloatingPointError while η=0.19\eta = 0.19 converges. For the line search, the returned step must satisfy the Armijo condition while twice it does not, checked on 50 Rosenbrock points, so the search returns the largest acceptable step on its grid. A full step that already works is returned unchanged. On f=x2f = x^2 at 1 along −4-4 the search needs two halvings and returns α=0.25\alpha = 0.25, which checks that each trial shrinks the previous one. A function whose trial values are NaN must have those trials rejected, ending at 0.125. Ascent and orthogonal directions and bad cc, ρ\rho, α0\alpha_0 must raise. A lying gradient must end in RuntimeError, with the 60th halving, 2−602^{-60}, still tried. Finally, 300 steepest-descent steps with Armijo on Rosenbrock must decrease ff monotonically, which is the whole point: a usable step size without knowing LL.

The reasoning chain is short, and each step answers one of the requirements above.

  1. The update is one line. xt+1=xt−η∇f(xt)x_{t+1} = x_t - \eta\nabla f(x_t). Everything else in gradient_descent exists to make the trajectory trustworthy as data.
  2. A trajectory is a list of independent snapshots. Callers plot and compare iterates, so each must be a separate array, and the caller’s x0 must not change. Writing x = x - lr * g (a new array each step) rather than x -= lr * g gives that for free.
  3. Divergence must be loud. Above 2/L2/L the stiff coordinate grows by ∣1−ηL∣>1\lvert 1 - \eta L\rvert > 1 per step. A list of inf and NaN returned as a “trajectory” hides the bug, so every iterate, including the last, is checked and the first non-finite value raises.
  4. Armijo needs the slope once. The bound f(x)+cα ∇f(x)⊤df(x) + c\alpha\,\nabla f(x)^\top d uses f(x)f(x) and the slope at α=0\alpha = 0; compute both once, before the loop.
  5. Refuse a non-descent direction. If the slope is ≥0\ge 0, no positive step can satisfy the bound for small α\alpha, so backtracking would halve forever; raise instead.
  6. Write the test so NaN fails it. if f(new) <= bound: return treats NaN as a rejection; the while f(new) > bound form accepts it.
  7. Bound the loop. For an LL-smooth ff the search stops after a bounded number of halvings. If it does not, the gradient is lying, and after 60 reductions (a factor 2−60≈10−182^{-60} \approx 10^{-18}) it raises rather than returning a meaningless step.

Here is gradient descent with those decisions in place:

MAX_REDUCTIONS = 60
def gradient_descent(f, grad, x0, lr: float, steps: int) -> list[NDArray]:
if not lr > 0: # also rejects lr = nan
raise ValueError(f"lr must be > 0, got {lr}")
if steps < 0:
raise ValueError(f"steps must be >= 0, got {steps}")
x = np.array(x0, dtype=np.float64, copy=True) # never alias the caller's x0
out = [x] # x_0 is part of the trajectory
for t in range(steps + 1):
fx = float(f(x))
if not (math.isfinite(fx) and np.isfinite(x).all()):
raise FloatingPointError(f"gradient descent diverged at step {t}: f(x_t) = {fx}")
if t == steps:
break # x_steps is checked, but not stepped from
x = x - lr * np.asarray(grad(x), dtype=np.float64) # a new array, not in place
out.append(x)
return out

The loop runs steps + 1 times so that the divergence check sees every iterate, x0x_0 through xstepsx_{\text{steps}}, while only steps updates are made. The guard is not lr > 0 instead of lr <= 0 because a NaN learning rate fails both comparisons, and only the first form rejects it. In the hand example, lr=0.1 from (10,1)(10, 1) produces [(10, 1), (9, 0), (8.1, 0)] for steps=2. With lr=3 on the same quadratic, the stiff coordinate grows by ∣1−30∣=29\lvert 1 - 30\rvert = 29 per step and the function raises at the first iterate whose ff is no longer finite.

The line search follows the same pattern: validate, compute the fixed quantities once, then loop with a test that is safe on NaN.

def armijo_step(f, grad, x, d, alpha0=1.0, c=1e-4, rho=0.5) -> float:
if not (0 < c < 1 and 0 < rho < 1 and alpha0 > 0):
raise ValueError(f"need 0 < c < 1, 0 < rho < 1, alpha0 > 0; got c={c}, rho={rho}, alpha0={alpha0}")
x = np.asarray(x, dtype=np.float64)
d = np.asarray(d, dtype=np.float64)
fx = float(f(x))
slope = float(np.dot(np.ravel(grad(x)), np.ravel(d))) # grad f(x)^T d, any shape
if not slope < 0: # ascent, orthogonal, or nan: refuse
raise ValueError(f"d is not a descent direction: grad(x) . d = {slope}")
alpha = alpha0
for _ in range(MAX_REDUCTIONS + 1): # alpha0 and 60 reductions: 61 trials
# Written so that a nan f(x + alpha d) fails the test and shrinks alpha.
if f(x + alpha * d) <= fx + c * alpha * slope:
return alpha # the first acceptable step, unshrunk
alpha *= rho # shrink the previous trial, not alpha0
raise RuntimeError(f"no step satisfies the Armijo condition after {MAX_REDUCTIONS} reductions")

Trace the hand search through it: f(1)=1f(1) = 1 and the slope is 2⋅(−2)=−42 \cdot (-2) = -4. Trial α=1\alpha = 1 gives f(−1)=1>1−4×10−4f(-1) = 1 > 1 - 4 \times 10^{-4}, so it is rejected and α=0.5\alpha = 0.5. Trial α=0.5\alpha = 0.5 gives f(0)=0≤0.9998f(0) = 0 \le 0.9998, and the function returns 0.5. The loop count is MAX_REDUCTIONS + 1 so that the 60th reduction, α0⋅2−60\alpha_0 \cdot 2^{-60}, is actually tried before giving up. np.ravel lets xx be a matrix (a weight array) as well as a vector.

The full solution is in gd.py.

Consider cost first. gradient_descent makes steps gradient calls and steps + 1 function calls, and stores steps + 1 arrays: O(steps⋅n)O(\text{steps} \cdot n) memory, which is the point, since it returns the trajectory. armijo_step costs one gradient, one f(x)f(x), and one ff per trial; on an LL-smooth function the number of trials is at most 1+log⁡1/ρ(α0L/(2(1−c)))1 + \log_{1/\rho}(\alpha_0 L / (2(1 - c))).

Copies beat in-place updates here. In-place x -= lr * g saves an allocation per step, but appending the same array object every step makes every entry the final iterate. For a reference whose output is a trajectory, a fresh array per step is the correct choice. The optimizers of the next section update in place because they return nothing.

Raising beats clamping or skipping. Silently stopping at the last finite iterate would hide a learning-rate bug; raising names the step where it happened.

Backtracking Armijo beats exact line search for this purpose. Minimizing f(x+αd)f(x + \alpha d) exactly costs many evaluations and buys little for gradient descent. Armijo needs only ff values, guarantees sufficient decrease, and is the building block of Wolfe searches (see Further reading). Halving instead of interpolating is simpler and loses at most a factor ρ\rho in step length.

Most bugs in these two functions are one character away from the correct code, and each produces a recognizable symptom:

PitfallSymptomHow the solution avoids it
a learning rate above 2/L2/L with no divergence checka list of inf and NaN returned as a trajectorychecks every iterate and raises FloatingPointError
while f(new) > bounda NaN trial point is acceptedif f(new) <= bound: return, so NaN is a rejection
searching along a direction that does not descendhalving forever, or accepting an ascentif not slope < 0: raise ValueError
updating x in place and appending itevery entry of the trajectory is the final iterate; x0 changescopies x0 once and builds a new array each step
the sign of the stepgradient ascentx - lr * grad(x)
the sign of the Armijo boundsteps that increase ff are acceptedthe bound is fx + c * alpha * slope with slope < 0
shrinking once more after successsteps half as long as they could bereturns alpha before alpha *= rho
returning a vanishing step instead of giving upsilent stalls at α=10−18\alpha = 10^{-18}raises RuntimeError after MAX_REDUCTIONS
leaving x0x_0 out of the trajectoryevery index is off by oneout = [x] before the loop
restarting each trial from α0\alpha_0, alpha = alpha0 * rhoonly α0\alpha_0 and ρα0\rho\alpha_0 are ever tried; a search that needs two halvings gives upalpha *= rho shrinks the previous trial

Warning: The NaN pitfall is the one that survives code review, because the while form looks like a faithful transcription of the math. Test it with a function that returns NaN outside its domain.

The next section, The Optimizer protocol: SGD, momentum, Nesterov, weight decay, wraps this update behind a common interface: SGD with momentum 0 performs exactly this step, in place. In AI Engineering, Training loop and the autograd bigram chooses the bigram’s learning rate and reads its loss curve with 2/L2/L and κ\kappa. Measuring the top Hessian eigenvalue during training against the 2/η2/\eta edge of stability (Cohen et al., below) applies the same eigen-direction analysis to real networks, and Error analysis, condition numbers, and tolerance budgets generalizes κ\kappa to the condition number of a matrix and of a problem.

  • Boyd and Vandenberghe, Convex Optimization, sections 3.1 and 9.2 to 9.3; Nocedal and Wright, Numerical Optimization (2nd ed.), chapter 3.
  • scipy.optimize.line_search (scipy/optimize/_linesearch.py) is the production version of armijo_step: it enforces the strong Wolfe conditions (a curvature test as well as Armijo) and interpolates instead of halving.
  • PyTorch’s torch.optim.LBFGS(line_search_fn="strong_wolfe") (torch/optim/lbfgs.py) puts a line search in a training loop with quasi-Newton directions that undo much of κ\kappa.
  • Conjugate gradient needs about κ\sqrt\kappa instead of κ\kappa steps on quadratics, by choosing HH-orthogonal directions; see Nocedal and Wright, chapter 5.
  • Convex function: a function whose chords lie above its graph; every stationary point is a global minimum.
  • LL-smooth: having an LL-Lipschitz gradient, so the function lies below a parabola of curvature LL (the descent lemma).
  • μ\mu-strongly convex: lying above a parabola of curvature μ\mu at every point.
  • Condition number κ\kappa: the ratio L/μL/\mu; for a quadratic, λmax⁡/λmin⁡\lambda_{\max}/\lambda_{\min} of the Hessian.
  • Descent direction: a direction dd with ∇f(x)⊤d<0\nabla f(x)^\top d < 0.
  • Armijo condition: the sufficient-decrease test f(x+αd)≤f(x)+c α ∇f(x)⊤df(x + \alpha d) \le f(x) + c\,\alpha\,\nabla f(x)^\top d.
  • Backtracking line search: trying α0,ρα0,ρ2α0,…\alpha_0, \rho\alpha_0, \rho^2\alpha_0, \dots until the Armijo condition holds.
  • On an LL-smooth, μ\mu-strongly convex function, gradient descent with step 1/L1/L shrinks the optimality gap by at least 1−1/κ1 - 1/\kappa per step, so it needs about κln⁡(1/ε)\kappa \ln(1/\varepsilon) steps.
  • On a quadratic, each eigen-direction is multiplied by 1−ηλi1 - \eta\lambda_i per step, so the iteration converges exactly when 0<η<2/L0 < \eta < 2/L and blows up beyond it.
  • Backtracking with the Armijo condition finds a step that decreases ff enough without knowing LL, and returns the first acceptable step of α0,ρα0,ρ2α0,…\alpha_0, \rho\alpha_0, \rho^2\alpha_0, \dots.
  • Numerical edge cases are part of the algorithm: a NaN trial value must be rejected, a direction that does not descend must be refused, and divergence must raise.
  1. Why does the gradient-descent loop run steps + 1 times when it makes only steps updates?

    Answer

    So that every iterate, including the last one, passes the divergence check. The final pass checks xstepsx_{\text{steps}} and then breaks before computing another update. Running only steps times would return a final iterate that might already be inf.

  2. Why x = x - lr * g and not x -= lr * g?

    Answer

    The trajectory list holds references. With an in-place update, every appended entry is the same array, so the whole list shows the final iterate; and on the first step it would also modify the caller’s x0 if x were not a copy. Creating a new array per step makes each entry an independent snapshot.

  3. What goes wrong if the acceptance test is written while f(x + alpha * d) > bound: alpha *= rho?

    Answer

    When the trial point is outside the function’s domain, ff returns NaN, and nan > bound is false, so the loop exits and accepts the bad step. In the solution’s form, nan <= bound is also false, so the step is rejected and shrunk. The two forms are equal in exact arithmetic and opposite on NaN.

  4. Why does armijo_step raise for a direction with ∇f(x)⊤d≥0\nabla f(x)^\top d \ge 0 instead of searching anyway?

    Answer

    If the slope is ≥0\ge 0, ff does not decrease to first order along dd. With a positive slope the bound f(x)+cα ∇f(x)⊤df(x) + c\alpha\,\nabla f(x)^\top d lies above f(x)f(x) and small steps increase ff; with a zero slope the bound asks for no decrease at all. Either way backtracking cannot deliver sufficient decrease: it halves until it gives up, or accepts a step that makes no progress. Refusing up front reports the caller’s bug where it happened.

  5. With η=1/L\eta = 1/L on f=12(x12+10x22)f = \tfrac12(x_1^2 + 10x_2^2), why is the stiff coordinate finished after one step?

    Answer

    Gradient descent multiplies each eigen-coordinate by 1−ηλi1 - \eta\lambda_i. With η=1/L=1/10\eta = 1/L = 1/10 and λ2=10\lambda_2 = 10, that factor is exactly 0, so x2x_2 becomes 0 in one step; the flat coordinate is multiplied by 1−1/10=0.91 - 1/10 = 0.9 per step, which is what limits the rate to 1−1/κ1 - 1/\kappa.