Skip to content

16.5 Optimization problem set, part a: convexity, GD rates, momentum, Adam

Every model in this series after the count-based bigram is trained by an optimizer you build in this chapter: gradient descent with a line search, SGD with momentum, and AdamW, which trains everything from the encoder-decoder of AI Engineering to its capstone. Those optimizers must match PyTorch’s trajectories step by step, and implementations go wrong on exactly the details this set drills: a step size past 2/L2/L that oscillates, momentum that is summed one step off, a bias correction applied to the wrong moment, weight decay coupled into the adaptive step. When a training run diverges, the first questions you will ask are the ones here: what is the curvature, what is the condition number, and is the learning rate under the stability limit?

Concepts from other sections (17)

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.

The set has 17 problems (24 numeric or symbolic parts and 2 proofs), grouped into convexity and the condition number, gradient descent rates and step size, momentum, and Adam. It is pen-and-paper work and supplies the analysis behind Convexity, smoothness, gradient descent, and Armijo line search, The Optimizer protocol: SGD, momentum, Nesterov, weight decay, and Adam and AdamW: bias correction and decoupled weight decay. You will want the proof habits of the Discrete math 1 problem set for q4 and q17, and the gradients and Hessians of the Calculus 3 problem set.

Work each problem on paper first, then open the answer and compare not just the result but the derivation. Each answer names the common wrong answer and why it is wrong, because those are exactly the mistakes that show up later as bugs in an optimizer. Each problem also carries a tag for the kind of answer it wants ([number], [interval], [expr in ...], [bool], [proof]); rates and steps are exact fractions.

Learning objectives

  • Decide convexity from second derivatives and Hessians, and compute LL, μ\mu, and κ\kappa for a quadratic.
  • Derive the stable step-size interval, the optimal constant step, and the step count for a target accuracy.
  • Analyze heavy-ball momentum through its velocity sum and its characteristic polynomial.
  • Compute Adam and AdamW steps by hand, and prove that bias correction makes the first moment unbiased.

The problems use a small set of symbols, collected here:

SymbolMeaningType / shape
f:Rn→Rf: \mathbb{R}^n \to \mathbb{R}the objective (loss)
x⋆,f⋆x^\star, f^\stara minimizer and the minimum value
∇f,∇2f\nabla f, \nabla^2 fgradient and Hessiannn, n×nn \times n
LLsmoothness: ∇2f⪯LI\nabla^2 f \preceq L I (largest curvature)scalar
μ\mustrong convexity: ∇2f⪰μI\nabla^2 f \succeq \mu I (smallest curvature)scalar
κ=L/μ\kappa = L/\mucondition numberscalar ≥1\ge 1
η\eta (eta)step size, the learning ratescalar
β\beta (beta), β1,β2\beta_1, \beta_2momentum and Adam’s moment decay ratesin [0,1)[0, 1)
mt,vtm_t, v_tAdam’s first and second moment estimateslike xx
λ\lambdaAdamW weight decayscalar

A function is convex if every chord lies on or above it: f(tx+(1−t)y)≤tf(x)+(1−t)f(y)f(tx + (1-t)y) \le t f(x) + (1-t) f(y) for t∈[0,1]t \in [0, 1]. For twice-differentiable ff this is equivalent to f′′≥0f'' \ge 0 in one variable, and to a positive semidefinite Hessian (v⊤∇2f v≥0v^\top \nabla^2 f\, v \ge 0 for every vv) in several. Convexity matters because every local minimum of a convex function is global. Sums and nonnegative multiples of convex functions are convex (q4); log⁡\log is concave; LSE\mathrm{LSE} is convex, so cross-entropy in the logits is convex, while the loss of a whole network in its weights is not.

Smoothness, strong convexity, and the condition number

Section titled “Smoothness, strong convexity, and the condition number”

ff is LL-smooth when its gradient changes by at most LL per unit of distance, which for twice-differentiable ff means every Hessian eigenvalue is at most LL; it is μ\mu-strongly convex when every eigenvalue is at least μ>0\mu > 0. For a quadratic f(x)=12x⊤Axf(x) = \frac12 x^\top A x with symmetric AA, LL and μ\mu are the largest and smallest eigenvalues of AA. The condition number κ=L/μ\kappa = L/\mu measures how elongated the level sets are: κ=1\kappa = 1 is a round bowl, κ=104\kappa = 10^4 a long narrow valley. It sets how many steps a digit of accuracy costs: about κln⁡10\kappa \ln 10 at η=1/L\eta = 1/L.

