5.6.9 · D4Machine Learning (Aerospace Applications)

Exercises — Optimization — SGD, momentum, Adam — derivations

2,823 words13 min readBack to topic

Throughout, symbols mean exactly what the parent note defined. One naming warning up front: the letter is used for two different things in this chapter, and mixing them is the #1 source of confusion — so we give each its own name here and never write a bare again.


Level 1 — Recognition

L1.1 Which direction?

You stand at and the gradient is (loss goes up as grows). With , does plain gradient descent move left (smaller) or right (larger), and to what value from ?

Recall Solution

WHAT: the update is . WHY: points downhill; a positive means uphill is to the right, so we step left. Moves left (smaller), to . ✔

L1.2 Name the term

In with , what is called and what does collapse to?

Recall Solution

is the momentum coefficient (a decay/memory factor). With : which is plain SGD. So momentum with no memory is SGD. ✔


Level 2 — Application

L2.1 One SGD step on a quadratic

, so . From , , compute and .

Recall Solution

WHAT: apply with . WHY : differentiating gives — the slope of a parabola at position is exactly , so the gradient literally equals where you stand. : . WHY it shrank by exactly : substituting makes the rule — every step scales by the same factor. : . So — a clean geometric decay toward the valley at . ✔

L2.2 Two momentum steps

Same , , , , . Compute and , and compare to SGD's .

Recall Solution

WHAT: run then . Step 0: , , . WHY step 0 matches SGD: with there is no stored velocity yet, so the first velocity is just — momentum has nothing extra to add on its very first move. Step 1: , , . WHY step 1 pulls ahead: keeps of the old velocity () and adds the fresh gradient (), so the velocity is nearly double the raw gradient — the ball has picked up speed in the consistent downhill direction. Momentum reaches vs SGD's faster, because accumulated the first gradient. ✔

L2.3 One Adam step

Same , , , . Compute .

Recall Solution

WHAT: compute the two moments, bias-correct, then step. . , bias-correct: . WHY the correction: starts from , so a single update only reaches — a shrunken version of the true gradient . Dividing by undoes that exact shrink and recovers the honest estimate . , bias-correct: , so . WHY divide by : estimates , so — dividing by it cancels the gradient's size, leaving only its sign. Adam's first step is magnitude-independent. ✔

Reading the figure below. The horizontal axis is the parameter , the vertical axis is the loss (the pale chalk parabola). All three optimizers start at (top right). Three coloured dotted-with-dots tracks show successive positions: blue = SGD, pink = Momentum, yellow = Adam. Each track is drawn slightly above the true parabola so you can tell them apart — the vertical offset is cosmetic only, read the horizontal position for the real . Takeaway: watch how the pink Momentum dots pull ahead (land further left per step) while blue SGD lags, and yellow Adam takes a deliberately modest, evenly-scaled first stride — the three L2 answers made visual.

Figure — Optimization — SGD, momentum, Adam — derivations

Level 3 — Analysis

L3.1 The amplification

On a steady slope the gradient is a constant . Show the momentum velocity converges to and evaluate for .

Recall Solution

WHAT: unroll from : WHY that sum: each step keeps a -fraction of the old velocity and adds the fresh — a geometric series (see Exponential Moving Average). As , (since ), so . For : — a 10× effective learning rate on steady directions. ✔

L3.2 Oscillation cancellation

Suppose a coordinate's gradient alternates: (steep ravine wall bouncing). Compute with , , , and explain what happens to the running velocity versus a same-sign case.

Recall Solution

. After the first entry , the velocity magnitude stays small (from on it bounces between about and ) because opposite-sign gradients keep cancelling. In a same-sign case it would instead have grown toward . That is exactly how momentum damps oscillations while accelerating consistent directions. ✔

L3.3 Why SGD escapes a saddle

At a saddle point , so full-batch GD's update is — it never moves. Explain, using , why an SGD mini-batch step generically does move.

Recall Solution

The mini-batch gradient is unbiased: at the saddle. But unbiased only means the average is zero — the actual has nonzero variance, so a typical draw is . That random kick pushes off the saddle along some direction; if it lands on a downhill escape direction (there always is one at a saddle) the loss drops. See Saddle Points and Loss Landscapes. This is why SGD's noise is a feature, not a bug.


Level 4 — Synthesis

L4.1 Derive Adam's bias correction

With and stationary gradients of mean , show and hence why we divide by .

Recall Solution

WHAT: unroll from : WHY: each new gradient enters with weight and old ones decay by . Take expectation with :

