Optimization problem set, part a: convexity, GD rates, momentum, Adam
Overview
Section titled “Overview”| Module | S-M10a · solve · none · Pass 2 · 4 to 5 h |
| You build | answers in solve/S-M10a.toml (24 checked by SymPy) and 2 proofs in solve/S-M10a/qN.md (self-graded against their rubrics) |
| Contract | none: a pen and paper set |
| Tests | course/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 |
| Needs | S-M05 (proof habits). Reading: the Optimization topic, and gradients and Hessians from S-M04 |
| Used by | no 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 |
| Milestone | MS-P2 (the Pass 2 gate runs ol check on every solve part of the pass) |
| Optional depth | Boyd 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) |
Key Takeaways
Section titled “Key Takeaways”- On a quadratic, gradient descent multiplies each eigen-direction by , so it converges exactly when , and its best rate is (q5).
- The condition number sets how many steps a digit of accuracy costs: about at (q2, q6).
- Momentum averages gradients with weights , so a constant gradient produces steps times larger; on a quadratic its modes shrink by per step (q10, q11).
- Adam’s moments start at zero and are biased toward it by a factor ; the bias correction removes exactly that, and the first step is times the sign of the gradient (q14, q15, q17).
- AdamW’s decay multiplies the weights by outside the adaptive step, so it is not rescaled by (q16).
How to work this chapter
Section titled “How to work this chapter”ol start S-M10a # writes solve/S-M10a.toml and one file per proofol 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 proof1. Why now
Section titled “1. Why now”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 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?
2. Principles
Section titled “2. Principles”| Symbol | Meaning | Type / shape |
|---|---|---|
| the objective (loss) | ||
| a minimizer and the minimum value | ||
| gradient and Hessian | , | |
| smoothness: (largest curvature) | scalar | |
| strong convexity: (smallest curvature) | scalar | |
| condition number | scalar | |
(eta) | step size, the learning rate | scalar |
(beta), | momentum and Adam’s moment decay rates | in |
| Adam’s first and second moment estimates | like | |
| AdamW weight decay | scalar |
2.1 Convexity
Section titled “2.1 Convexity”A function is convex if every chord lies on or above it: for . For twice-differentiable this is equivalent to in one variable, and to a positive semidefinite Hessian ( for every ) in several. Convexity matters because every local minimum of a convex function is global. Sums and nonnegative multiples of convex functions are convex (q4); is concave; 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”is -smooth when its gradient changes by at most per unit of distance, which for twice-differentiable means every Hessian eigenvalue is at most ; it is -strongly convex when every eigenvalue is at least . For a quadratic with symmetric , and are the largest and smallest eigenvalues of . The condition number measures how elongated the level sets are: is a round bowl, a long narrow valley.
2.3 Gradient descent on a quadratic
Section titled “2.3 Gradient descent on a quadratic”In the eigenbasis of , gradient descent acts on each coordinate separately: coordinate is multiplied by every step. It converges from every start exactly when for every , that is . The worst factor is smallest when the extreme eigenvalues balance, , giving and rate . The safe default gives rate . With rate , reducing the error by a factor takes steps. Without strong convexity, an -smooth convex still satisfies at .
Backtracking (Armijo) line search avoids knowing : start with and shrink by until the step decreases by at least a fraction of what the linear model predicts, .
2.4 Momentum
Section titled “2.4 Momentum”Heavy-ball momentum keeps a velocity and steps . Unrolled, : an exponentially weighted sum of past gradients, which for a constant gradient approaches . On a one-dimensional quadratic with curvature the iterates satisfy the linear recurrence ; substituting gives a quadratic in , and the iterates shrink like the larger root’s modulus. When the roots are complex their product is the squared modulus, so both shrink by per step, independent of . Choosing so that this holds for every eigenvalue gives and a rate of about instead of .
2.5 Adam and AdamW
Section titled “2.5 Adam and AdamW”Adam keeps exponential averages of the gradient and of its elementwise square, and , starting from 0. Because they start at 0, early averages are too small by the factor (q17), and dividing by it gives and . The step is per-coordinate: dividing by the root mean square of recent gradients makes it roughly in size whatever the gradient’s scale. On the first step and , so the step is when . AdamW adds weight decay as a separate multiplication of the weights by , instead of adding to the gradient where would rescale it.
3. Worked example by hand
Section titled “3. Worked example by hand”This is a sibling of q5 and q16, not one of the graded problems.
Step sizes on . The Hessian is , so , , . Gradient descent converges for . The best constant step is , where the factors are and : both coordinates shrink by per step, the rate . At the factors are and , so the rate is : slower, though the stiff coordinate converges in one step. From with : , . In solve/ the interval would be answer = "(0, 1/3)".
One AdamW step. , , , , . Then , , the adaptive step is , and the decay is . So : this step’s push upward and the decay cancel exactly.
4. The problem set
Section titled “4. The problem set”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 as beta, as beta1, as eta, and intervals as (a, b), [a, b), or [a, oo).
Convexity and the condition number
Section titled “Convexity and the condition number”q1. Is the function convex on the given domain? (a) on . (b) on . (c) on . [bool]
q2. with . Give (a) the smoothness constant , (b) the strong convexity constant , and (c) the condition number . [number]
q3. On which interval is convex? Give the largest one. [interval]
q4. Prove: if and are convex on , then is convex. [proof]
Gradient descent rates and step size
Section titled “Gradient descent rates and step size”q5. Gradient descent with a constant step .
(a) For (so ), give the set of for which from every start. [interval]
For :
(b) give the step that minimizes the worst per-coordinate contraction ; [number]
(c) give that worst contraction factor; [number]
(d) give the worst contraction factor with . [number]
q6. The error shrinks by a factor per step. What is the smallest number of steps with ? [number]
q7. Run gradient descent on from with . Give . [number]
q8. Backtracking (Armijo) line search accepts the first with . For at with , , , , which is accepted? [number]
q9. For convex, -smooth , gradient descent with satisfies . Give the bound for , , . [number]
Momentum
Section titled “Momentum”Heavy-ball momentum: , , with .
q10. Suppose the gradient is a constant . (a) Give . [expr in g, beta] (b) For , by what factor is the long-run step larger than plain gradient descent’s step ? [number]
q11. On the iterates satisfy .
(a) Give the monic characteristic polynomial in whose roots are the modes . [expr in r, beta, eta, h]
(b) When its two roots are complex, both have modulus . Give that modulus for . [number]
q12. The best heavy-ball momentum on a quadratic with condition number is . Give for . [number]
q13. With and gradient at every step, give . [number]
Adam keeps and from , corrects and , and steps . AdamW also decays the weights: .
q14. If every gradient equals the same , give . [expr in g, beta1, t]
q15. Take . On the first step the gradient is and . Give . [number]
q16. AdamW with , , , , . Give . [number]
q17. Prove that if every has the same expectation , then , so is an unbiased estimate of . [proof]
5. Pitfalls
Section titled “5. Pitfalls”| Pitfall | Symptom | Caught by |
|---|---|---|
| Confusing where increases with where it is convex | a wrong region of convexity | q3 (canary [1, oo)) |
| Taking as the stability limit, or including | needlessly small learning rates, or a run that oscillates forever | q5 (canaries (0, 1/10) and (0, 1/5]) |
| Rate instead of | absurd step-count estimates | q5 (canary 1/10) |
| Rounding a step count down | one step short of the target accuracy | q6 (canary 131) |
| Dropping the factor 2 in | half-speed descent | q7 (canary 27/64) |
| Accepting without the Armijo test | a step that increases the loss | q8 (canary 1) |
| Summing momentum from the wrong index | velocity off by one term | q10 (canary), q13 (canaries 3/2 and 15/8) |
| Wrong sign of the term | a momentum analysis that predicts divergence | q11 (canary) |
| Forgetting the bias correction is about ‘s start at 0 | first steps 10 times too small | q14 (canary g) |
| Decay inside the adaptive step, or omitted | AdamW trajectories that drift from torch’s | q16 (canaries 9/10 and 99/100) |
6. Where it’s used next
Section titled “6. Where it’s used next”| Direction | Module | How it uses this |
|---|---|---|
| Back | S-M05 | proof habits for q4 and q17 |
| Forward | M10.1 | gradient_descent and armijo_step, tested by the bound of q5 |
| Forward | M10.2 | SGD with momentum and Nesterov: q10 to q13 |
| Forward | M10.3 | AdamW with bias correction and decoupled decay: q14 to q17 |
| Forward | M10.4 | schedules change over time; clipping bounds the step |
| Forward | L0.5 | the first training loop that calls your optimizer |
| Forward | M10.5 | optional: the top Hessian eigenvalue and the edge of stability, where |