In the eigenbasis of AA, gradient descent xt+1=xt−ηAxtx_{t+1} = x_t - \eta A x_t acts on each coordinate separately: coordinate ii is multiplied by 1−ηλi1 - \eta\lambda_i every step. It converges from every start exactly when ∣1−ηλi∣<1\lvert 1 - \eta \lambda_i \rvert < 1 for every ii, that is 0<η<2/L0 < \eta < 2/L. The worst factor max⁡i∣1−ηλi∣\max_i \lvert 1 - \eta\lambda_i \rvert is smallest when the extreme eigenvalues balance, 1−ημ=−(1−ηL)1 - \eta\mu = -(1 - \eta L), giving η=2L+μ\eta = \frac{2}{L + \mu} and rate κ−1κ+1\frac{\kappa - 1}{\kappa + 1}. The safe default η=1/L\eta = 1/L gives rate 1−1/κ1 - 1/\kappa. With rate ρ\rho, reducing the error by a factor ε\varepsilon takes t≥ln⁡(1/ε)/ln⁡(1/ρ)t \ge \ln(1/\varepsilon) / \ln(1/\rho) steps. Without strong convexity, an LL-smooth convex ff still satisfies f(xt)−f⋆≤L∥x0−x⋆∥22tf(x_t) - f^\star \le \frac{L\lVert x_0 - x^\star \rVert^2}{2t} at η=1/L\eta = 1/L.

Backtracking (Armijo) line search avoids knowing LL: start with α0\alpha_0 and shrink by ρ\rho until the step decreases ff by at least a fraction cc of what the linear model predicts, f(x+αd)≤f(x)+c α∇f(x)⊤df(x + \alpha d) \le f(x) + c\,\alpha \nabla f(x)^\top d.

Heavy-ball momentum keeps a velocity vt+1=βvt+∇f(xt)v_{t+1} = \beta v_t + \nabla f(x_t) and steps xt+1=xt−ηvt+1x_{t+1} = x_t - \eta v_{t+1}. Unrolled, vt+1=∑k=0tβk∇f(xt−k)v_{t+1} = \sum_{k=0}^{t} \beta^k \nabla f(x_{t-k}): an exponentially weighted sum of past gradients, which for a constant gradient approaches g/(1−β)g/(1-\beta), so steps are 1/(1−β)1/(1 - \beta) times larger. On a one-dimensional quadratic with curvature hh the iterates satisfy the linear recurrence xt+1=(1+β−ηh)xt−βxt−1x_{t+1} = (1 + \beta - \eta h)x_t - \beta x_{t-1}; substituting xt=rtx_t = r^t gives a quadratic in rr, and the iterates shrink like the larger root’s modulus. When the roots are complex their product β\beta is the squared modulus, so both shrink by β\sqrt\beta per step, independent of hh. Choosing β\beta so that this holds for every eigenvalue gives β⋆=(κ−1κ+1)2\beta^\star = \left(\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}\right)^2 and a rate of about 1−1/κ1 - 1/\sqrt\kappa instead of 1−1/κ1 - 1/\kappa.

Adam keeps exponential averages of the gradient and of its elementwise square, mtm_t and vtv_t, starting from 0. Because they start at 0, early averages are too small by the factor 1−βt1 - \beta^t (q17), and dividing by it gives m^t\hat m_t and v^t\hat v_t. The step η m^t/(v^t+ϵ)\eta\, \hat m_t / (\sqrt{\hat v_t} + \epsilon) is per-coordinate: dividing by the root mean square of recent gradients makes it roughly η\eta in size whatever the gradient’s scale. On the first step m^1=g1\hat m_1 = g_1 and v^1=g12\hat v_1 = g_1^2, so the step is η⋅sign(g1)\eta \cdot \mathrm{sign}(g_1) when ϵ=0\epsilon = 0. AdamW adds weight decay as a separate multiplication of the weights by 1−ηλ1 - \eta\lambda, instead of adding λθ\lambda\theta to the gradient where v^\hat v would rescale it.

Before the set itself, here are two siblings of q5 and q16, worked completely, so you can see the level of detail the answers expect.

