Skip to content

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

ModuleS-M10a · solve · none · Pass 2 · 4 to 5 h
You buildanswers in solve/S-M10a.toml (24 checked by SymPy) and 2 proofs in solve/S-M10a/qN.md (self-graded against their rubrics)
Contractnone: a pen and paper set
Testscourse/solve/S-M10a/key.toml (hidden): typed answers plus reject canaries; the problems are in course/solve/S-M10a/problems.md and in section 4
NeedsS-M05 (proof habits). Reading: the Optimization topic, and gradients and Hessians from S-M04
Used byno call site (a solve set). It checks the analysis behind M10.1 (gradient descent, Armijo, the condition number), M10.2 (SGD with momentum), and M10.3 (Adam and AdamW with bias correction); part b, S-M10b, is optional with L12
MilestoneMS-P2 (the Pass 2 gate runs ol check on every solve part of the pass)
Optional depthBoyd and Vandenberghe, Convex Optimization (free), ch. 3 and 9; Goh, “Why Momentum Really Works” (Distill, 2017); Kingma and Ba, “Adam” (2015), sections 2 and 3; Loshchilov and Hutter, “Decoupled Weight Decay Regularization” (2019)
  • 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).
Terminal window
ol start S-M10a # writes solve/S-M10a.toml and one file per proof
ol check S-M10a # SymPy checks the answers, then asks each proof rubric (y/n)
ol check S-M10a --regrade # ask the rubrics again after you change a proof

Your Pass 1 bigram was fitted by counting, with no optimizer at all. From Pass 2 on, every model is trained by an optimizer you write: M10.1 gradient descent with a line search, M10.2 SGD with momentum, and M10.3 AdamW, which trains everything from L4.1 to the capstone. Their tests compare your trajectories with PyTorch’s step by step, and they fail 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 in Pass 5, 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?

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.

2.2 Smoothness, strong convexity, and the condition number

Section titled “2.2 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.

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). 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.

This is a sibling of q5 and q16, not one of the graded problems.

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). In solve/ the interval would be answer = "(0, 1/3)".

One AdamW step. θ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.

Write each answer in solve/S-M10a.toml; lettered parts are their own tables:

[q5.a]
answer = "(0, 1/5)"
[q10.a]
answer = "g/(1 - beta)"
[q14]
answer = "g*(1 - beta1^t)"
[q17]
proof = "S-M10a/q17.md"

Rates and steps are exact fractions; write β\beta as beta, β1\beta_1 as beta1, η\eta as eta, and intervals as (a, b), [a, b), or [a, oo).

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]

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]

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

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

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]

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]

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]

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]

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]

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]

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]

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]

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

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]

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]

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]

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]

PitfallSymptomCaught by
Confusing where ff increases with where it is convexa wrong region of convexityq3 (canary [1, oo))
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 foreverq5 (canaries (0, 1/10) and (0, 1/5])
Rate 1/κ1/\kappa instead of 1−1/κ1 - 1/\kappaabsurd step-count estimatesq5 (canary 1/10)
Rounding a step count downone step short of the target accuracyq6 (canary 131)
Dropping the factor 2 in ∇x2=2x\nabla x^2 = 2xhalf-speed descentq7 (canary 27/64)
Accepting α0\alpha_0 without the Armijo testa step that increases the lossq8 (canary 1)
Summing momentum from the wrong indexvelocity off by one termq10 (canary), q13 (canaries 3/2 and 15/8)
Wrong sign of the xt−1x_{t-1} terma momentum analysis that predicts divergenceq11 (canary)
Forgetting the bias correction is about mtm_t‘s start at 0first steps 10 times too smallq14 (canary g)
Decay inside the adaptive step, or omittedAdamW trajectories that drift from torch’sq16 (canaries 9/10 and 99/100)
DirectionModuleHow it uses this
BackS-M05proof habits for q4 and q17
ForwardM10.1gradient_descent and armijo_step, tested by the (1−1/κ)t(1 - 1/\kappa)^t bound of q5
ForwardM10.2SGD with momentum and Nesterov: q10 to q13
ForwardM10.3AdamW with bias correction and decoupled decay: q14 to q17
ForwardM10.4schedules change η\eta over time; clipping bounds the step
ForwardL0.5the first training loop that calls your optimizer
ForwardM10.5optional: the top Hessian eigenvalue and the edge of stability, where η≈2/λmax⁡\eta \approx 2/\lambda_{\max}