Every model that learns is a walker on a landscape it cannot see. The ground is "how wrong am I right now", and learning is nothing more than walking downhill in thick fog — feel the tilt under your boots, step the other way, feel again. This unit is that walk, told in pictures you can push around: why there is a landscape at all, where the walk stops, how long a step to take (and what goes wrong when you are greedy), how to let the ground choose your stride, and the trick that makes the walk possible when the landscape is built from a million examples — read a spoonful of the data, not the pot. The formulas arrive after the pictures, as the sentence you would have written yourself.
≈ 90 min read + play11 interactive widgets · 5 in 3D · a fog you can lift, a race you can run15 inline checks🧾 16 proofs, folded away — open "if you want the algebra" when you are ready✍ 12 solved practice problems
drag the landscape to orbit
1
Downhill in the fog
Imagine this
Night, a mountainside, fog so thick you can see one metre of ground around your boots and nothing else. You want the bottom of the valley. No map, no view, no idea where the bottom is. But you have one sense that works perfectly: you can feel which way the ground tilts under your feet. So you face straight downhill, take a step, stop, feel again, turn a little, step again. That is the entire method, and it is enough. Try it — the landscape below is real, and you can only see the lit disc:
Walking in the fogThe whole unit in one picture: a landscape you cannot see, a lit disc of ground you can, and a walker who only knows which way is downhill
Try: press ▶ walk and watch the lit disc slide downhill until the ground goes flat under the walker. Then press lift the fog — there were two valleys all along, and the walk found the one downhill from where it started.
drag the picture to orbit · drag the glowing walker to move it
—
Nothing in the rule x←x−γ∇f(x) needs the landscape: only the tilt of the ground under your feet, which the gradient gives. That is the whole reason it scales to a million dimensions — and the whole reason it can only find the valley downhill from where it starts. Drag the walker onto the hilltop to feel a flat spot that is not a bottom.
Three things to take from the fog, because everything else in the unit is a variation on them.
You never needed the map. The walker reached a valley bottom without ever seeing one. All it used, step after step, was the tilt of the ground at its own feet. That matters more than it sounds: a modern model has millions of knobs, so its landscape lives in millions of dimensions — nobody can see it, draw it, or search it. But "tilt under the feet" is a quantity you can compute at any point, in any number of dimensions. The fog is not a limitation we tolerate. It is the situation we are always in, and the method was designed for it.
Downhill is a direction, and the tilt tells you which. Lift the fog and look at the arrows: the orange one points the way the ground rises fastest, and the green step points exactly opposite. In the unit's language the orange arrow is the gradient, written ∇f; it is the compass needle that always points uphill, and its length says how steep. The walker's whole strategy in one line:
The realization
new position=position−γ∇f(position)
Take the uphill needle, flip it, scale it by a stride γ, step. The formula is not a rule imposed on the picture; it is the picture, written down.
You find the valley downhill from where you started. Press lift the fog after a walk: there were two valleys all along, and the walker never knew. It went to the one its slope led to. Start on the far side and it goes to the other. Gradient descent finds a low point, the nearest one downhill — not the lowest. Keep this in your pocket; it comes back in Section 3 and again at the very end.
One more word, then we start walking for real. The walk assumes there are no fences — you may put your feet anywhere. Real problems sometimes fence you in (parameters that must stay positive, weights that must add to one); that is constrained optimization and it gets its own unit. Here, the whole mountainside is yours.
Pause & predict
Under your feet the ground tilts so that the uphill needle is ∇f=(3,−4). Which way is your next step, and how steep is it here?
In one sentence: Learning is a walk in fog — feel the tilt, step the other way — and that is exactly why it works in a million dimensions and exactly why it only finds the valley downhill from where it starts.
2
Why there is a landscape at all
Four receipts
A print shop has four receipts: 1 box of paper cost 3.1, 2 boxes cost 4.9, 3 boxes cost 7.3, 4 boxes cost 9.1. The owner wants a rule of thumb — price ≈ a × boxes + b — so she can quote any order in her head. Any pair (a,b) is a guess. Some guesses are terrible (a flat line at zero), some are close, one is best. Now ask the question that creates the landscape: how wrong is each guess, as a single number?
Draw the guess as a line through the four points. Each receipt sits some distance above or below the line — those red sticks in the picture are the misses. Square each miss (so above and below both count, and a big miss counts a lot) and add them up. That total is the guess's wrongness, and it is the height of the ground at the point (a,b). Every pair of knob settings has a height; put them all together and you have a landscape. Turn the knobs and watch the line tilt and the ball climb or fall:
The fitting bowlFour data points, one line, and the loss surface L(a, b) as a real landscape — drag to orbit, then let the ball roll
Try: press ▶ descend from the origin and watch the ball zig-zag down the narrow trench before crawling along it. Then push γ past 0.030 and press ▶ again — the steps grow instead of shrinking.
drag the picture to orbit
—
The loss L(a,b)=∑i(yi−(axi+b))2 is a bowl, but a lopsided one: its Hessian has one stiff eigenvalue (≈66.8) and one flat (≈1.2). Gradient descent must keep γ<2/66.8≈0.030 to survive the stiff direction, and at that speed it barely moves along the flat one — the trench you see the ball crawl along.
So the landscape is not a metaphor we imported. It is manufactured, automatically, the moment you say what "wrong" means. Any model, any way of scoring it, gives you one — a height for every setting of the knobs. Training a model means walking that landscape down. The fitting bowl here has two knobs so you can see it; a neural network's has millions, and nobody ever sees it, which is why Section 1 mattered.
Two things you can read straight off the picture, before any formula.
The tilt has an obvious meaning. Start at a=b=0, the line lying flat on the floor with every receipt above it. Which way is downhill? Obviously "tilt the line up toward the points". And that is what the ball does: the first step raises both a and b. When the line is above the data, the misses are negative and the tilt points the other way. The gradient of the wrongness is just "which way should the line move to reduce its misses" — the residuals decide the sign, the inputs decide how strongly each knob feels it.
The realization
With misses ri=yi−(axi+b), the wrongness is L=∑ri2 and its tilt is ∇L=(−2∑xiri,−2∑ri): the intercept feels the plain sum of misses, the slope feels each miss weighted by its x. Positive misses ⇒ push the line up. That is all the formula says.
The bowl is a trench, not a dish. Orbit the stage: the valley is long and narrow. Nudge the slope a and the wrongness shoots up — a small change of slope moves the far points a lot. Nudge the intercept b and almost nothing happens. Two knobs, two very different stiffnesses. The number that measures this, the ratio of stiff to soft, is about 56 here. Remember the shape: it is the single biggest reason the walk in Section 4 will zig-zag, and the reason step sizes are hard in Section 5.
You might object: this bowl has a formula for its bottom (a∗=2.04,b∗=1; Practice Problem 11 finds it in three lines). True — and the line is the last model for which that will ever be the case. Make the model any bigger and no formula exists. The walk is not one option among many; it is the only one. That is why we practise it here, where we can check the answer.
Pause & predict
Start at a=b=0: the line lies flat on the floor and all four receipts sit above it. What does the first descent step do to the slope a?
If you want the algebra · 1 proof, step by step
Prove it · the gradient of the squared-error loss
Claim. With residuals ri=yi−(axi+b), ∂a∂L=−2∑ixiri and ∂b∂L=−2∑iri.
1
Name the inside of each square: ri=yi−axi−b. Then L=∑iri2.
Giving the residual a name turns a messy expression into "a sum of squares of something", which is easy to differentiate.
2
Differentiate one square by the chain rule:
∂a∂ri2=2ri⋅∂a∂ri=2ri⋅(−xi)=−2xiri.Outer derivative (2ri) times inner derivative. Inside ri, the only place a appears is −axi, whose derivative is −xi.
3
Same for b: inside ri, b appears as −b, so ∂b∂ri=−1 and
∂b∂ri2=2ri⋅(−1)=−2ri.The intercept touches every residual in the same way, so its partial is just the plain sum of residuals.
4
A derivative of a sum is the sum of the derivatives:
∇L=(−2i∑xiri,−2i∑ri).That is the claim.∎Read it: if the residuals are mostly positive (the line sits below the data), both partials are negative, so the descent step −γ∇Lraisesa and b — the line tilts up toward the points. The compass agrees with common sense.
In one sentence: Say what "wrong" means and a landscape appears by itself — a height for every setting of the knobs — and its tilt is simply "which way should the line move to miss less".
3
Where the walk ends — flat ground comes in three kinds
The marble
Drop a marble on a curved floor: it rolls until the floor under it is level, and there it stops. But level floor is not the same as "the bottom". A marble balanced on top of a hill is on level floor too — one breath and it rolls off. And a marble on a saddle (the middle of a Pringle) is level yet falls if nudged sideways. Flat ground is where the walker stops; whether it stopped somewhere sensible is a second question.
Take the smallest landscape that has all three kinds of flat spot — one dimension, a bumpy curve — and put a probe on it. The probe's tangent is the tilt; where the tangent is level the walk would end. Then press ▶ and let it walk:
The flat-spot proberSlide a probe along a curve, read the slope, and walk downhill — the one-dimensional heart of every optimiser
Try: put the probe at x=−1.3 and press ▶ walk downhill: it slides into the valley at x≈0.66. Now start at x=−1.6, one hair to the left of the hilltop, and it falls into the deep valley at x≈−4.48 instead.
—
The rule is x←x−γf′(x): the slope's sign says which way is downhill, its size says how far to go. At a flat spot the walk stops — but flat is not the same as low: f′′ tells a valley from a hilltop, and nothing in the rule can tell a local valley from the global one.
Watch the probe on the quartic curve. Three flat spots: a deep valley on the left, a hilltop in the middle, a shallow valley on the right. Start the walk anywhere right of the hilltop and it slides into the shallow valley and stops — with a much deeper valley sitting a short distance away behind the hill. It cannot get there. Every step goes downhill from where it is, and getting to the deep valley would mean going up first. Section 1's fog again, now with a number attached: the deep valley is 43 units lower, and the walker will never know.
How do you tell the three flat spots apart without the picture? By the way the ground curves under your feet. In a valley the ground curls up on both sides of you — like standing in a bowl; on a hilltop it curls down on both sides; on a saddle it curls up one way and down the other. The number that records the curl is the second derivative, and its sign is the whole test:
The realization
Flat (f′=0) and curling up (f′′>0) ⇒ a valley, the walk is done. Flat and curling down (f′′<0) ⇒ a hilltop, a nudge sends you rolling. In two or more dimensions the "curl" becomes the Hessian from Unit 8 and the same three verdicts (bowl, dome, saddle) apply. Nothing new — Unit 8's judge was built for exactly this moment.
Why does the curl decide? Picture the ground right next to a flat spot as a parabola — the closest simple shape. A parabola that opens up has its bottom at the flat spot; one that opens down has its top there. The second derivative is just "which way, and how tightly, does the parabola open". For the tiny landscapes of a textbook you can skip the walk entirely: find the flat spots by algebra, test the curl at each, done. For anything real you cannot, and the walk plus the test is all you have.
Pause & predict
You start the walk on the quartic curve at x0=0. Where does it end?
If you want the algebra · 1 proof, step by step
Prove it · the second-derivative test, from Taylor
Claim. If f′(c)=0 and f′′(c)>0, then c is a local minimum; if f′′(c)<0, a local maximum.
1
Write Taylor's formula about c with the remainder from Unit 8, for a small step h:
f(c+h)=f(c)+f′(c)h+21f′′(ξ)h2for some ξ between c and c+h.Value, slope times step, half the bend times step squared — with the bend taken at a secret nearby point so the formula is exact.
2
The slope term dies because f′(c)=0:
f(c+h)−f(c)=21f′′(ξ)h2.At a flat spot the only thing left to decide "up or down" is the bend.
3
h2≥0 always. If f′′(c)>0, then by continuity f′′(ξ)>0 for all small enough h, so the right side is ≥0: f(c+h)≥f(c) on both sides of c — a valley. If f′′(c)<0 the right side is ≤0 — a hilltop.
∎The sign of h never mattered: a square erases it. That is exactly why the second derivative alone decides. (If f′′(c)=0 the test is silent — the ground is flatter than a parabola and you must look further, as in Unit 8.)
In one sentence: The walk stops on flat ground; the curl of the ground says whether that is a valley, a hilltop, or a saddle; and the valley you get is the one downhill from where you started.
4
The rule, and the trench that makes it zig-zag
A ball on a spring
Pull a ball away from the centre of a bowl and let go: it is pulled back, harder the further you pulled. That is the simplest landscape there is — a bowl — and its tilt is a spring: the pull is proportional to how far you are from the bottom. Now make the bowl a trench: a stiff spring across the trench, a soft spring along it. The ball is pulled back hard across, gently along. Every step of gradient descent on a bowl is a ball obeying two springs of different strength. Watch what that does to the path:
The descent stageGradient descent on a quadratic valley, in 3D — fixed steps zig-zag, exact line search turns square corners. Drag to orbit.
Try: on the lecture's valley set γ=0.09 and press ▶: the walker bounces across the steep walls (factor −0.81) while inching along the floor (factor 0.82). Then switch to exact line search and watch every step turn exactly 90°.
drag the picture to orbit
—
For f(x)=21x⊤Ax+b⊤x the gradient is Ax+b and the minimiser is x∗=−A−1b. Along each eigen-direction of A a fixed step multiplies the error by 1−γλ: the stiff direction (big λ) forces γ small, and then the flat direction barely moves. Exact line search picks the γ that minimises f along the ray — which is exactly why consecutive steps are perpendicular.
On the lecture's valley the walk does something that looks wrong and is not: it bounces from wall to wall while creeping along the floor. The stiff spring (across the trench, stiffness about 20) flings the ball to the other side each step; the soft spring (along it, stiffness about 2) barely tugs. One step size has to serve both, and it cannot serve both well. Set γ=0.09 and press ▶: the wall-to-wall bouncing is the stiff spring, the slow crawl is the soft one, and the ratio of the two stiffnesses — ten here, fifty-six on the fitting trench of Section 2 — is how bad the zig-zag gets. This is the picture to keep. Section 5 puts numbers on it; Unit 10's whole reason to exist is to fix it.
Now the rule itself, which you have already used a hundred times in the widgets without reading it:
The realization
xi+1=xi−γ∇f(xi)and for a bowl∇f(x)=Ax+b,
which reads: the tilt of a bowl is a spring — a matrix A of stiffnesses times the displacement, plus a constant offset b that says where the bottom sits (x∗=−A−1b, the point where the springs balance to zero). The eigenvalues of A are the stiffnesses of the springs along the trench's own axes.
Two claims hide inside the rule, and both are things you can see rather than take on trust. First: "against the tilt" really is the steepest way down. Stand on a slope and turn slowly through a full circle: the ground drops fastest when you face exactly away from the uphill needle, and any other facing drops less — the drop is the needle's length times the cosine of the angle you turned. Second: a short enough step against the tilt always lowers the height, because close to your feet the ground is nearly a plane, and on a plane a step downhill goes down. The word "short enough" is doing real work — it is Section 5's whole subject. (The algebra for both, and for the spring formula, waits at the end of the section.)
Pause & predict
On the lecture's valley the walker starts at x0=(−3,−1). The springs pull it with ∇f=Ax0+b. Which way does the first step go, and which coordinate moves more?
If you want the algebra · 3 proofs, step by step
Prove it · the gradient points the steepest way up
Claim. Among all unit directions u, the rate of change of f along u is largest when u points along ∇f, and that largest rate is ∥∇f∥.
1
From Unit 6, the rate of change of f along a unit direction u (the directional derivative) is a dot product:
Duf=∇f⋅u.Walk a tiny distance t along u; f changes by about t∇f⋅u.
2
Cauchy–Schwarz (Unit 3) bounds any dot product by the product of lengths:
∇f⋅u≤∥∇f∥∥u∥=∥∇f∥⋅1.A dot product is ∥a∥∥b∥cosθ, and cosθ≤1.
3
Equality holds exactly when cosθ=1, i.e. u points the same way as ∇f: u=∇f/∥∇f∥. Then Duf=∥∇f∥, the largest possible. Turning u the opposite way gives cosθ=−1 and the most negative rate, −∥∇f∥: the steepest descent.
∎So the uphill compass is not a metaphor. The gradient direction is the unique winner of "which way is steepest", and its length is the steepness.
Prove it · a small step against the gradient lowers f
Claim. If g=∇f(x)=0, then for all small enough γ>0, f(x−γg)<f(x).
1
Unroll the step into one variable, the line trick from Unit 8: h(γ)=f(x−γg). Its derivative at γ=0 is, by the chain rule,
h′(0)=∇f(x)⋅(−g)=−g⋅g=−∥g∥2<0.Moving along −g means the directional derivative is the gradient dotted with −g. A nonzero vector dotted with itself is strictly positive, so with the minus sign the slope of h at the start is strictly negative.
2
Taylor for h at 0 with the remainder:
h(γ)=h(0)+h′(0)γ+21h′′(ξ)γ2=f(x)−γ∥g∥2+21h′′(ξ)γ2.The value, a drop proportional to γ, and a correction proportional to γ2.
3
For small γ the γ term beats the γ2 term: if ∣h′′∣≤M nearby, then
f(x−γg)≤f(x)−γ∥g∥2+21Mγ2=f(x)−γ(∥g∥2−21Mγ),
and the bracket is positive as soon as γ<2∥g∥2/M. So f goes down.
∎Notice what the proof also warns: the guarantee is only for γsmall enough, and "small enough" depends on the bend M. Too big a step and the γ2 term wins — the step overshoots and f can go up. Section 5 turns that warning into an exact number.
Prove it · the gradient of a quadratic bowl is Ax + b
Claim. For symmetric A, f(x)=21x⊤Ax+b⊤x has ∇f=Ax+b, and its only flat point is x∗=−A−1b.
1
Write the two pieces out in coordinates:
21x⊤Ax=21j∑k∑Ajkxjxk,b⊤x=j∑bjxj.A quadratic form is a double sum of "entry times two coordinates"; the linear part is a plain dot product.
2
Differentiate with respect to one coordinate xi. In the double sum, xi appears in the terms with j=i (giving ∑kAikxk) and in the terms with k=i (giving ∑jAjixj); by symmetry these two are equal, so
∂xi∂(21x⊤Ax)∂xi∂(b⊤x)=21(k∑Aikxk+j∑Ajixj)=k∑Aikxk=(Ax)i,=bi.The 21 in front was put there precisely to cancel the 2 that symmetry produces — that is why the lecture writes the bowl as 21x⊤Ax.
3
Stack the coordinates: ∇f=Ax+b. Setting it to zero, Ax=−b, so x∗=−A−1b. For the lecture's numbers, A−1=391[20−1−12] and −b=(5,3), giving
x∗=391[100−3−5+6]=(3997,391)≈(2.487,0.026),f∗≈−6.256.∎So for this valley we know exactly where the walk should end, and can watch it get there — or fail to. Run it:
In one sentence: On a bowl the tilt is a spring, ∇f=Ax+b, and a trench is two springs of unequal stiffness — which is why one step size bounces across it while crawling along it.
5
How long a step — the bouncing ball
Stairs in the dark
Going down a staircase with the lights off, you can shuffle an inch at a time (safe, and you will be there by morning) or leap three steps (fast, until you overshoot a landing and stumble back up a flight). There is a stride that is just right, and it depends on the stairs: steep, tight stairs demand short strides. Gradient descent has exactly this dial, and on a bowl you can see everything it does with a single bouncing ball:
The step-size dialOne number, 1 − γλ, decides everything: crawl, glide, overshoot, bounce forever, or blow up
Try: press sweet — with γλ=1 a single step lands exactly at the bottom. Then press edge: γλ=2 bounces between ±x0 forever, and one notch more explodes.
—
On f=21λx2 the update is xk+1=xk−γλxk=(1−γλ)xk: a pure geometric sequence. The factor must sit strictly inside (−1,1) — that is the rule γ<2/λ. In two dimensions each eigen-direction has its own factor, and the stiffest one (the largest λ) sets the speed limit for all.
Play with the one-dimensional tab and you will discover the whole story in four presets. Crawl: tiny stride, the ball inches down the same side, forever. Sweet: one stride lands it exactly at the bottom. Bounce: a bigger stride overshoots the bottom, lands on the far wall a little lower, overshoots back — a shrinking zig-zag that still gets there. Edge: the ball bounces between the same two heights for ever. One notch more and every bounce is higher than the last: the walk explodes.
There is one number behind all of it. On a bowl of stiffness λ, a step of size γ does not move the ball by a fixed amount — it multiplies its distance from the bottom by the same factor every time:
The realization
distance after the step=(1−γλ)×distance before.
Read the factor like a volume dial. Between 0 and 1: shrinking, same side (crawl when near 1). Exactly 0: one perfect step. Between −1 and 0: shrinking but flipping sides (bounce). At −1: bouncing forever. Beyond −1: growing. So the stride must keep γλ below 2 — the speed limitγ<2/λ. Stiffer bowl, shorter stride, exactly like the stairs.
Now switch to the two-dimensional tab and the zig-zag of Section 4 explains itself. A trench is two springs, so it is two factors at once: 1−γλstiff across and 1−γλsoft along. The stride has to obey the stiff spring's speed limit, and once it does, the soft spring's factor is stuck near 1 — the ball is on the bounce setting across the trench and on the crawl setting along it, at the same time, with the same γ. Press zig-zag: factors −0.90 and 0.81. That pair of numbers is the zig-zag. The ratio of stiffnesses (the condition number) is how badly any single stride is torn between the two.
So what does a practitioner do, not knowing the stiffnesses? Two honest answers. The lazy one: watch the height. If a step made things worse, you were too bold — undo it and halve the stride; if it helped, be a little bolder. You only ever keep steps that go down, so the height can never rise; Section 8 turns this reflex into an algorithm. The clever one: let the ground choose the stride, which is the next section.
Pause & predict
A trench has stiffnesses 20 (across) and 2 (along). You choose γ=0.15. What happens?
Pause & predict
The fitting trench of Section 2 has stiffnesses about 66.8 and 1.2. Roughly what stride is safe, and what does the soft direction do at that stride?
If you want the algebra · 2 proofs, step by step
Prove it · the speed limit γ < 2/λ
Claim. On f=21λx2, gradient descent converges to 0 from any start if and only if 0<γ<2/λ; it lands exactly at 0 in one step when γ=1/λ.
1
Apply the update k times. The factor is the same each time, so
xk=(1−γλ)kx0.A geometric sequence: the position after k steps is the start times the factor to the power k.
2
A number to the power k goes to 0 exactly when its size is below 1:
∣1−γλ∣<1⟺−1<1−γλ<1⟺0<γλ<2⟺0<γ<λ2.Subtract 1 from all three parts, multiply by −1 (which flips the inequalities), divide by λ>0.
3
Read the four regimes off the factor. 0<1−γλ<1 (γ<1/λ): shrinks, same side each time — smooth descent, slow when the factor is near 1. 1−γλ=0 (γ=1/λ): x1=0, done in one step. −1<1−γλ<0 (1/λ<γ<2/λ): shrinks but flips sign — overshoots the bottom every step, still converging. ∣1−γλ∣≥1 (γ≥2/λ): never shrinks; at exactly 2/λ it bounces ±x0 forever, beyond that each step is bigger than the last.
∎Steeper valley (bigger λ) means a lower speed limit 2/λ. That is the dark staircase: steep stairs, short strides.
Prove it · each direction has its own factor — the zig-zag explained
Claim. On f=21(λ1x2+λ2y2) with λ1>λ2>0, a fixed step γ must satisfy γ<2/λ1, and then the y-direction converges with factor 1−γλ2, which is close to 1 when λ1/λ2 is large.
1
The gradient is (λ1x,λ2y), so the two coordinates update independently:
xk+1=(1−γλ1)xk,yk+1=(1−γλ2)yk.The bowl has no cross-term, so each coordinate lives on its own parabola and gets its own factor.
2
Both factors must have size below 1. The stricter demand comes from the larger λ:
γ<λ12(the steep direction sets the limit).If the stiff direction diverges, the whole walk diverges — one bad coordinate is enough.
3
With γ held under that limit, the gentle direction's factor is
1−γλ2>1−λ12λ2.
For the lecture's valley λ1≈20.06,λ2≈1.94, so this factor is above 0.81 no matter what: at best the y-error shrinks by a fifth per step while the x-error, near the limit, flips sign with a factor near −1 — bouncing across the trench while creeping along it.
∎The ratio λ1/λ2 is the condition number. Big ratio, long trench, slow zig-zag walk. For a general bowl 21x⊤Ax the same argument runs in the eigenbasis of A (Unit 4): the eigenvalues of the Hessian are the λ's.
In one sentence: Each step multiplies the distance to the bottom by 1−γλ; the stiffest spring sets the speed limit γ<2/λmax, the softest sets the crawl, and the gap between them is the zig-zag.
6
Let the ground choose the stride — line search
Sliding the foot
Back on the dark staircase, try this instead of guessing a stride: point your foot downhill and slide it forward along the floor, feeling, until the floor stops going down and starts coming up. Put your weight there. You have found the lowest point along that line. Then feel the tilt again — it will point somewhere new — and slide again.
That is line search: fix the direction (against the tilt), and let the landscape tell you how far. Along the ray the whole landscape collapses to a single curve — height against distance — and "how far" is just "where is the bottom of that curve". Drag the bead along the ray and watch the little curve on the right:
The line-search lensSlide a bead down the descent ray, watch h(γ) bottom out, and see why the next gradient is perpendicular to the last step
Try: drag γ slowly and watch the orange gradient arrow at the bead swing round; press snap to γ* and it stands exactly at right angles to the ray, where the ray just kisses a contour. Then take the step and repeat from the new point.
—
Along the ray xk−γg the bowl becomes a one-variable function h(γ)=f(xk−γg). Its derivative is h′(γ)=−g⊤∇f(bead), so h′(γ∗)=0 says precisely that the new gradient has no component along the old direction — the two are perpendicular. For a quadratic, γ∗=g⊤g/g⊤Ag in closed form.
Slide the bead slowly and watch the orange arrow — the tilt at the bead's own position. Early on it leans back along the ray: you are still going down, keep sliding. Slide too far and it leans forward: you have passed the bottom. Exactly at the best spot it is perpendicular to the ray, and at that same spot the ray just kisses a contour line without crossing it. Those are the same fact seen twice: a contour is a line of constant height, the tilt is always perpendicular to contours, and the lowest point along a straight line is where the line touches a contour rather than cutting through to a lower one.
The realization
The best stride γ∗ is where the tilt at the new point is at right angles to the direction you just walked. On a bowl it has a closed form, γ∗=g⊤Agg⊤g with g the tilt where you stand — the stride you would get by sliding your foot, written as a fraction. On the stretched bowl from (2,2) it is 5/28, and one slide removes 89% of the height.
Press take the step a few times and a pattern appears that no fixed stride ever shows: every corner is a right angle. The walk turns exactly 90∘ at each step, because each new tilt is perpendicular to the last direction. Beautiful — and still a zig-zag on a long trench, just a tidier one: each step is the best possible in its direction, but the direction is still "straight against the tilt", which on a trench is mostly "across". Better strides do not fix a bad direction; that is Unit 10's job.
Is it used? Less than you would think. Sliding your foot costs a whole one-variable minimisation per step, and each height check on a real loss means a pass over the data. So plain gradient descent settles for an inexact slide — "far enough that the height clearly dropped" (the Armijo rule of Section 7) — and saves the exact version for methods like Newton's that genuinely need it.
Pause & predict
From (2,2) on the stretched bowl the exact slide lands at (9/7,−1/7), where the tilt is (18/7,−6/7). Dot it with the old tilt (4,12). What do you get, and why?
If you want the algebra · 3 proofs, step by step
Prove it · the lecture's line search on x² + 3y²
Claim. For f=x2+3y2 the best step from (x,y) is γ∗=2x2+54y2x2+9y2; from (2,2) it is 5/28, landing at (9/7,−1/7).
1
The gradient is ∇f=(2x,6y). The trial point a distance γ down the ray is
(x−2γx,y−6γy)=((1−2γ)x,(1−6γ)y).Each coordinate keeps its own factor, exactly as in Section 5 — but now γ is a variable we will choose.
2
Put the trial point into f:
h(γ)=(1−2γ)2x2+3(1−6γ)2y2.A quadratic in γ with a positive leading coefficient — a parabola in γ with a single bottom.
3
Differentiate with respect to γ (chain rule: inner derivatives −2 and −6) and set to zero:
h′(γ)=−4(1−2γ)x2−36(1−6γ)y2=0.2(1−2γ)⋅(−2)=−4(1−2γ) and 3⋅2(1−6γ)⋅(−6)=−36(1−6γ).
4
Expand, collect the γ terms, solve:
−4x2+8γx2−36y2+216γy2γ(8x2+216y2)γ∗=0=4x2+36y2=8x2+216y24x2+36y2=2x2+54y2x2+9y2.Divide top and bottom by 4. It is a minimum because h′′=8x2+216y2>0.
5
At (2,2): γ∗=8+2164+36=22440=285. The new point is
((1−2810)⋅2,(1−2830)⋅2)=(2818⋅2,−282⋅2)=(79,−71),
and f drops from 16 to 4981+493=712≈1.71.
∎One landscape-chosen step removed 89% of the height. Notice y overshot past 0 to −1/7: even the best step along the gradient overshoots in the stiff direction. The next step's γ∗ is 5/12, landing at (3/14,3/14).
Prove it · the general line-search step for a quadratic
Claim. For f=21x⊤Ax+b⊤x with A symmetric positive-definite and g=∇f(x)=Ax+b, the best step along −g is γ∗=g⊤Agg⊤g.
1
h(γ)=f(x−γg). By the chain rule, h′(γ)=∇f(x−γg)⋅(−g).
Derivative of f along a moving point = gradient at that point dotted with the point's velocity, which here is −g.
2
The gradient at the trial point is A(x−γg)+b=(Ax+b)−γAg=g−γAg. So
h′(γ)=−g⊤(g−γAg)=−g⊤g+γg⊤Ag.The gradient of a quadratic is linear in the point, so it splits cleanly into "the old gradient" minus "γ times Ag".
3
Set h′(γ)=0:
γ∗=g⊤Agg⊤g,
a minimum because h′′=g⊤Ag>0 for positive-definite A.
∎Check it against the lecture: for x2+3y2, A=diag(2,6), g=(2x,6y), so g⊤g=4x2+36y2 and g⊤Ag=8x2+216y2 — the same fraction as before. Practice Problems 6, 7 and 9 use this formula.
Prove it · after an exact step, the new gradient is perpendicular to the old direction
Claim. If αt minimises J(wt+αgt) over α, then gt⊤∇J(wt+1)=0, where wt+1=wt+αtgt.
1
Suppose not: gt⊤∇J(wt+1)=0. Move a tiny extra ±δ along the same direction and use first-order Taylor at wt+1:
J(wt+1±δgt)≈J(wt+1)±δgt⊤∇J(wt+1).Value plus (step) dot (gradient). This is exactly the line trick from Unit 8, one derivative deep.
2
The correction ±δgt⊤∇J is nonzero, and we get to pick its sign: choose +δ if the dot product is negative, −δ if it is positive. Either way,
J(wt+1±δgt)<J(wt+1).A nonzero slope along the line means one of the two ways along the line goes down.
3
But that is a point on the same ray — at α=αt±δ — with a lower value of J, contradicting that αt was the minimiser. So the dot product must be zero: gt⊥∇J(wt+1).
∎In one-variable language: h′(αt)=gt⊤∇J(wt+1), and the minimiser of a smooth function has zero derivative. Geometrically: the ray stops exactly where it is tangent to a contour, and the gradient is always perpendicular to contours. Consequence: steepest descent with exact line search turns a right angle at every corner — a zig-zag of perfect squares, which on a long trench is still a lot of corners.
In one sentence: Slide along the ray until the ground stops dropping — that is where the ray kisses a contour and the new tilt is at a right angle to the step — and on a bowl the stride is the fraction g⊤g/g⊤Ag.
7
Finding the bottom of one line — the radio dial
The radio dial
An old radio: somewhere on the dial the station comes in clearest, but you cannot see signal strength, only hear hiss. So you play a game. Pick a spot, nudge the knob a hair to the right, and ask: better or worse? Better means the sweet spot is further right, worse means it is to the left. Each answer throws away half the dial. Ten answers and you are within a thousandth of the station — without ever knowing where it was.
Section 6 left one job open: on a real loss there is no fraction for the best stride, only the ability to ask the ground its height at any distance along the ray. That is the radio. The curve of height-against-distance usually has one dip — down, bottom, up, no side dips — and a curve like that can be cornered by squeezing a bracket around its bottom. Three ways to squeeze, and one way to cheat:
The bracket squeezerThree ways to pick a step along a line: halve the bracket, cut it by the golden ratio, or back off until the drop is good enough
Try: step the binary search on the practice function and read the brackets: [0,4]→[2,4]→[2,3]→[2,2.5]. Then switch to the bumpy function — the bracket can squeeze down on the wrong dip and lose the true minimiser.
—
Bracketing methods assume h is unimodal on [a,b] — one dip, nothing else — and then squeeze: binary search halves the width each time (two evaluations per step), golden section shrinks it by 0.618 with only one new evaluation. Armijo asks a humbler question: is h(α)≤h(0)+cαh′(0)? If not, halve α and ask again.
Binary is the radio game exactly: check the slope at the middle of the bracket (compare the height at the midpoint with the height a hair to the right), keep the half that goes down. Every round halves the bracket, so 9 rounds take [0,4] to a width under 0.01 — you can count the rounds with a logarithm and nothing else. Golden-section is the same game for someone who cannot measure slope, only height: put two probes inside the bracket, keep the side with the lower one. Its trick is where to put the probes — at 38% and 62% of the bracket — because after the cut, one old probe is already sitting at the right fraction of the new bracket and can be reused. One new height per round instead of two; that is the whole reason the golden ratio shows up. Armijo gives up on finding the bottom. It starts with a bold stride and asks only "did the height drop by a decent fraction of what the tilt promised?" — if yes, take it; if no, halve and ask again. It does not need the curve to have one dip, it costs a handful of height checks, and it is what ordinary gradient descent actually uses. The exact squeezes are for the methods that truly need the bottom.
The realization
Bracket width after k halvings is (b−a)/2k; golden cuts shrink it by 0.618 per round and reuse a probe; Armijo accepts any stride with h(α)≤h(0)+cαh′(0) for a small c — "a decent fraction of the promised drop".
Pause & predict
Binary search over [0,4]: at the midpoint 2 the height a hair to the right is lower than at 2. Which half do you keep, and how wide is the bracket after three such rounds?
If you want the algebra · 1 proof, step by step
Prove it · how many squeezes, and why the golden ratio
Claim. Binary search reaches a bracket of width ≤w from [a,b] in k=⌈log2((b−a)/w)⌉ rounds; golden-section search shrinks the bracket by φ=(5−1)/2≈0.618 per round and re-uses one point each time.
1
Binary: each round halves the width, so after k rounds the width is (b−a)/2k. Demanding (b−a)/2k≤w:
2k≥wb−a⟺k≥log2wb−a.
For [0,4] and w=0.01: log2400≈8.64, so k=9 (width 4/512≈0.0078).
Take the logarithm base 2 of both sides; round up because k counts whole rounds. This is Practice Problem 12.
2
Golden: put m1=b−φ(b−a) and m2=a+φ(b−a). Suppose we keep [a,m2], whose width is φ(b−a). Where does the old m1 sit inside it, as a fraction from the left?
m2−am1−a=φ(b−a)(1−φ)(b−a)=φ1−φ.We want this to equal 1−φ, the left golden fraction, so that m1 can serve as the new bracket's left interior point without a fresh evaluation.
3
Demand φ1−φ=1−φ, i.e. 1−φ=φ−φ2, i.e. φ2+φ−1=0, whose positive root is
φ=2−1+5≈0.618.∎So the golden ratio is not decoration — it is the unique fraction that lets each round re-use a point. Width after k rounds: (b−a)φk; for [0,4] and w=0.01, k=⌈ln(0.0025)/ln0.618⌉=13 rounds, but only 13 new evaluations against binary search's 18 (two per round). Fewer evaluations per unit of squeeze — that is the whole point when each h costs a pass over the data.
In one sentence: With no formula for the bottom of the ray, squeeze a bracket around it — halve it, golden-cut it, or take Armijo's "good enough" stride, which is what real training does.
8
Bold early, careful late — schedules and the bold driver
Parking the car
Nobody parks at one speed. Far from the wall you drive fast, because the wall is far; as it comes near you slow to a crawl, because a fast car near a wall overshoots. A learning rate is a speed. Section 5's bouncing ball was the car at one fixed speed — fine early, bouncing off the walls later. The cure is obvious the moment you say it: start bold, finish careful.
Three ways to slow down on a timer, and one way to slow down by feel. Compare the timers, then watch the driver find the speed limit on its own:
Schedules and the bold driverThree ways to shrink the learning rate on a clock, and one way to let the loss itself drive
Try: set k=0.05 and read the three rates at t=10: 0.485, 0.533, 0.089. Then open the bold driver tab and press ▶ — the rate climbs 5 % a step until the first overshoot, then halves on the spot.
—
Exponential decay α0e−kt shrinks geometrically, inverse decay α0/(1+kt) only harmonically, step decay α0/3⌊t/5⌋ drops in cliffs. The bold driver ignores the clock: after every step it looks at the loss — improved? grow α by 5 %; worse? undo the step and halve α.
The timers: exponential loses the same fraction every epoch (fast to fall — after 1/k epochs it is at 37%); inverse loses less and less (half at 1/k, a third at 2/k, a slow tail); step holds steady and then drops off a cliff every few epochs. Which is right depends on how far the walk still has to go — a rate that dies too fast leaves the walker stranded above the bottom; one that dies too slowly keeps it jittering. Section 11 will show why, with noisy tilts, the slowing down is not a nicety but the only way the walk ever ends.
The bold driver replaces the timer with a reflex. After each step, look at the height. Lower than before? Press the accelerator a touch — rate up 5%. Higher? You overshot: undo the step, halve the rate, try again from where you were. Kept steps only ever go down, so the height can never rise; and the rate drifts up until it hits the speed limit of Section 5, gets knocked back, drifts up again. In the widget it hovers around 2/λstiff=0.1 — it has discovered the stiffest spring by bumping into it. One honest caveat, which points at Section 10: if the height you look at is only an estimate (a spoonful of the data, not the pot), it can go up by chance, and the driver panics for no reason. Then judge every few steps, not every step.
The realization
αt=α0e−kt (exponential), αt=α0/(1+kt) (inverse), divide-by-a-constant-every-few-epochs (step); t counts epochs, so the rate is fixed within one pass over the data. Bold driver: ×1.05 on success, undo and ×0.5 on failure.
Pause & predict
Training starts at α0=0.8. Under step decay that divides the rate by 3 every 5 epochs, what is the rate during epoch 12?
Pause & predict
A bold driver is at rate 0.08. The next step makes the height go up. What does it do?
If you want the algebra · 1 proof, step by step
Prove it · the two half-way marks
Claim. Inverse decay halves the learning rate exactly at t=1/k; exponential decay multiplies it by 1/e≈0.37 at the same t.
1
Inverse: set 1+ktα0=2α0. Cross-multiplying,
1+kt=2⟺t=k1.For k=0.05 that is epoch 20: 0.8/(1+1)=0.4. Practice Problem 3.
2
Exponential at the same t: α0e−k⋅(1/k)=α0e−1≈0.37α0. Already below half — and at t=2/k it is e−2≈0.14 against inverse decay's 1/3.
∎Exponential decay is geometric (a fixed fraction lost per epoch), inverse decay is harmonic (it loses less and less). Which is right depends on how far the walk still has to go: a rate that dies too fast leaves the walker stranded short of the bottom, one that dies too slowly keeps it jittering. Section 11 shows the second danger with noisy gradients, where decay is not a convenience but a requirement.
In one sentence: A learning rate is a speed that should fall as the wall approaches — on a timer (exponential, inverse, step) or by feel (bold driver: up 5% on success, undo and halve on failure).
9
Is my compass even right? — the known weight
The known weight
Before an inspector trusts a shopkeeper's scale, she puts a known one-kilogram weight on it. If it reads 1.0, fine. If it reads 1.3, every sale that day was wrong and nobody noticed. A gradient computed by code — hand-written derivatives, or backprop from Unit 7 — is that scale. It will confidently steer a million-knob walk, and if it is off by a sign or a factor, the walk goes confidently to the wrong place. So you put a known weight on it.
The known weight is the meaning of "tilt" itself: nudge one knob by a tiny Δ, see how much the height moves, divide. That is the tilt, measured rather than computed, and it should match the code. Two ways to nudge — forward only, or a little each way — and they are not equally good. Slide Δ and watch the two error curves:
The gradient auditorCheck a hand-derived gradient against finite differences — and see where round-off wrecks the check
Try: at Δ=0.1 read the two estimates of ∂J/∂w1: forward 13.61, central 13.01, truth 13. Halve Δ and the forward error halves while the central error drops fourfold — then slide below 10−6 and watch both curves turn back up.
—
J(w1,w2)=w13+w1w22 at (2,1). Taylor says the forward difference is off by 21ΔJ′′ and the central one by 61Δ2J′′′ — the slopes 1 and 2 on the log-log chart. But each J carries a rounding error of about 10−16, and dividing that by Δ makes it explode as Δ→0: the sweet spot is around 10−5, not zero.
The picture has three regions, and each teaches something. On the right (large Δ) both estimates are poor — a wide secant is nowhere near the tangent. In the middle, the two curves are straight lines with different slopes: shrink Δ tenfold and the forward error drops tenfold, but the central error drops a hundredfold. Nudging both ways cancels the ground's curl (the second-derivative term) that the one-sided nudge leaves behind — Unit 8's Taylor lines say exactly which terms survive. On the left (tiny Δ) both curves turn back up: the height moves by less than the computer's sixteen digits can tell, the true digits cancel, and what is left is rounding noise divided by a tiny number. The sweet spot is a moderately small Δ, and the audit is a spot check of a few knobs — never the way you compute gradients for real.
The realization
Forward: ΔJ(w+Δ)−J(w), error ≈21J′′Δ. Central: 2ΔJ(w+Δ)−J(w−Δ), error ≈61J′′′Δ2. On J=w13+w1w22 at (2,1) with Δ=0.1: true 13, forward 13.61, central 13.01.
Pause & predict
With Δ=0.1 the central estimate of a tilt is off by 0.01. You shrink Δ to 0.01. Roughly what is the new error?
If you want the algebra · 1 proof, step by step
Prove it · forward error ∝ Δ, central error ∝ Δ²
Claim. For a smooth one-variable J, the forward difference errs by about 21J′′Δ and the central difference by about 61J′′′Δ2.
1
Taylor at w, one step forward and one step back:
J(w+Δ)J(w−Δ)=J+J′Δ+21J′′Δ2+61J′′′Δ3+⋯=J−J′Δ+21J′′Δ2−61J′′′Δ3+⋯Same series, with Δ replaced by −Δ: odd powers flip sign, even powers do not.
2
Forward: subtract J and divide by Δ:
ΔJ(w+Δ)−J(w)error=J′+21J′′Δ+⋯≈21J′′Δ.The leading leftover is the bend term; it shrinks only as fast as Δ.
3
Central: subtract the backward series from the forward one — every even-power term cancels — and divide by 2Δ:
2ΔJ(w+Δ)−J(w−Δ)=2Δ2J′Δ+31J′′′Δ3+⋯=J′+61J′′′Δ2+⋯∎The bend term is gone; the error is Δ2-sized. Halve Δ and the forward error halves but the central error quarters. On the widget's example, J=w13+w1w22 at (2,1) with Δ=0.1: forward 13.61 (error 0.61≈21⋅12⋅0.1), central 13.01 (error 0.01=61⋅6⋅0.01). Practice Problem 8.
In one sentence: Audit a coded gradient by nudging a few knobs; nudging both ways is a hundred times better for a tenfold smaller nudge, until rounding drowns everything.
10
Three ways to walk — batch, minibatch, stochastic
One spoon, not the pot
A cook checking whether the soup needs salt does not drink the whole pot. One spoonful, stirred from anywhere, is enough to decide "a pinch more". She adds it, stirs, tastes another spoon. Fifty small, slightly-wrong decisions get the soup right faster than one perfect measurement that requires drinking the pot. A real loss is the pot: the wrongness is a sum over every example, so the tilt costs one pass over all the data — every single step. The spoonful is the whole idea of this section.
There are three walkers, and they differ in exactly one thing: how much of the data they read before each step. Batch reads everything, so its tilt is exact. Minibatch reads a handful — a spoonful — and steps on the tilt of that handful. Stochastic reads a single example and steps on that. Put all three on the same bowl, same start, same stride, and race them. Then change what "fair" means:
Three ways to walk: batch, minibatch, stochasticSame loss bowl, same start, same step size — one walker reads all 40 points before every step, one reads a handful, one reads a single point
Try: press ▶ in the fair per step view and batch looks best. Switch to fair per data read and press ▶ again: while batch is still reading its first 40 points, the stochastic walker has already sprinted to the bottom — then jitters there until you switch on decay.
drag the picture to orbit
—
A full gradient costs all N residuals; a minibatch of ∣S∣ costs ∣S∣, with the same expected direction and noise shrinking like 1/∣S∣. Judged per step, batch wins. Judged per point of data read — the only budget that matters when N is millions — the noisy walkers are far ahead, and a decaying step size lets them settle at the end.
The race has two verdicts, and both are right. Judged per step, batch looks best: smooth, straight, no wobble; the other two jitter. Judged per point of data read — the only budget that matters when the data is millions of examples — the picture flips completely. By the time batch has read its first forty points and taken one step, the stochastic walker has taken forty and is already most of the way down. The loss chart on the right is the whole lesson: against data read, the noisy walkers plunge and the exact one ambles. That is not a trick of this example. It is why every large model you have heard of was trained on minibatches.
Why can a spoonful steer at all? Because it is right on average. Each example's tilt points a little differently, but the true tilt is their average, so a random handful's tilt scatters around the true one — not to one side of it. Over many steps the scatter cancels and the drift is exactly the batch walker's direction. And the scatter shrinks as the spoon grows: four times the batch, half the noise. Bigger spoon, calmer walk, dearer step. Minibatch is the dial between the two extremes, and in practice it is set by the hardware (whatever fits in memory at once) as much as by the maths.
Batch
Minibatch (|S| points)
Stochastic
reads before each step
all N examples
a random handful, ∣S∣
one random example
cost of one step
N
∣S∣
1
tilt used
exact
right on average, noise ∝1/∣S∣
right on average, noisiest
the path
smooth, slow per data read
slightly wobbly, fast
jittery, fastest early, never settles at fixed stride
the bottom
reaches it exactly
hovers within a small cloud; settles with a decaying stride
hovers in a bigger cloud; settles with a decaying stride
use it when
data is small, or the exact tilt matters
almost always — this is deep learning's default
streaming data, one example at a time
The realization
The loss is a sum, L=∑nLn, so the tilt is a sum of per-example tilts. A random handful S gives ∇J(S)=∑i∈S∇Li, and scaled by N/∣S∣ its average is exactly ∇L — unbiased. Its noise falls like 1/∣S∣. Step on the handful's tilt: w←w−α∇J(S). That is minibatch gradient descent; ∣S∣=1 is stochastic gradient descent; ∣S∣=N is what we did all unit.
Pause & predict
You have a million examples. One batch step reads all of them. With minibatches of 100, how many steps do you get for the same reading, and how good are they?
Pause & predict
In the race, switch from fair per step to fair per data read. Why does the ranking flip?
In one sentence: Batch, minibatch and stochastic differ only in how much data they read before each step; a random spoonful's tilt is right on average with noise ∝1/∣S∣, so per point of data read the noisy walkers win — which is why deep learning trains on minibatches.
11
Noise, tamed — and noise as a friend
The nervous hand
Try to place a coin exactly on a mark while your hand trembles. Big, confident movements get you close fast — and then the tremble keeps knocking the coin off the mark. The fix every steady-handed person uses without thinking: make the final movements small, so the tremble, which is a fraction of each movement, becomes a fraction of something tiny. A noisy tilt is a trembling hand. The stride is the size of the movement.
Watch one noisy walker up close, with the true tilt and the spoonful's tilt drawn at every step, then switch on a decaying stride:
The noisy descentEstimate the gradient from a handful of points instead of all 40 — cheaper, noisier, and still it finds the bottom
Try: set the batch to 1 and press ▶: the walk jitters around the bottom and never settles. Switch to the decay tab — the same noisy steps, shrinking with t, calm down onto the minimiser.
—
The full gradient averages a residual term over all N=40 points; a minibatch averages the same term over ∣S∣ of them. The estimate is unbiased — on average it is the true gradient — with variance shrinking like 1/∣S∣. Each step costs ∣S∣ residuals instead of N, so a batch of 4 buys ten steps for the price of one exact one.
With a fixed stride the walker never stops. Near the bottom the true tilt is nearly zero, but the spoonful keeps saying "a bit this way", "a bit that way", and the walker jitters in a small cloud around the bottom. The cloud is not a bug in the example; it is what a trembling hand does at any fixed movement size, and its size is proportional to the stride — halve γ, halve the cloud. So Section 8's slowing down is not a refinement here: it is the only way the walk ever ends. Shrink the stride over time (fast enough that the cloud closes, slowly enough that the walker still gets wherever it needs to go) and the jitter dies onto the bottom. That is the content of the theorem the lecture quotes — with a suitably decaying rate, stochastic descent settles into a local minimum almost surely — and the widget's decay tab is the theorem happening.
One last turn of the story, and it flips the villain. Remember the two valleys of Section 1 and the shallow dip of Section 3: an exact walker that rolls into a shallow dip is stuck there for ever, because every exact step goes down. A noisy walker is not. Its trembling steps sometimes go up, and on a bumpy landscape a few upward stumbles are enough to hop out of a shallow dip and find a deeper valley. The fog and the noise, the two things that made the walk hard, turn out to be the two things that make it work at scale — and the second one is even, sometimes, the thing that finds a better answer.
The realization
On a bowl of stiffness λ with tilt noise of size σ, a fixed stride γ leaves the walker in a cloud of variance ≈γσ2/2λ — proportional to γ. A schedule with ∑γt=∞ and ∑γt2<∞ (for instance γ0/(1+kt)) closes the cloud without stranding the walker.
Pause & predict
You run the single-example walker with a fixed stride. The loss falls fast, then stops improving and wobbles slightly above the minimum. What is going on, and what fixes it?
Pause & predict
Two walkers start in the same shallow dip of a bumpy landscape: one uses exact tilts, one uses single-example tilts. Which can end up in the deeper valley next door, and why?
If you want the algebra · 3 proofs, step by step
Prove it · the minibatch gradient is unbiased
Claim. If S is a random sample of m indices out of N (each index equally likely to be included), then the scaled sample gradient mN∑i∈S∇Li has expected value exactly ∇L.
1
Write the sample sum with an indicator for each example: 1n=1 if n∈S, else 0. Then
i∈S∑∇Li=n=1∑N1n∇Ln.Summing over "the chosen ones" is the same as summing over everyone with a switch that is on only for the chosen.
2
Take expectations. The gradients ∇Ln are fixed numbers; only the switches are random, and each is on with probability m/N:
E[n∑1n∇Ln]=n∑E[1n]∇Ln=n∑Nm∇Ln=Nm∇L.Expectation of a sum is the sum of expectations; the expectation of a 0/1 switch is the probability it is on. Every example is in the sample with the same chance, m/N.
3
Multiply by N/m: E[mN∑i∈S∇Li]=∇L.
∎"Unbiased" means: on average, over many spoonfuls, the estimate is exactly the true gradient. It does not mean any single spoonful is right — a single one can point well off. The scale factor N/m is absorbed into the learning rate in practice, which is why the lecture's update simply uses ∇J(S).
Prove it · bigger spoon, less noise — variance falls like 1/|S|
Claim. If the per-example gradients are drawn independently with variance σ2 (per component), the average of m of them has variance σ2/m.
1
Let g1,…,gm be the sampled gradients (one component) and gˉ=m1∑igi their average. Pulling the constant out of a variance squares it:
Var(gˉ)=m21Var(i∑gi).Var(cX)=c2Var(X).
2
For independent draws the variance of a sum is the sum of variances:
Var(i∑gi)=i∑σ2=mσ2.Independence kills the cross-terms; each draw contributes σ2.
3
So Var(gˉ)=m2mσ2=mσ2, and the typical size of the noise (the standard deviation) is σ/m.
∎Four times the batch, half the noise. That is the lecture's last line — large minibatches give lower variance — and the trade it names: a bigger spoon is a better estimate but a dearer step. In the widget, ∣S∣=4 out of 40 buys ten steps for the price of one exact one, with half the noise of a single example.
Prove it · a constant step leaves a jitter cloud proportional to γ
Claim. On f=21λx2 with a noisy gradient λxk+εk (noise of mean 0, variance σ2, independent step to step) and a constant γ<2/λ, the iterates settle into a cloud around 0 of variance λ(2−γλ)γσ2≈2λγσ2.
1
The noisy update is
xk+1=xk−γ(λxk+εk)=(1−γλ)xk−γεk.Section 5's factor, plus a random kick of size γεk every step.
2
Square and take expectations. The cross-term vanishes because εk has mean 0 and is independent of xk:
E[xk+12]=(1−γλ)2E[xk2]+γ2σ2.(a−b)2=a2−2ab+b2; the middle term's expectation is 2(1−γλ)γE[xk]E[εk]=0.
3
In the long run the spread stops changing: E[xk+12]=E[xk2]=V. Then
VV(1−(1−γλ)2)V=(1−γλ)2V+γ2σ2=γ2σ2=γλ(2−γλ)γ2σ2=λ(2−γλ)γσ2.∎1−(1−γλ)2=γλ(2−γλ) (difference of squares). For small γ this is ≈γσ2/2λ: the cloud's variance is proportional to γ. Halve the step and you halve the cloud; only γ→0 shrinks it to a point — which is exactly what a decaying schedule does.
In one sentence: A noisy tilt is a trembling hand — a fixed stride leaves a jitter cloud proportional to γ, a decaying stride closes it, and the same tremble is what lets a walker hop out of a shallow dip.
12
What to carry forward
One reflex — feel the tilt, step the other way — and every section was a picture of one question about that step.
The picture
What it says
The line of math it becomes
The fog
you only ever need the tilt under your feet — and you find the valley downhill from where you start
x←x−γ∇f
The four receipts
say what "wrong" means and a landscape appears; its tilt is "which way should the line move"
∇L=(−2∑xiri,−2∑ri)
The marble
flat ground comes as valley, hilltop, saddle — the curl decides
f′=0, sign of f′′ (the Hessian in 2-D)
The two springs
a bowl's tilt is a spring; a trench is a stiff and a soft one
∇f=Ax+b, eigenvalues = stiffnesses
The bouncing ball
each step multiplies the distance by a factor; the stiffest spring sets the speed limit
1−γλ, γ<2/λmax
Sliding the foot
stop where the ray kisses a contour; the next tilt is at a right angle
γ∗=g⊤g/g⊤Ag
The radio dial
squeeze a bracket: halve it, golden-cut it, or accept "good enough"
(b−a)/2k, 0.618, Armijo
Parking the car
fast far away, slow near the wall
α0e−kt, α0/(1+kt), step; bold driver
The known weight
test the compass by nudging; two-sided nudges are far better
error ∝Δ vs ∝Δ2
One spoon, not the pot
a random handful's tilt is right on average; per data read, noisy wins
E[∇J(S)]∝∇L, noise ∝1/∣S∣
The trembling hand
a fixed stride leaves a cloud; a shrinking stride closes it; the tremble can hop a lip
cloud ∝γ; ∑γt=∞,∑γt2<∞
The one thing to remember
Feel the slope, step against it, and let the stiffest direction set your stride. The tilt is the only map you get in the fog; a stride under 2/λmax always goes down; a trench makes that stride tiny and the walk a zig-zag; and when the data is too big to read, a random spoonful of it points the right way on average. Everything the next units add — momentum, Newton, constraints — is a way of walking the same fog more cleverly.
13
Practice arena — twelve problems, solved in full
Twelve problems, easy to hard, covering the whole walk: classifying flat spots, tracing gradient-descent steps by hand, learning-rate schedules, the line-search stride derived and applied, gradient checking, a coupled bowl, training a straight line by descent, and a bracketing trace. Every number below is machine-verified.
Two habits do most of the work: trace steps in a table — point, tilt, new point, height — so nothing is lost in the bookkeeping; and for any stride question, find the stiffest spring first, because 2/λmax is the limit and 1−γλ is the whole story of the approach.
Problem 1easy
Let f(x)=2x3−9x2+12x+5. (a) Find every stationary point of f. (b) Classify each one with the second-derivative test and give the value of f there. (c) Does f have a global minimum over the whole real line? Say why.
What this tests. The one-variable routine that every descent method is built on — set the slope to zero, read the curvature, and then ask the question people skip: what happens far away. Plan. Differentiate and factor (try small integers first); evaluate f′′ at each root; compute the values; finish by looking at x→±∞ before saying anything about "global".
Show the full solution
(a) Step 1 — set the slope to zero. Differentiate term by term and pull out the common factor:
f′(x)=6x2−18x+12=6(x2−3x+2)=6(x−1)(x−2).
So f′(x)=0 exactly at x=1 and x=2. These are the two stationary points.
(b) Step 2 — read the curvature. The second derivative is f′′(x)=12x−18:
f′′(1)f′′(2)=12−18=−6<0=24−18=+6>0⇒local maximum at x=1,⇒local minimum at x=2.
Step 3 — the values. Substitute each point into f, one term at a time:
f(1)f(2)=2(1)−9(1)+12(1)+5=2−9+12+5=10,=2(8)−9(4)+12(2)+5=16−36+24+5=9.
The local maximum is 10 at x=1; the local minimum is 9 at x=2. Notice the hill top is only 1 higher than the valley floor — a gentle wobble in the middle of a steep cubic.
(c) Step 4 — look far away. The leading term 2x3 has odd degree, so as x→−∞ the function runs off to −∞. For instance f(−10)=−2000−900−120+5=−3015, far below 9. A function that goes below every level has no global minimum; x=2 is a local minimum only.
(a) x=1 and x=2. (b) x=1: local maximum, f=10 (f′′=−6); x=2: local minimum, f=9 (f′′=+6). (c) No — a cubic is unbounded below, so there is no global minimum.
Remember
"Slope zero" finds candidates; "curvature sign" sorts them; only a look at ±∞ earns the word global. The usual slip on this type is stopping after step 2 and calling x=2 the minimum of f, when a cubic has no floor at all — and gradient descent started far to the left would simply run away forever.
Problem 2easy
Apply gradient descent to f(x,y)=x2+2y2 with step size γ=0.1, starting at (x0,y0)=(4,2). (a) Write the update equations for x and y. (b) Carry out two iterations, reporting the point and the value of f after each. (c) Check that f went down at every step.
What this tests. The update rule itself on a bowl whose two coordinates do not talk to each other — and the discipline of keeping a table. Plan. Compute the gradient symbolically first, so each coordinate's update becomes "multiply by a constant"; then fill a row per iteration: point, gradient, new point, f; finally read the f column top to bottom.
Show the full solution
(a) Step 1 — the update rule, coordinate by coordinate. The gradient is ∇f=(2x,4y). The update rule xk+1=xk−γ∇f(xk) then reads
xk+1yk+1=xk−0.1(2xk)=xk−0.2xk=0.8xk,=yk−0.1(4yk)=yk−0.4yk=0.6yk.
Each coordinate simply shrinks by its own fixed factor, 1−γ×(its curvature): 1−0.1⋅2=0.8 for x and 1−0.1⋅4=0.6 for y.
(b) Step 2 — iterate in a table.
k
point (xk,yk)
gradient (2xk,4yk)
new point
f(xk,yk)
0
(4,2)
(8,8)
(4,2)−0.1(8,8)=(3.2,1.2)
16+2⋅4=16+8=24
1
(3.2,1.2)
(6.4,4.8)
(3.2,1.2)−0.1(6.4,4.8)=(2.56,0.72)
10.24+2⋅1.44=10.24+2.88=13.12
2
(2.56,0.72)
—
—
6.5536+2⋅0.5184=6.5536+1.0368=7.5904
The shortcut from part (a) gives the same points with no gradient at all: 0.8⋅4=3.2, 0.6⋅2=1.2; then 0.8⋅3.2=2.56, 0.6⋅1.2=0.72.
(c) Step 3 — read the f column.24>13.12>7.5904.
The value fell at both steps. Both coordinates are heading for the minimiser (0,0), and y gets there faster because its factor 0.6 is smaller than 0.8 — the steeper direction is the quicker one when the step size is safely small.
(a) xk+1=0.8xk, yk+1=0.6yk. (b) (3.2,1.2) with f=13.12; then (2.56,0.72) with f=7.5904. (c) 24>13.12>7.5904: strictly decreasing.
Remember
Always work in a table — point, gradient, new point, f — so no arithmetic goes missing between rows. On a separable bowl each coordinate shrinks by its own factor 1−γλ; this is the same number that decides stability in Problem 11. The common slip is subtracting the gradient without the γ, or evaluating f at the old point instead of the new one.
Problem 3easy
Training starts with learning rate α0=0.8 and decay constant k=0.05. (a) Compute αt at t=10 and t=20 under exponential decay αt=α0e−kt and under inverse decay αt=α0/(1+kt). (b) Under step decay that divides α by 3 every 5 epochs, what is α during epoch t=12? (c) At which epoch does inverse decay first reach α0/2?
What this tests. The three decay schedules as plug-in formulas, plus one small piece of reasoning about how a schedule reads its counter. Plan. Compute kt once for each t and reuse it in both formulas; for step decay count how many divisions have happened before epoch 12; for (c) set the formula equal to half and solve for t.
Show the full solution
(a) Step 1 — exponential versus inverse. With k=0.05: kt=0.5 at t=10 and kt=1 at t=20.
exponential:α10α20inverse:α10α20=0.8e−0.5=0.8×0.6065=0.4852,=0.8e−1=0.8×0.3679=0.2943;=1+0.50.8=1.50.8=0.5333,=1+10.8=20.8=0.4.
Exponential decay is the more aggressive of the two: geometric shrinking beats the slow 1/t fade of the inverse schedule.
(b) Step 2 — step decay. The rate is divided by 3 at t=5 and again at t=10; the next division is at t=15. So epoch 12 sits in the third block, after two divisions:
α=320.8=90.8≈0.0889.
(c) Step 3 — when inverse decay halves. Set the formula equal to α0/2 and cancel α0:
1+ktα01+ktktt=2α0=2=1=k1=0.051=20epochs.
A pleasing pattern: at the same t=1/k, exponential decay has multiplied the rate by 1/e (that is the 0.2943 from part (a)).
(a) exponential: 0.4852 at t=10, 0.2943 at t=20; inverse: 0.5333 and 0.4. (b) 0.8/9≈0.0889. (c) t=1/k=20.
Remember
In the update wt+1=wt−αt∇J the counter t is in epochs, so α is constant inside an epoch. The one place this type goes wrong is the step schedule — count the divisions that have already happened (two by epoch 12), not the block number.
Problem 4easy
Let f(x,y)=x2+y2−2x−4y+5. (a) Find the exact minimiser (x∗,y∗) and the minimum value. (b) Run two gradient-descent iterations from (0,0) with γ=0.25, reporting f each time.
What this tests. Knowing the destination before you start walking — complete the square, then watch every iterate land closer to it. Plan. Rewrite f as a sum of squares to read off the minimiser (and confirm with ∇f=0); then two table rows; finally compare each f with the previous one and spot the fixed ratio.
Show the full solution
(a) Step 1 — complete the square. Group the x terms and the y terms:
f=(x2−2x)+(y2−4y)+5=(x−1)2−1+(y−2)2−4+5=(x−1)2+(y−2)2.
A sum of two squares is smallest when both squares are zero, so the minimiser is (x∗,y∗)=(1,2) with f∗=0. The same point comes from the gradient: ∇f=(2x−2,2y−4)=(0,0) gives x=1,y=2.
(b) Step 2 — iteration 1. At (0,0) the value is f=1+4=5 and the gradient is
∇f(0,0)=(2⋅0−2,2⋅0−4)=(−2,−4).
Step:
(x1,y1)f(x1,y1)=(0,0)−0.25(−2,−4)=(0.5,1),=(0.5−1)2+(1−2)2=(−0.5)2+(−1)2=0.25+1=1.25.
Step 3 — iteration 2. At (0.5,1):
∇f(0.5,1)(x2,y2)f(x2,y2)=(2⋅0.5−2,2⋅1−4)=(−1,−2),=(0.5,1)−0.25(−1,−2)=(0.75,1.5),=(0.75−1)2+(1.5−2)2=(−0.25)2+(−0.5)2=0.0625+0.25=0.3125.
So f:5→1.25→0.3125, closing in on (1,2).
Step 4 — the pattern behind the numbers. Both curvatures equal 2, so each coordinate's distance to the target is multiplied by 1−γ⋅2=1−0.5=0.5 per step: the point moves exactly halfway to (1,2) every time. Since f is a squared distance, it shrinks by 0.52=41: 5/4=1.25, 1.25/4=0.3125. If you needed f≤0.01, keep dividing by 4: 0.078,0.0195,0.0049 — three more steps, five in total.
(a) (x∗,y∗)=(1,2), f∗=0. (b) (0.5,1) with f=1.25, then (0.75,1.5) with f=0.3125; f is divided by 4 at every step.
Remember
Completing the square first gives you the answer key: every iterate can be checked against the destination, and the ratio 1−γλ tells you how fast you approach it. The slip to watch for here is a sign error when the gradient is negative — subtracting γ×(−2)adds0.5.
Problem 5medium
Let f(x,y)=x3−3x+y2−2y. (a) Find all critical points (where ∇f=0). (b) Classify each one using the Hessian. (c) Evaluate f at each critical point and say which one gradient descent can converge to.
What this tests. The two-variable version of Problem 1, and the fact that "gradient zero" is where descent stops, not necessarily where it should. Plan. The two gradient equations separate, so solve each on its own; the Hessian is diagonal, so its eigenvalues are just the diagonal entries; evaluate, then ask which point a descent path can actually settle at.
Show the full solution
(a) Step 1 — solve ∇f=0.∇f=(3x2−3,2y−2)=(0,0).
The first equation gives 3x2=3, so x=±1; the second gives 2y=2, so y=1. Two critical points:
P1=(1,1),P2=(−1,1).
(b) Step 2 — the Hessian.fxx=6x,fyy=2,fxy=0:
H(x,y)=[6x002].
A diagonal matrix has its eigenvalues on the diagonal, so the classification is immediate:
P1=(1,1):P2=(−1,1):H=[6002],eigenvalues 6,2>0⇒local minimum;H=[−6002],eigenvalues −6,2⇒saddle.
At P2 the surface curves down along x and up along y: a mountain pass.
(c) Step 3 — values and the reachable point.f(1,1)f(−1,1)=1−3+1−2=−3,=−1+3+1−2=1.
Gradient descent from any sensible start heads for the local minimum P1=(1,1). At the saddle P2 the gradient is also zero, so a path that landed exactly there would stop — but the slightest nudge in x (either way: f(−1.1,1)<1 and f(−0.9,1)<1) sends it downhill again. Note also that f has no global minimum: x3−3x→−∞ as x→−∞, so a start far to the left of the saddle slides away forever.
(a) (1,1) and (−1,1). (b) (1,1): eigenvalues 6,2, local minimum; (−1,1): eigenvalues −6,2, saddle. (c) f(1,1)=−3, f(−1,1)=1; descent targets (1,1).
Remember
∇f=0 alone never proves a minimum — saddles satisfy it too, and gradient descent is blind to the difference until a nudge comes along. For a diagonal Hessian the eigenvalues are the diagonal entries: all positive is a bowl, mixed signs is a saddle. The slip on this type is forgetting the negative root x=−1.
Problem 6medium
Let f(x)=21x⊤Ax−b⊤x with
A=[4113],b=[12].(a) Write ∇f and find the exact minimiser x∗ and f(x∗). (b) From x0=(0,0) take one step with fixed γ=0.1 and evaluate f. (c) Show that the exact line-search step from x along −g, where g=∇f(x), is γ∗=g⊤g/g⊤Ag; use it from the same x0 and compare with (b).
What this tests. The quadratic model in matrix language — gradient Ax−b, minimiser from a linear system — and the three-line derivation of the line-search formula. Plan. Solve Ax=b by substitution; use Ax∗=b to shortcut f∗; do the fixed step in pieces (Ax, then the two dot products); derive γ∗ by differentiating h(γ)=f(x−γg); then plug the numbers in and line the three values up.
Show the full solution
(a) Step 1 — gradient and minimiser. For this quadratic, ∇f(x)=Ax−b. Setting it to zero means solving Ax=b:
4x+y=1,x+3y=2.
From the first, y=1−4x. Substitute into the second:
x+3(1−4x)x+3−12x−11xx=2=2=−1=111,y=1−114=117.
So x∗=(111,117)≈(0.091,0.636). Since 4>0 and detA=12−1=11>0, A is positive-definite and this is the unique global minimum.
Step 2 — the minimum value, the short way. Because Ax∗=b, we have x∗⊤Ax∗=x∗⊤b=b⊤x∗, so
f(x∗)=21b⊤x∗−b⊤x∗=−21b⊤x∗=−21(1⋅111+2⋅117)=−21⋅1115=−2215≈−0.682.
(b) Step 3 — one fixed step, γ=0.1. At the origin g0=∇f(0)=A0−b=(−1,−2), so
x1=0−0.1(−1,−2)=(0.1,0.2).
Evaluate f(x1) in pieces:
Ax1x1⊤Ax1b⊤x1f(x1)=(4⋅0.1+0.2,0.1+3⋅0.2)=(0.6,0.7),=0.1⋅0.6+0.2⋅0.7=0.06+0.14=0.2,=1⋅0.1+2⋅0.2=0.1+0.4=0.5,=21(0.2)−0.5=0.1−0.5=−0.4.
(c) Step 4 — derive the line-search formula. Let h(γ)=f(x−γg). By the chain rule, with ∇f(z)=Az−b,
h′(γ)=−g⊤∇f(x−γg)=−g⊤(Ax−γAg−b)=−g⊤(=gAx−b)+γg⊤Ag=−g⊤g+γg⊤Ag.
Setting h′(γ)=0:
γ∗=g⊤Agg⊤g.
It is a true minimum of h, because h′′=g⊤Ag>0 for positive-definite A.
Step 5 — apply it at x0. With g0=(−1,−2):
g0⊤g0Ag0g0⊤Ag0γ∗=1+4=5,=(−4−2,−1−6)=(−6,−7),=(−1)(−6)+(−2)(−7)=6+14=20,=205=0.25.
The line-search point and its value:
x1LSAx1LSx⊤Axb⊤xf(x1LS)=0−0.25(−1,−2)=(0.25,0.5),=(1+0.5,0.25+1.5)=(1.5,1.75),=0.25⋅1.5+0.5⋅1.75=0.375+0.875=1.25,=0.25+1.0=1.25,=21(1.25)−1.25=−0.625.
Step 6 — compare.
after one step
f
fixed γ=0.1
−0.400
exact line search, γ∗=0.25
−0.625
optimum f∗
−0.682
Same direction, better length: one line-search step covers most of the way to the bottom, while the fixed step covers about a third of it.
For a quadratic, the line-search formula γ∗=g⊤g/g⊤Ag comes from three lines — differentiate h, recognise Ax−b as g, set to zero — so re-derive it rather than recall it. Watch the sign convention: with −b⊤x in f the gradient is Ax−b and the optimum solves Ax=b; with +b⊤x both signs flip.
Problem 7medium
Gradient descent is applied to f(x,y)=3x2+y2 from (x0,y0)=(1,3), choosing at each step the γ that minimises h(γ)=f((x,y)−γ∇f(x,y)). (a) Show that h(γ)=3(1−6γ)2x2+(1−2γ)2y2 and derive γ∗=54x2+2y29x2+y2. (b) Evaluate γ∗ at (1,3); compute the new point and the new value of f.
What this tests. Building the one-variable function h by hand and minimising it — the line-search formula derived from scratch on a separable bowl. Plan. Write the trial point with a factor (1−ciγ) on each coordinate, where ci is that coordinate's gradient coefficient; substitute into f; differentiate in γ; solve the linear equation; then put the numbers in.
Show the full solution
(a) Step 1 — build h. The gradient is ∇f=(6x,2y), so the trial point is
(x−γ⋅6x,y−γ⋅2y)=((1−6γ)x,(1−2γ)y).
Substituting into f=3x2+y2:
h(γ)=3(1−6γ)2x2+(1−2γ)2y2.
Step 2 — minimise h. Differentiate with the chain rule (the inner derivatives are −6 and −2):
h′(γ)=3⋅2(1−6γ)(−6)x2+2(1−2γ)(−2)y2=−36x2(1−6γ)−4y2(1−2γ).
Set h′(γ)=0, expand, and collect the γ terms:
−36x2+216γx2−4y2+8γy2γ(216x2+8y2)γ∗=0=36x2+4y2=216x2+8y236x2+4y2=54x2+2y29x2+y2,
dividing top and bottom by 4 in the last line. It is a minimum because h′′(γ)=216x2+8y2>0.
(b) Step 3 — evaluate at (1,3).γ∗=54(1)+2(9)9(1)+9=7218=41.
Step 4 — new point and new value. Use the factors from Step 1 with γ=41:
x1y1=(1−6⋅41)⋅1=(1−23)=−21,=(1−2⋅41)⋅3=21⋅3=23.
Before and after:
f(1,3)f(−21,23)=3+9=12,=3⋅41+49=43+49=412=3.
One optimally sized step removed three quarters of the objective. Notice x overshot past 0 to −21: the stiff direction (curvature 6) was pushed too far by a step that was right for the whole line.
(a) h(γ)=3(1−6γ)2x2+(1−2γ)2y2, γ∗=54x2+2y29x2+y2. (b) γ∗=41 at (1,3); new point (−21,23); f:12→3.
Remember
For any f=ax2+by2 the safe routine is: trial point with factors (1−ciγ), substitute, differentiate, solve the linear equation for γ. Re-derive it every time — a memorised formula breaks the moment the coefficients change. The slip to avoid is dropping the inner derivative (−6) or (−2) when differentiating the squares.
Problem 8medium
Your code computes gradients of J(w1,w2)=w13+w1w22, and you audit it at (2,1) with Δ=0.1. (a) Compute the analytic gradient at (2,1). (b) Estimate both partials with the forward difference (J(…,wi+Δ,…)−J(…))/Δ and report the absolute errors. (c) Re-estimate ∂J/∂w1 with the central difference and compare the orders of the two errors.
What this tests. The finite-difference audit — the cheap check that catches a wrong gradient before it silently corrupts every descent step — and why the symmetric version is so much better. Plan. Differentiate by hand first; compute the three needed values of J exactly; form the two quotients; then explain the error sizes with the second and third derivatives.
Show the full solution
(a) Step 1 — the analytic gradient.∂w1∂J∂w2∂J=3w12+w22=2w1w2⇒3(4)+1=13at (2,1),⇒2(2)(1)=4at (2,1).
So ∇J(2,1)=(13,4).
(b) Step 2 — forward differences. The base value is J(2,1)=8+2=10.
For w1, nudge to 2.1:
J(2.1,1)0.1J(2.1,1)−J(2,1)=2.13+2.1⋅12=9.261+2.1=11.361,=0.111.361−10=0.11.361=13.61(error 0.61).
For w2, nudge to 1.1:
J(2,1.1)0.1J(2,1.1)−J(2,1)=8+2⋅1.12=8+2(1.21)=8+2.42=10.42,=0.110.42−10=0.10.42=4.20(error 0.20).
(c) Step 3 — central difference for w1. One more value, at 1.9:
J(1.9,1)2(0.1)J(2.1,1)−J(1.9,1)=1.93+1.9=6.859+1.9=8.759,=0.211.361−8.759=0.22.602=13.01(error 0.01).
Step 4 — why the errors are what they are. Taylor-expand J in w1 about 2. The forward quotient picks up the whole second-order term:
forward error≈2Δ∂w12∂2J=20.1⋅6w1=20.1⋅12=0.6,
which matches the 0.61 we saw (the extra 0.01 is the third-order term). In the central quotient the ±Δ evaluations cancel every even-order term, so the first surviving error is third order:
central error≈6Δ2∂w13∂3J=60.01⋅6=0.01,
exactly what we found. Forward is O(Δ), central is O(Δ2): shrink Δ tenfold and the forward error drops tenfold (to 0.0601) but the central error drops a hundredfold (to 0.0001). For w2 the forward error was exactly 2Δ⋅∂2J/∂w22=0.05⋅4=0.20, because J is only quadratic in w2.
(a) ∇J(2,1)=(13,4). (b) Forward (13.61,4.20), errors (0.61,0.20). (c) Central 13.01, error 0.01: forward error ∼Δ⋅6, central error ∼Δ2⋅1.
Remember
The central difference costs one extra evaluation and buys an extra order of accuracy — always use it for an audit. A deliberately large Δ makes the orders visible in your own arithmetic; a tiny Δ like 10−15 is worse, not better, because floating-point round-off then swamps the quotient. The usual slip is dividing the central difference by Δ instead of 2Δ.
Problem 9medium
Steepest descent with exact line search is applied to J(x,y)=x2+4y2 at w0=(2,1). (a) Write the descent direction at w0 and derive the optimal step γ∗ by minimising h(γ)=J(w0−γ∇J(w0)). (b) Compute w1 and the drop in J.
What this tests. The same line-search recipe as Problem 7 on a different bowl, with numbers in place of symbols from the start — and a look at what the optimal step does to the stiff coordinate. Plan. Gradient at the point; write the ray; expand h′ into a linear function of γ; solve; substitute; keep everything as exact fractions until the last line.
Show the full solution
(a) Step 1 — direction at w0.∇J=(2x,8y), so
∇J(2,1)=(4,8),descent direction =−(4,8).
Step 2 — the ray and h. The trial point is (2−4γ,1−8γ), hence
h(γ)=(2−4γ)2+4(1−8γ)2.
Step 3 — minimise h.h′(γ)=2(2−4γ)(−4)+4⋅2(1−8γ)(−8)=−8(2−4γ)−64(1−8γ)=−16+32γ−64+512γ=544γ−80.
Set to zero:
γ∗=54480=345≈0.147,
a minimum since h′′=544>0.
(b) Step 4 — new point and drop in J.w1=(2−4⋅345,1−8⋅345)=(3468−20,3434−40)=(3448,−346)=(1724,−173)≈(1.412,−0.176).
Values:
J(w0)J(w1)=4+4=8,=(1724)2+4(173)2=289576+36=289612=1736≈2.118.
The single optimal step removed about 74% of the objective.
Step 5 — what the sign of y1 is telling you. The y-coordinate overshot past 0, to −173, while x is still on its way. On an elongated bowl even the best step along the gradient goes too far in the stiff direction and not far enough in the gentle one. The next gradient, (1748,−1724), is perpendicular to (4,8) — the perpendicularity property of exact line search — so the path must turn a right angle and zig-zag its way down the valley.
(a) direction −(4,8); h′(γ)=544γ−80, so γ∗=5/34≈0.147. (b) w1=(24/17,−3/17)≈(1.41,−0.18); J:8→36/17≈2.12.
Remember
Keep the fractions: 80/544 reduces to 5/34, and 612/289 to 36/17 — decimals hide the check that the two answers are consistent. Overshooting in the stiff coordinate is not a mistake, it is the reason steepest descent zig-zags; every exact line-search step ends where the new gradient is perpendicular to the old one.
Problem 10hard
Consider the coupled quadratic f(x,y)=x2+xy+y2. (a) Find the minimiser analytically and confirm it is a minimum via the Hessian. (b) From (x0,y0)=(2,0) with γ=0.2, perform two gradient-descent iterations, reporting the gradient, the new point and f each time. (c) Explain, with one computed number, why the coordinates can no longer be treated independently.
What this tests. Careful differentiation when a cross term is present — ∂f/∂x=2x+y, not 2x — and seeing what the off-diagonal Hessian entry does to a descent path. Plan. Solve the two linear gradient equations; read the Hessian's eigenvalues from the a±b pattern; run the table with the full gradient at every row; then point at the one number that shows the coupling.
Show the full solution
(a) Step 1 — the analytic minimiser.∇f=(2x+y,x+2y)=(0,0).
From the first equation y=−2x; from the second x=−2y. Substituting one into the other, x=−2(−2x)=4x, so 3x=0, x=0, and then y=0. The only critical point is (0,0), with f(0,0)=0. The Hessian is constant:
H=[2112],eigenvalues 2±1=3and1,
both positive, so H is positive-definite and (0,0) is the global minimum. (For any matrix [abba] the eigenvalues are a±b, with eigenvectors (1,1) and (1,−1).)
(b) Step 2 — iteration 1. At (2,0):
∇f(2,0)f(2,0)(x1,y1)f(1.2,−0.4)=(2(2)+0,2+2(0))=(4,2),=4+0+0=4,=(2,0)−0.2(4,2)=(2−0.8,0−0.4)=(1.2,−0.4),=1.44+(1.2)(−0.4)+0.16=1.44−0.48+0.16=1.12.
Step 3 — iteration 2. At (1.2,−0.4):
∇f(x2,y2)f(0.8,−0.48)=(2(1.2)−0.4,1.2+2(−0.4))=(2.4−0.4,1.2−0.8)=(2.0,0.4),=(1.2,−0.4)−0.2(2.0,0.4)=(1.2−0.4,−0.4−0.08)=(0.8,−0.48),=0.64+(0.8)(−0.48)+0.2304=0.64−0.384+0.2304=0.4864.
So f:4→1.12→0.4864, heading for (0,0).
k
point
gradient
new point
f
0
(2,0)
(4,2)
(1.2,−0.4)
4
1
(1.2,−0.4)
(2.0,0.4)
(0.8,−0.48)
1.12
2
(0.8,−0.48)
—
—
0.4864
(c) Step 4 — why no decoupling. At (2,0) the y-coordinate already sits at its "own" best value 0, yet
∂y∂f(2,0)=x+2y=2+0=2=0,
so the first step drags y to −0.4 purely because x is large. The cross term xy makes each partial depend on both variables; updates of the form xk+1=(constant)xk no longer exist. The coupling is the off-diagonal 1 in H. What does decouple is the eigenvector frame: along (1,1) the function is f(t,t)=3t2 (curvature 3) and along (1,−1) it is f(t,−t)=t2 (curvature 1); in those coordinates the two factors are 1−0.2⋅3=0.4 and 1−0.2⋅1=0.8.
(a) Minimiser (0,0); H=[2112] with eigenvalues 3,1 — positive-definite. (b) (1.2,−0.4) with f=1.12, then (0.8,−0.48) with f=0.4864. (c) At (2,0), ∂f/∂y=2=0 although y=0.
Remember
A cross term is there to test whether you differentiate carefully: ∂f/∂x=2x+y. Everything else — the update rule, the table — is unchanged. The eigenvalue fact for [abba], namely a±b, is worth keeping; it is where the coordinates do come apart.
Problem 11hard
The line y^=ax+b is fitted to the four points x=(0,1,2,3), y=(1,3,5,7) by minimising L(a,b)=∑i=14(yi−(axi+b))2. (a) Find the exact minimiser (a∗,b∗) from the normal equations and interpret L∗. (b) From (a0,b0)=(0,0) with γ=0.05, perform two gradient-descent iterations; report the gradient, the parameters and L after each. (c) The Hessian of L is H=2[∑xi2∑xi∑xin], with eigenvalues ≈33.6 and ≈2.4. Compute the largest usable step size γmax=2/λmax and use it to explain the path seen in (b).
What this tests. The whole story in one place: the normal equations give the destination, descent gives the journey, and the Hessian explains the driving style. Plan. Compute the four sums once and reuse them everywhere; keep residuals in a row so the gradient is two dot products; after each update recompute predictions, residuals, L; finally compare γ with 2/λmax and read the sign of 1−γλmax.
Show the full solution
(a) Step 1 — the four sums.∑xi∑xi2=0+1+2+3=6,=0+1+4+9=14,∑yi∑xiyi=1+3+5+7=16,=0+3+10+21=34,
and n=4.
Step 2 — the normal equations.a∗b∗=n∑xi2−(∑xi)2n∑xiyi−∑xi∑yi=4(14)−364(34)−6(16)=56−36136−96=2040=2,=n∑yi−a∗∑xi=416−2⋅6=416−12=1.
Check the fit: y=2x+1 gives 1,3,5,7 — every point exactly. So L∗=0: the data are perfectly linear, and the minimum of the loss is a perfect fit.
(b) Step 3 — the gradient formulas. With residuals ri=yi−(axi+b),
∂a∂L=−2∑xiri,∂b∂L=−2∑ri.
Step 4 — iteration 1. At (0,0) the predictions are all 0, so r=(1,3,5,7):
∑xiri∇L=0+3+10+21=34,=(−2⋅34,−2⋅16)=(−68,−32),∑riL0=16,=1+9+25+49=84.
Update:
(a1,b1)=(0,0)−0.05(−68,−32)=(3.4,1.6).
New predictions y^=3.4x+1.6 and residuals:
y^rL1=(1.6,5.0,8.4,11.8),=(1−1.6,3−5.0,5−8.4,7−11.8)=(−0.6,−2.0,−3.4,−4.8),=0.36+4+11.56+23.04=38.96.
Step 5 — iteration 2. From those residuals:
∑xiri∇L=0−2.0−6.8−14.4=−23.2,=(−2(−23.2),−2(−10.8))=(46.4,21.6).∑ri=−10.8,
Update:
(a2,b2)=(3.4,1.6)−0.05(46.4,21.6)=(3.4−2.32,1.6−1.08)=(1.08,0.52).
New predictions and residuals:
y^rL2=(0.52,1.60,2.68,3.76),=(0.48,1.40,2.32,3.24),=0.2304+1.96+5.3824+10.4976=18.0704≈18.07.
k
(ak,bk)
∇L
new (a,b)
L
0
(0,0)
(−68,−32)
(3.4,1.6)
84
1
(3.4,1.6)
(46.4,21.6)
(1.08,0.52)
38.96
2
(1.08,0.52)
—
—
18.07
The pattern: a jumped past its target 2 (to 3.4), then back below it (to 1.08), yet L:84→38.96→18.07 keeps falling.
(c) Step 6 — the step-size speed limit. With the sums from Step 1,
H=2[14664]=[2812128],trH=36,detH=224−144=80.
The eigenvalues solve λ2−36λ+80=0:
λ=236±1296−320=236±976=18±244=18±15.62⇒λmax≈33.62,λmin≈2.38.
The speed limit is
γmax=λmax2≈33.622≈0.0595.
Our γ=0.05 is just under it. Along the stiff eigen-direction the error is multiplied each step by
1−γλmax≈1−0.05×33.62≈−0.68:
magnitude below 1, so the loss keeps shrinking, but negative, so the parameters flip from one side of the optimum to the other — exactly the overshoot-and-return seen in (b). A smaller γ, say 0.02, gives 1−0.02×33.62≈+0.33: a smooth, one-sided approach, but a slower one along the gentle direction (1−0.02×2.38≈0.95).
(a) (a∗,b∗)=(2,1), L∗=0 — a perfect fit. (b) (3.4,1.6), L=38.96; then (1.08,0.52), L≈18.07: oscillating but improving. (c) λ≈33.62,2.38; γmax≈0.0595; 1−γλmax≈−0.68 is negative, so the iterates alternate sides of the optimum.
Remember
The reusable diagnostic is the single number 1−γλ: its size sets the speed, its sign says whether the approach is smooth (+) or oscillating (−), and ∣1−γλmax∣<1 is the same statement as γ<2/λmax. The slip on this type is forgetting the factor 2 in the Hessian (and so in γmax), or dropping the −2 from the gradient of a squared residual.
Problem 12hard
A line search must minimise the unimodal h(α)=α2−5α+8 over [0,αmax]=[0,4]. (a) Trace three iterations of binary search: at each midpoint m decide from the sign of the slope (compare h(m) with h(m+ϵ), ϵ=10−6) whether the minimum lies to the left or the right, and give the new interval. (b) What is the bracket width after k iterations, and how many iterations guarantee a width ≤0.01?
What this tests. The bracketing trace — a decision rule applied mechanically — and the halving count that follows. Plan. Fix the rule before starting: slope negative at m means the minimum is to the right, keep [m,b]; slope positive means keep [a,m]. Then three midpoints; then the width formula and one logarithm.
Show the full solution
(a) Step 1 — iteration 1. Interval [0,4], midpoint m=2. The slope is h′(α)=2α−5, so
h′(2)=4−5=−1<0(equivalently h(2+ϵ)<h(2)).h is still going down at m, so the minimum lies to the right: new interval [2,4].
Step 2 — iteration 2. Midpoint m=3:
h′(3)=6−5=+1>0(h(3+ϵ)>h(3)).h is going up at m, so the minimum lies to the left: new interval [2,3].
Step 3 — iteration 3. Midpoint m=2.5:
h′(2.5)=5−5=0.
Numerically the probe still decides: h(2.5+ϵ)=h(2.5)+ϵ2>h(2.5) (since h(2.5+t)−h(2.5)=t2 exactly), so the rule treats m as "going up" and keeps the left half: new interval [2,2.5].
Step 4 — sanity check. The true minimiser is α∗=2.5 (where h′=0), and it sits inside every bracket:
[0,4]⊃[2,4]⊃[2,3]⊃[2,2.5]. ✓
(Its value is h(2.5)=6.25−12.5+8=1.75; at the two earlier midpoints h(2)=h(3)=2.)
(b) Step 5 — the width count. Each iteration halves the bracket, so
width after k iterations=2k4.
Require 4/2k≤0.01:
2kkk≥400≥log2400≈8.64=9(width 4/512≈0.0078).
Eight iterations leave 4/256≈0.0156, still too wide; nine is the first that works.
Fix the decision rule before you start and the trace runs itself; the width formula (b−a)/2k plus one log2 finishes the count. If golden-section search is used instead, the bracket shrinks by 0.618 per evaluation and the same target takes 13 steps; and if h is not unimodal, no bracketing method is safe — the inexact Armijo rule is the fallback.
Next up
Unit 10 · Optimization I — Gradients that Work— coming soon
You can now walk downhill, choose a stride, and afford the walk on big data. Unit 10 asks why the plain walk is so slow in a trench and fixes it: momentum that remembers the last step, strides that adapt per knob, and the difference between a landscape that is a bowl and one that only looks like it.