Consider step sizes on f(x)=12(2x12+6x22)f(x) = \frac12(2x_1^2 + 6x_2^2). The Hessian is diag(2,6)\mathrm{diag}(2, 6), so μ=2\mu = 2, L=6L = 6, κ=3\kappa = 3. Gradient descent converges for 0<η<2/6=1/30 < \eta < 2/6 = 1/3. The best constant step is η=2/(6+2)=1/4\eta = 2/(6 + 2) = 1/4, where the factors are 1−2/4=1/21 - 2/4 = 1/2 and 1−6/4=−1/21 - 6/4 = -1/2: both coordinates shrink by 1/21/2 per step, the rate (κ−1)/(κ+1)=2/4(\kappa - 1)/(\kappa + 1) = 2/4. At η=1/L=1/6\eta = 1/L = 1/6 the factors are 2/32/3 and 00, so the rate is 2/3=1−1/κ2/3 = 1 - 1/\kappa: slower, though the stiff coordinate converges in one step. From x0=(1,1)x_0 = (1, 1) with η=1/4\eta = 1/4: x1=(1/2,−1/2)x_1 = (1/2, -1/2), x2=(1/4,1/4)x_2 = (1/4, 1/4).

Now take one AdamW step with θ0=2\theta_0 = 2, g1=−5g_1 = -5, η=1/10\eta = 1/10, λ=1/2\lambda = 1/2, ϵ=0\epsilon = 0. Then m^1=−5\hat m_1 = -5, v^1=25\hat v_1 = 25, the adaptive step is −η⋅(−5)/5=+1/10-\eta \cdot (-5)/5 = +1/10, and the decay is −ηλθ0=−1/10-\eta\lambda\theta_0 = -1/10. So θ1=2−1/10+1/10=2\theta_1 = 2 - 1/10 + 1/10 = 2: this step’s push upward and the decay cancel exactly.

The problems are grouped by topic. Each group builds on the toolkit above; open an answer only after you have one of your own.

q1. Is the function convex on the given domain? (a) f(x)=x4f(x) = x^4 on R\mathbb{R}. (b) f(x)=log⁡xf(x) = \log x on (0,∞)(0, \infty). (c) LSE(x1,x2)=log⁡(ex1+ex2)\mathrm{LSE}(x_1, x_2) = \log(e^{x_1} + e^{x_2}) on R2\mathbb{R}^2. [bool]

Answer

(a) true, (b) false, (c) true.

  1. (a) f′′(x)=12x2≥0f''(x) = 12x^2 \ge 0 everywhere, so ff is convex. A common mistake is to answer false because f′′(0)=0f''(0) = 0: convexity needs f′′≥0f'' \ge 0, not f′′>0f'' > 0 (that would be strict convexity, and x4x^4 is strictly convex anyway).
  2. (b) f′′(x)=−1/x2<0f''(x) = -1/x^2 < 0 on (0,∞)(0, \infty), so log⁡\log is concave, not convex.
  3. (c) The Hessian of LSE\mathrm{LSE} is diag(p)−pp⊤\mathrm{diag}(p) - pp^\top with p=softmax(x)p = \mathrm{softmax}(x), the covariance matrix of a one-hot draw from pp, so v⊤Hv=Varp(vi)≥0v^\top H v = \mathrm{Var}_p(v_i) \ge 0. This is why cross-entropy, which is LSE\mathrm{LSE} of the logits minus a linear term, is convex in the logits.

q2. f(x)=12x⊤Axf(x) = \frac12 x^\top A x with A=diag(1,10)A = \mathrm{diag}(1, 10). Give (a) the smoothness constant LL, (b) the strong convexity constant μ\mu, and (c) the condition number κ=L/μ\kappa = L/\mu. [number]

Answer

(a) L=10L = 10, (b) μ=1\mu = 1, (c) κ=10\kappa = 10.

The Hessian of 12x⊤Ax\frac12 x^\top A x is AA itself, and its eigenvalues are the diagonal entries 1 and 10. LL is the largest, μ\mu the smallest, so κ=L/μ=10\kappa = L/\mu = 10. A common mistake is κ=1/10\kappa = 1/10, which is μ/L\mu/L: the condition number is at least 1 by definition. This is the kind of quadratic on which the gradient descent of Convexity, smoothness, gradient descent, and Armijo line search shows the (1−1/κ)t(1 - 1/\kappa)^t gap.

q3. On which interval is f(x)=x3−3xf(x) = x^3 - 3x convex? Give the largest one. [interval]

Answer

[0,∞)[0, \infty).

f′(x)=3x2−3f'(x) = 3x^2 - 3 and f′′(x)=6xf''(x) = 6x, which is nonnegative exactly when x≥0x \ge 0. A common mistake is [1,∞)[1, \infty): that is where f′≥0f' \ge 0, where ff is increasing, which says nothing about curvature. Another is (−∞,0](-\infty, 0], the concave side.

q4. Prove: if ff and gg are convex on Rn\mathbb{R}^n, then f+gf + g is convex. [proof]

Answer