=(1-\beta_1)\,g\cdot\frac{1-\beta_1^{t}}{1-\beta_1}=g\,(1-\beta_1^{t}).$$ Since $1-\beta_1^{t}<1$ for small $t$, raw $m_t$ **underestimates** $g$ (biased toward 0). Dividing: $$\hat m_t=\frac{m_t}{1-\beta_1^{t}} \;\Rightarrow\; \mathbb{E}[\hat m_t]=g.$$ Same algebra gives $\hat s_t=s_t/(1-\beta_2^{t})$. ✔

L4.2 Bias-correction shrink factor

By how much does the bias correction inflate the first-step first moment for (i.e. the factor )? And for the second moment at with ?

Recall Solution

First moment, : . Second moment, : . These are exactly the inflations we used in L2.3: () and (). ✔

Reading the figure below. The horizontal axis is the step counter ; the vertical axis (on a log scale, so equal spacing means equal multiplying factor) is the inflation factor that bias correction applies. Pink is the first-moment factor ; blue is the second-moment factor . The yellow dashed line at 1 is "no correction needed." Takeaway: at the pink factor is and the blue is a huge — wildly different — but both curves slide down toward the yellow line as grows, so the correction quietly switches itself off once training is underway. This is why skipping it only hurts the very first steps.

Figure — Optimization — SGD, momentum, Adam — derivations

L4.3 Adam on a noise-only coordinate

A coordinate receives zero-mean noise: gradients with , . Argue the long-run Adam step , and contrast with a steady-gradient coordinate where the step .

Recall Solution

Noise case: but . Steady case (): , , so Adam trusts consistent directions (unit step) and ignores pure noise (zero step) — a per-coordinate signal-to-noise filter. ✔


Level 5 — Mastery

L5.1 One-step descent guarantee

Using the first-order Taylor Expansion with , show the loss strictly decreases (to first order) whenever and . What breaks the guarantee?

Recall Solution

WHAT: substitute the GD update into the Taylor expansion: Here is just the definition of the squared length of a vector (an identity, not the Cauchy–Schwarz inequality — the steepest-descent direction used Cauchy–Schwarz back in the parent note; this step is a plain dot-product-with-itself). WHY it decreases: and equals only if . With , the change , so drops. What breaks it: the approximation is only first-order; if is large the ignored second-order (curvature) term can dominate and may rise — the overshoot from the L1/L3 traps. ✔

L5.2 Convergence rate on the quadratic

For (so ), the SGD map is . For what does , and what gives the fastest convergence? Cover all cases including , , and the non-positive .

Recall Solution

WHAT: iterate the linear map to closed form. Since each step multiplies by the constant , after steps WHY the multiplier decides everything: exactly when the powers shrink, i.e. when . Solve : Now walk every case of the multiplier :

  • (non-positive rate): , so and never shrinks. In fact steps in the (uphill) direction — pure gradient ascent, loss grows. gives : frozen forever. So is mandatory.
  • : — smooth monotone shrink toward (our L2.1 case, ).
  • : , so one-step convergence, the fastest possible.
  • : — still converges, but alternates sign each step (oscillating shrink).
  • : , so bounces forever, magnitude never shrinks (marginal, no convergence).
  • : diverges, grows without bound.

Summary: converges ; fastest at ; frozen at ; ascends for ; marginal bounce at ; diverges for . ✔

L5.3 Synthesize: choose the optimizer

An aerospace CFD surrogate net has (a) a noisy mini-batch gradient, (b) a deep ravine loss valley, and (c) layers whose gradient magnitudes differ by . Which single optimizer addresses all three and by which mechanism each?

Recall Solution

Adam. Mechanism-by-mechanism:

  • (a) Noise → the first moment (an Exponential Moving Average of gradients) averages out zero-mean noise, just like momentum.
  • (b) Ravine → the same momentum-like averaging accelerates the consistent along-valley direction and cancels the oscillating across-valley one (L3.2).
  • (c) scale mismatch → dividing by rescales every coordinate to a step, so the -larger gradients don't explode and the tiny ones don't stall (L4.3). One optimizer, three mechanisms. That combination is why Adam is the default for such nets. ✔

Recall Self-test checklist

One-step SGD from on ::: Momentum (same setup, ) ::: Adam (same setup) ::: Momentum steady-slope amplification for ::: Adam first-step second-moment inflation, ::: SGD converges on for which ? ::: , fastest at ; diverges/ascends for or