Proof. Let x,y∈Rnx, y \in \mathbb{R}^n and t∈[0,1]t \in [0, 1]. By convexity of ff and of gg at the same xx, yy, tt:

f(tx+(1−t)y)≤tf(x)+(1−t)f(y),g(tx+(1−t)y)≤tg(x)+(1−t)g(y).f(tx + (1-t)y) \le t f(x) + (1-t) f(y), \qquad g(tx + (1-t)y) \le t g(x) + (1-t) g(y).

Adding the two inequalities gives

(f+g)(tx+(1−t)y)≤t(f+g)(x)+(1−t)(f+g)(y),(f+g)(tx + (1-t)y) \le t (f+g)(x) + (1-t)(f+g)(y),

which is the definition of convexity for f+gf + g. ■\blacksquare

What makes the proof work: both inequalities are taken at the same points and the same tt, so the right-hand sides add term by term. The same argument with a factor c≥0c \ge 0 shows nonnegative multiples are convex; with c<0c < 0 the inequality flips, which is why the condition is needed.

q5. Gradient descent xt+1=xt−η∇f(xt)x_{t+1} = x_t - \eta \nabla f(x_t) with a constant step η>0\eta > 0. (a) For f(x)=5x2f(x) = 5x^2 (so L=10L = 10), give the set of η>0\eta > 0 for which xt→0x_t \to 0 from every start. [interval] For f(x)=12(x12+10x22)f(x) = \frac12 (x_1^2 + 10 x_2^2): (b) give the step η\eta that minimizes the worst per-coordinate contraction max⁡i∣1−ηλi∣\max_i \lvert 1 - \eta \lambda_i \rvert; [number] (c) give that worst contraction factor; [number] (d) give the worst contraction factor with η=1/L\eta = 1/L. [number]

Answer

(a) (0,1/5)(0, 1/5), (b) η=2/11\eta = 2/11, (c) 9/119/11, (d) 9/109/10.

  1. (a) f′(x)=10xf'(x) = 10x, so xt+1=(1−10η)xtx_{t+1} = (1 - 10\eta) x_t. This converges from every start iff ∣1−10η∣<1\lvert 1 - 10\eta \rvert < 1, i.e. 0<η<2/10=1/50 < \eta < 2/10 = 1/5. A common mistake is (0,1/10)(0, 1/10), the condition η≤1/L\eta \le 1/L: it is sufficient but not the whole set. Another is (0,1/5](0, 1/5]: at η=2/L\eta = 2/L the factor is exactly −1-1 and the iterate flips sign forever without shrinking.
  2. (b) The eigenvalues are μ=1\mu = 1 and L=10L = 10. The worst factor max⁡(∣1−η∣,∣1−10η∣)\max(\lvert 1 - \eta \rvert, \lvert 1 - 10\eta \rvert) is smallest where the two balance, 1−η=−(1−10η)1 - \eta = -(1 - 10\eta), so 11η=211\eta = 2 and η=2/11\eta = 2/11. Not 1/L1/L: that is the safe default, not the optimum.
  3. (c) At η=2/11\eta = 2/11 both factors have magnitude 1−2/11=9/11=(κ−1)/(κ+1)1 - 2/11 = 9/11 = (\kappa - 1)/(\kappa + 1).
  4. (d) At η=1/10\eta = 1/10 the factors are 9/109/10 and 00, so the worst is 9/10=1−1/κ9/10 = 1 - 1/\kappa. A common mistake is 1/10=1/κ1/10 = 1/\kappa, which would predict absurdly fast convergence.

q6. The error shrinks by a factor 9/109/10 per step. What is the smallest number of steps tt with (9/10)t≤10−6(9/10)^t \le 10^{-6}? [number]

Answer

132.

Take logs: tln⁡(9/10)≤−6ln⁡10t \ln(9/10) \le -6 \ln 10, and since ln⁡(9/10)<0\ln(9/10) < 0 dividing flips the inequality: t≥6ln⁡10/ln⁡(10/9)≈13.8155/0.10536≈131.13t \ge 6 \ln 10 / \ln(10/9) \approx 13.8155 / 0.10536 \approx 131.13. The smallest integer is 132. A common mistake is 131, rounding down: after 131 steps the error is still slightly above 10−610^{-6}. This matches the rule of thumb from the toolkit above: at η=1/L\eta = 1/L with κ=10\kappa = 10, each digit costs about κln⁡10≈23\kappa \ln 10 \approx 23 steps (the exact figure is ln⁡10/ln⁡(10/9)≈21.9\ln 10 / \ln(10/9) \approx 21.9).

q7. Run gradient descent on f(x)=x2f(x) = x^2 from x0=1x_0 = 1 with η=1/4\eta = 1/4. Give x3x_3. [number]

Answer

x3=1/8x_3 = 1/8.

f′(x)=2xf'(x) = 2x, so xt+1=xt−14⋅2xt=xt/2x_{t+1} = x_t - \frac14 \cdot 2x_t = x_t / 2. From x0=1x_0 = 1: 1/21/2, 1/41/4, 1/81/8. A common mistake is 27/6427/64, which comes from using f′(x)=xf'(x) = x (dropping the 2), so the factor becomes 3/43/4.

q8. Backtracking (Armijo) line search accepts the first α∈{α0,ρα0,ρ2α0,… }\alpha \in \{\alpha_0, \rho\alpha_0, \rho^2\alpha_0, \dots\} with f(x+αd)≤f(x)+c α ∇f(x)⊤df(x + \alpha d) \le f(x) + c\,\alpha\, \nabla f(x)^\top d. For f(x)=x2f(x) = x^2 at x=1x = 1 with d=−∇f(1)d = -\nabla f(1), α0=1\alpha_0 = 1, c=1/2c = 1/2, ρ=1/2\rho = 1/2, which α\alpha is accepted? [number]

Answer

α=1/2\alpha = 1/2.

At x=1x = 1: f(1)=1f(1) = 1, ∇f(1)=2\nabla f(1) = 2, d=−2d = -2, and ∇f⊤d=−4\nabla f^\top d = -4.

  1. α=1\alpha = 1: x+αd=−1x + \alpha d = -1, f=1f = 1. The test needs 1≤1+12⋅1⋅(−4)=−11 \le 1 + \frac12 \cdot 1 \cdot (-4) = -1. It fails: the step overshoots to the mirror point with no decrease.
  2. α=1/2\alpha = 1/2: x+αd=0x + \alpha d = 0, f=0f = 0. The test needs 0≤1+12⋅12⋅(−4)=00 \le 1 + \frac12 \cdot \frac12 \cdot (-4) = 0. It holds, so 1/21/2 is accepted.

A common mistake is α=1\alpha = 1, accepting α0\alpha_0 without testing it; another is 1/41/4, backtracking once more after the test already passed.

q9. For convex, LL-smooth ff, gradient descent with η=1/L\eta = 1/L satisfies f(xt)−f⋆≤L∥x0−x⋆∥22tf(x_t) - f^\star \le \frac{L \lVert x_0 - x^\star \rVert^2}{2t}. Give the bound for L=4L = 4, ∥x0−x⋆∥=3\lVert x_0 - x^\star \rVert = 3, t=10t = 10. [number]

Answer

9/59/5.

L∥x0−x⋆∥22t=4⋅92⋅10=3620=95\frac{L \lVert x_0 - x^\star \rVert^2}{2t} = \frac{4 \cdot 9}{2 \cdot 10} = \frac{36}{20} = \frac95. A common mistake is 18/518/5, dropping the 2 in the denominator. Note the 1/t1/t rate: without strong convexity, halving the gap costs twice as many steps, not a constant number.

Heavy-ball momentum: vt+1=βvt+∇f(xt)v_{t+1} = \beta v_t + \nabla f(x_t), xt+1=xt−η vt+1x_{t+1} = x_t - \eta\, v_{t+1}, with v0=0v_0 = 0.

q10. Suppose the gradient is a constant gg. (a) Give lim⁡t→∞vt\lim_{t \to \infty} v_t. [expr in g, beta] (b) For β=0.9\beta = 0.9, by what factor is the long-run step larger than plain gradient descent’s step ηg\eta g? [number]

Answer

(a) g/(1−β)g/(1 - \beta), (b) 10.

  1. (a) With v0=0v_0 = 0: v1=gv_1 = g, v2=βg+gv_2 = \beta g + g, and in general vt=g(1+β+⋯+βt−1)=g1−βt1−β→g1−βv_t = g(1 + \beta + \dots + \beta^{t-1}) = g\frac{1 - \beta^t}{1 - \beta} \to \frac{g}{1 - \beta}. A common mistake is βg/(1−β)\beta g/(1 - \beta), which sums from the second term only and forgets that the current gradient enters with weight 1.
  2. (b) The long-run step is ηg/(1−β)\eta g/(1 - \beta), so the factor is 1/(1−0.9)=101/(1 - 0.9) = 10. A common mistake is 9, which is β/(1−β)\beta/(1 - \beta). This factor is why a learning rate tuned for plain SGD must shrink by about 1−β1 - \beta when momentum is turned on.

q11. On f(x)=h2x2f(x) = \frac{h}{2} x^2 the iterates satisfy xt+1=(1+β−ηh) xt−β xt−1x_{t+1} = (1 + \beta - \eta h)\, x_t - \beta\, x_{t-1}. (a) Give the monic characteristic polynomial in rr whose roots are the modes xt∝rtx_t \propto r^t. [expr in r, beta, eta, h] (b) When its two roots are complex, both have modulus β\sqrt{\beta}. Give that modulus for β=81/100\beta = 81/100. [number]

Answer

(a) r2−(1+β−ηh)r+βr^2 - (1 + \beta - \eta h) r + \beta, (b) 9/109/10.

  1. (a) Substitute xt=rtx_t = r^t into xt+1=(1+β−ηh)xt−βxt−1x_{t+1} = (1 + \beta - \eta h)x_t - \beta x_{t-1} and divide by rt−1r^{t-1}: r2=(1+β−ηh)r−βr^2 = (1 + \beta - \eta h) r - \beta, i.e. r2−(1+β−ηh)r+β=0r^2 - (1 + \beta - \eta h) r + \beta = 0. A common mistake is +…−β+\ldots - \beta, the wrong sign on the xt−1x_{t-1} term; the product of the roots would then be −β-\beta and the analysis would predict a mode of modulus above 1. Another is r2−(1−ηh)rr^2 - (1 - \eta h) r, plain gradient descent with a spurious root at 0.
  2. (b) For a monic quadratic the product of the roots is the constant term, β\beta. Complex roots are conjugates, so rrˉ=∣r∣2=βr \bar r = \lvert r \rvert^2 = \beta and ∣r∣=81/100=9/10\lvert r \rvert = \sqrt{81/100} = 9/10. A common mistake is 81/10081/100, which is β\beta itself rather than its square root.

q12. The best heavy-ball momentum on a quadratic with condition number κ\kappa is β⋆=(κ−1κ+1)2\beta^\star = \left(\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}\right)^2. Give β⋆\beta^\star for κ=100\kappa = 100. [number]

Answer

81/12181/121.

100=10\sqrt{100} = 10, so β⋆=(10−110+1)2=(911)2=81121\beta^\star = \left(\frac{10 - 1}{10 + 1}\right)^2 = \left(\frac{9}{11}\right)^2 = \frac{81}{121}. A common mistake is 9/119/11, forgetting the square; another is 99/10199/101, using κ\kappa where κ\sqrt\kappa belongs. The convergence rate is then β⋆=9/11\sqrt{\beta^\star} = 9/11, which for κ=100\kappa = 100 beats plain gradient descent’s best rate of 99/10199/101 by a wide margin: that is the κ\sqrt\kappa speed-up.

q13. With β=1/2\beta = 1/2 and gradient 11 at every step, give v3v_3. [number]

Answer

v3=7/4v_3 = 7/4.

v1=12⋅0+1=1v_1 = \frac12 \cdot 0 + 1 = 1, v2=12⋅1+1=32v_2 = \frac12 \cdot 1 + 1 = \frac32, v3=12⋅32+1=74v_3 = \frac12 \cdot \frac32 + 1 = \frac74. A common mistake is 3/23/2 (v2v_2) or 15/815/8 (v4v_4): an off-by-one in where the sum starts. With v0=0v_0 = 0, vtv_t has exactly tt terms.

Adam keeps mt=β1mt−1+(1−β1)gtm_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t and vt=β2vt−1+(1−β2)gt2v_t = \beta_2 v_{t-1} + (1 - \beta_2) g_t^2 from m0=v0=0m_0 = v_0 = 0, corrects m^t=mt/(1−β1t)\hat m_t = m_t / (1 - \beta_1^t) and v^t=vt/(1−β2t)\hat v_t = v_t / (1 - \beta_2^t), and steps θt=θt−1−η m^t/(v^t+ϵ)\theta_t = \theta_{t-1} - \eta\, \hat m_t / (\sqrt{\hat v_t} + \epsilon). AdamW also decays the weights: θt=θt−1−ηλθt−1−η m^t/(v^t+ϵ)\theta_t = \theta_{t-1} - \eta \lambda \theta_{t-1} - \eta\, \hat m_t / (\sqrt{\hat v_t} + \epsilon).

q14. If every gradient equals the same gg, give mtm_t. [expr in g, beta1, t]

Answer

mt=g(1−β1t)m_t = g(1 - \beta_1^t).

Unroll: mt=(1−β1)∑i=1tβ1t−ig=g(1−β1)1−β1t1−β1=g(1−β1t)m_t = (1 - \beta_1) \sum_{i=1}^{t} \beta_1^{t-i} g = g (1 - \beta_1)\frac{1 - \beta_1^t}{1 - \beta_1} = g(1 - \beta_1^t). A common mistake is gg, which is the bias-corrected m^t\hat m_t, not mtm_t. With β1=0.9\beta_1 = 0.9, m1=0.1gm_1 = 0.1 g: without the correction the first step would be ten times too small, which is the bias that Adam and AdamW removes.

q15. Take ϵ=0\epsilon = 0. On the first step the gradient is g1=−3g_1 = -3 and η=1/100\eta = 1/100. Give θ1−θ0\theta_1 - \theta_0. [number]

Answer

θ1−θ0=1/100\theta_1 - \theta_0 = 1/100.

  1. m1=(1−β1)g1m_1 = (1 - \beta_1) g_1 and m^1=m1/(1−β1)=g1=−3\hat m_1 = m_1/(1 - \beta_1) = g_1 = -3.
  2. v1=(1−β2)g12v_1 = (1 - \beta_2) g_1^2 and v^1=g12=9\hat v_1 = g_1^2 = 9, so v^1=3\sqrt{\hat v_1} = 3.
  3. θ1−θ0=−η m^1/v^1=−1100⋅−33=+1100\theta_1 - \theta_0 = -\eta\,\hat m_1/\sqrt{\hat v_1} = -\frac{1}{100} \cdot \frac{-3}{3} = +\frac{1}{100}.

The first step is −η sign(g1)-\eta\,\mathrm{sign}(g_1) whatever the size of the gradient. A common mistake is −1/100-1/100 (moving with the gradient instead of against it); another is 3/1003/100 (using the raw gradient size, as plain SGD would).

q16. AdamW with ϵ=0\epsilon = 0, θ0=1\theta_0 = 1, g1=4g_1 = 4, η=1/10\eta = 1/10, λ=1/10\lambda = 1/10. Give θ1\theta_1. [number]

Answer

θ1=89/100\theta_1 = 89/100.

  1. The adaptive step is −η sign(g1)=−110-\eta\,\mathrm{sign}(g_1) = -\frac{1}{10}, by the same argument as q15.
  2. The decoupled decay is −ηλθ0=−110⋅110⋅1=−1100-\eta\lambda\theta_0 = -\frac{1}{10}\cdot\frac{1}{10}\cdot 1 = -\frac{1}{100}.
  3. θ1=1−1100−110=89100\theta_1 = 1 - \frac{1}{100} - \frac{1}{10} = \frac{89}{100}.

A common mistake is 9/109/10 (forgot the decay) or 99/10099/100 (forgot the Adam step). Had the decay been added to the gradient instead (g′=g+λθg' = g + \lambda\theta), Adam’s normalization would have divided it by v^\sqrt{\hat v} and the first step would still be exactly −η-\eta, the decay would vanish into the sign: that is why AdamW keeps it outside.

q17. Prove that if every gig_i has the same expectation E[g]E[g], then E[mt]=(1−β1t) E[g]E[m_t] = (1 - \beta_1^t)\, E[g], so m^t\hat m_t is an unbiased estimate of E[g]E[g]. [proof]

Answer

Proof. From m0=0m_0 = 0 and mt=β1mt−1+(1−β1)gtm_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t, induction on tt gives

mt=(1−β1)∑i=1tβ1t−igi.m_t = (1 - \beta_1) \sum_{i=1}^{t} \beta_1^{t-i} g_i.

(Base case t=1t = 1: m1=(1−β1)g1m_1 = (1 - \beta_1) g_1. Step: β1mt−1+(1−β1)gt=(1−β1)(∑i=1t−1β1t−igi+gt)\beta_1 m_{t-1} + (1 - \beta_1) g_t = (1 - \beta_1)\left(\sum_{i=1}^{t-1} \beta_1^{t-i} g_i + g_t\right).) Taking expectations and using linearity with E[gi]=E[g]E[g_i] = E[g] for every ii:

E[mt]=(1−β1)E[g]∑k=0t−1β1k=(1−β1)E[g]1−β1t1−β1=(1−β1t) E[g].E[m_t] = (1 - \beta_1) E[g] \sum_{k=0}^{t-1} \beta_1^{k} = (1 - \beta_1) E[g] \frac{1 - \beta_1^t}{1 - \beta_1} = (1 - \beta_1^t)\,E[g].

Dividing by the constant 1−β1t1 - \beta_1^t gives E[m^t]=E[g]E[\hat m_t] = E[g], so m^t\hat m_t is unbiased. ■\blacksquare

The same argument with gi2g_i^2 shows E[v^t]=E[g2]E[\hat v_t] = E[g^2]. The assumption that every gig_i has the same mean is what makes the geometric sum factor out; in training the mean drifts slowly, which is why the correction is exact only in this idealization but matters most in the first few hundred steps, where β2t\beta_2^t is still far from 0.

The common wrong answers in the set fall into a handful of patterns, and each one has a counterpart bug in optimizer code:

PitfallSymptomHow to avoid it
Confusing where ff increases with where it is convexa wrong region of convexitytest f′′≥0f'' \ge 0, not f′≥0f' \ge 0 (q3)
Taking η<1/L\eta < 1/L as the stability limit, or including η=2/L\eta = 2/Lneedlessly small learning rates, or a run that oscillates foreverrequire ∣1−ηλ∣<1\lvert 1 - \eta\lambda \rvert < 1 strictly, giving the open interval (0,2/L)(0, 2/L) (q5(a))
Rate 1/κ1/\kappa instead of 1−1/κ1 - 1/\kappaabsurd step-count estimatescompute the factor 1−ηλi1 - \eta\lambda_i per eigenvalue (q5(d))
Rounding a step count downone step short of the target accuracytake the ceiling of ln⁡(1/ε)/ln⁡(1/ρ)\ln(1/\varepsilon)/\ln(1/\rho) (q6)
Dropping the factor 2 in ∇x2=2x\nabla x^2 = 2xhalf-speed descentdifferentiate before iterating (q7)
Accepting α0\alpha_0 without the Armijo testa step that increases the lossevaluate the sufficient-decrease test at every candidate, including α0\alpha_0 (q8)
Summing momentum from the wrong indexvelocity off by one termstart from v0=0v_0 = 0 so vtv_t has exactly tt terms (q10, q13)
Wrong sign of the xt−1x_{t-1} terma momentum analysis that predicts divergencederive the polynomial from the recurrence; the root product is +β+\beta (q11)
Forgetting the bias correction is about mtm_t‘s start at 0first steps 10 times too smallunroll mtm_t to get the factor 1−β1t1 - \beta_1^t (q14, q17)
Decay inside the adaptive step, or omittedAdamW trajectories that drift from torch’sapply −ηλθ-\eta\lambda\theta separately from −ηm^/v^-\eta\hat m/\sqrt{\hat v} (q16)

The convexity and rate problems (q1 to q9) are the theory behind gradient_descent and armijo_step in Convexity, smoothness, gradient descent, and Armijo line search, whose convergence follows the (1−1/κ)t(1 - 1/\kappa)^t bound of q5. The momentum problems (q10 to q13) analyze SGD with momentum and Nesterov in The Optimizer protocol, and the Adam problems (q14 to q17) check AdamW with bias correction and decoupled decay in Adam and AdamW. Learning-rate schedules (cosine, WSD, Noam) and gradient clipping then changes η\eta over time and bounds the step, and Training loop and the autograd bigram in AI Engineering is the first training loop that calls these optimizers. Beyond this book, the top Hessian eigenvalue during training and the edge of stability, where η≈2/λmax⁡\eta \approx 2/\lambda_{\max}, apply q5’s analysis to real networks. Part b of the set, Optimization problem set, part b, turns to constrained optimization and the derivation behind DPO.

  • On a quadratic, gradient descent multiplies each eigen-direction by 1−ηλi1 - \eta\lambda_i, so it converges exactly when 0<η<2/L0 < \eta < 2/L, and its best rate is κ−1κ+1\frac{\kappa - 1}{\kappa + 1} (q5).
  • The condition number κ=L/μ\kappa = L/\mu sets how many steps a digit of accuracy costs: about κln⁡10\kappa \ln 10 at η=1/L\eta = 1/L (q2, q6).
  • Momentum averages gradients with weights βk\beta^k, so a constant gradient produces steps 1/(1−β)1/(1 - \beta) times larger; on a quadratic its modes shrink by β\sqrt\beta per step (q10, q11).
  • Adam’s moments start at zero and are biased toward it by a factor 1−βt1 - \beta^t; the bias correction removes exactly that, and the first step is η\eta times the sign of the gradient (q14, q15, q17).
  • AdamW’s decay multiplies the weights by 1−ηλ1 - \eta\lambda outside the adaptive step, so it is not rescaled by v^\hat v (q16).