Every model that learns does one simple thing: it walks downhill in thick fog. The ground is "how wrong am I right now". You cannot see the valley. You can only feel which way the ground slopes under your feet — so you step the other way, and feel again. This unit shows that walk in pictures you can push around: where the landscape comes from, where the walk stops, how big a step to take, and the trick that makes it work on millions of examples — taste a spoonful, not the whole pot.
≈ 90 min read + play11 interactive widgets · 6 in 3D · a fog you can lift, a ball you can bounce, 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
You are trekking down a hill in Munnar at six in the morning. The mist is so thick that you can see only the patch of ground around your shoes. Somewhere below is the village, at the bottom of the valley. You have no map and no view.
But you can still feel one thing: which way the ground slopes under your feet. So you turn to face straight downhill. You take one step, stop, and feel again. Step by step, you reach the bottom.
That is the whole method of this unit. Try it below. The landscape is real, but you only see the lit patch.
Walking in the fogA landscape you cannot see, a lit patch you can, and a walker who only knows which way is down
Try: press ▶ walk and watch the lit patch slide downhill until the ground goes flat. Then press lift the fog: there were two valleys all along.
drag the picture to orbit · drag the glowing walker to move it
—
The walker only ever uses the slope under its feet, ∇f. Drag it onto the hilltop to feel a flat spot that is not a bottom.
Three lessons hide in that walk. Everything else in this unit grows out of them.
1 · You never needed the map. The walker reached a valley without ever seeing it. It only used the slope under its own feet. This matters because a real model has millions of knobs. Its landscape lives in millions of dimensions, and nobody can draw it. But the slope at one point can always be computed.
2 · The slope is an arrow. Lift the fog and look at the orange arrow. It points the way the ground rises fastest, and its length says how steep it is. Mathematicians call this arrow the gradient and write it ∇f (read it: "grad f"). The green step points the exact opposite way.
A tiny example. You stand at (1,1), and the uphill arrow there is ∇f=(2,1). You choose a stride γ=0.5 (read it: "gamma"; machine learning calls it the learning rate). Turn the arrow around, shrink it by the stride, and step:
(1,1)−0.5×(2,1)=(0,0.5).
3 · You find the valley below you, not the deepest one. After a walk, press lift the fog. There were two valleys all along. The walker went into the one its slope led to. Start somewhere else and it may end in the other. Gradient descent finds a low point, not always the lowest.
One note before we go on. Here you may put your feet anywhere. Real problems sometimes add fences, such as "this number must stay positive". That is called constrained optimization, and it gets its own unit later.
The realization
new=old−γ∇f(old)
Read it: new position equals old position, minus the stride times the uphill arrow at the old position. Take the arrow, turn it around, shrink it to your stride, and step. This one line is the picture you just played with, written in symbols.
Pause & predict
Under your feet, the uphill arrow is ∇f=(3,−4). Which way do you step, and how steep is the ground here?
In one sentence: learning is a walk in fog — feel the slope, step the other way, repeat — which works in any number of dimensions, but finds only the valley below where you started.
2
Why there is a landscape at all
Imagine this
A stationery shop near your college sells printer paper by the box. Here are four old bills, in hundreds of rupees: 1 box cost 3.1, 2 boxes cost 4.9, 3 boxes cost 7.3, and 4 boxes cost 9.1.
The owner wants a quick rule to quote any order: price ≈ a × boxes + b. Here a is the price per box and b is a fixed charge. Every pair (a,b) is a guess. Some guesses are terrible. One is best.
So ask one question: how wrong is each guess, as a single number? The answer builds a landscape.
Draw a guess as a straight line through the four bills. Each bill sits a little above or below the line. That gap is a miss. Its formal name is a residual, written ri.
Square each miss, so that misses above and below both count, and big misses count a lot. Then add them up. That total is the guess's wrongness. Its formal name is the loss, written L(a,b).
Two guesses, worked out:
Guess (a,b)
Line predicts
Misses
Wrongness L
(0, 0)
0, 0, 0, 0
3.1, 4.9, 7.3, 9.1
169.72
(2, 1)
3, 5, 7, 9
0.1, −0.1, 0.3, 0.1
0.12
Now the key step. Treat L as a height above the point (a,b). Every guess gets a height. Put them all together, and you have a landscape. Turn the knobs below and watch the line move and the ball rise or fall.
The fitting bowlFour bills, one line, and the wrongness L(a, b) as a real landscape — drag to orbit, then let the ball roll
Try: press ▶ descend and watch the ball zig-zag down the narrow trench, then crawl along it. Now push γ past 0.030 and press ▶ again: the steps grow instead of shrinking.
drag the picture to orbit
—
One direction of this trench is about 56 times stiffer than the other (stiffness 66.8 against 1.2). The stride must stay under 2/66.8≈0.030, and at that stride the flat direction barely moves.
So the landscape is not just a nice picture. It appears by itself the moment you say what "wrong" means. Training any model means walking down such a landscape. This one has two knobs, so you can see it. A neural network has millions, which is why the fog of Section 1 matters.
The slope has a plain meaning. Start at a=b=0. The line lies flat on the floor, below all four bills. Which way is downhill? "Lift the line up toward the bills." That is exactly what the first step does: both a and b go up. In symbols, with misses ri=yi−(axi+b):
L=i∑ri2,∇L=(−2i∑xiri,−2i∑ri).
Read it aloud. The fixed charge b feels the plain total of the misses. The price per box a feels each miss weighted by its number of boxes.
The bowl is a long trench, not a round dish. Orbit the picture. Change a a little and the wrongness shoots up, because the 4-box bill moves a lot. Change b a little and almost nothing happens. The steep direction is about 56 times stiffer than the flat one. Remember this trench. It makes the walk zig-zag in Section 4, and it makes choosing a step hard in Section 5.
This small problem also has a direct formula. The best line is a∗=2.04,b∗=1, with wrongness 0.072. Bigger models have no such formula. Walking downhill is the only way, so we practise it here, where we can check the answer.
The realization
Wrongness is the sum of squared misses, L=∑i(yi−(axi+b))2. Its slope, ∇L=(−2∑xiri,−2∑ri), just says "move the line toward the points". If the misses are mostly positive, push the line up.
Pause & predict
Start at a=b=0. The line lies flat on the floor, and all four bills sit above it. What does the first downhill step do to the price per box 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 — one height for every setting of the knobs — with a slope that says which way the line should move.
3
Where the walk ends — flat ground comes in three kinds
Imagine this
Drop a small lemon into an empty kadhai. It rolls down and stops at the bottom, where the metal is level.
Now turn the kadhai upside down and balance the lemon on top. The metal is level there too. But one tiny push and the lemon rolls off.
Last, picture a Pringles chip. Along one edge it curves up; along the other it curves down. The very middle is level, yet a lemon placed there would slide off sideways. This shape is called a saddle.
So "level ground" comes in three kinds. The walker stops on any of them.
Level ground means the slope is zero. For a curve f(x), the slope is the derivative f′(x) (read it: "f prime of x"). So the walk can only stop where f′(x)=0. These are the flat spots. Their formal name is stationary points.
How do you tell the three kinds apart without seeing them? Feel how the ground bends. Inside a kadhai it bends up on both sides. On an upside-down kadhai it bends down. The bend is measured by the second derivative f′′(x) (read it: "f double prime").
Flat spot
In everyday words
The test
Valley (minimum)
lemon inside a kadhai: it stays
f′=0 and f′′>0
Hilltop (maximum)
lemon on an upside-down kadhai: it rolls off
f′=0 and f′′<0
Saddle
lemon on a Pringles chip: slides off sideways
in 2-D: bends up one way, down the other
A tiny example: f(x)=x2−4x. The slope is f′(x)=2x−4, which is zero at x=2. The bend is f′′(x)=2, which is positive. So x=2 is a valley, at height f(2)=−4.
Below, a probe sits on a curve. Its tangent line shows the slope. Press ▶ to let it walk downhill.
The flat-spot proberSlide a probe along a curve, read the slope, and let it walk downhill
Try: put the probe at x=−1.3 and press ▶: it slides into the valley at x≈0.66. Now start at x=−1.6, just left of the hilltop: 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, its size says how far. The walk stops on any flat spot, and it cannot tell a shallow valley from the deepest one.
Look at the quartic curve. It has three flat spots: a deep valley on the left, a hilltop in the middle, and a shallow valley on the right.
Start anywhere to the right of the hilltop, and the probe rolls into the shallow valley. The deep valley is 43 units lower, just behind the hill. But the walker cannot get there. Each step goes downhill, and reaching the deep valley would mean climbing first. This is Section 1's warning again, now with numbers.
In two or more dimensions, the bend becomes a small table of second derivatives: the Hessian from Unit 8. It gives the same three verdicts: bowl, dome or saddle.
The realization
The walk stops where the slope is zero, f′(x)=0. The bend decides what kind of stop it is: f′′>0 is a valley, f′′<0 is a hilltop. And the valley you reach is the one below your start, not always the deepest.
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 level ground; the bend tells a valley from a hilltop or a saddle; and you land in the valley below where you started.
4
The rule, and the trench that makes it zig-zag
Imagine this
Think of the concrete drain along a city road: a long, U-shaped channel. Its sides are steep. Along its length it is almost flat.
Drop a marble onto one side wall. It does not roll along the drain toward the far end. It rolls across, up the other wall, and back again — side to side — and only slowly drifts along.
That side-to-side dance is the most famous problem with gradient descent. This section shows where it comes from.
First, the rule in symbols. You have already used it many times:
xnew=x−γ∇f(x).
Now take the simplest landscape of all: a bowl. Its slope works like a spring. The further you are from the bottom, the harder it pulls you back.
A tiny example. On the round bowl f=x2+y2 the slope is ∇f=(2x,2y). From (3,4), with stride γ=0.25:
(3,4)−0.25×(6,8)=(1.5,2).
The distance to the bottom halved in one step, in both directions at once. A round bowl is easy.
A trench is a bowl with two different springs: a stiff one across it and a soft one along it. For a general bowl f(x)=21x⊤Ax+b⊤x, the slope is
∇f(x)=Ax+b.
The matrix A holds the springs. Its eigenvalues are the stiffness along each of the trench's own directions. The bottom is where the springs balance: Ax+b=0, so x∗=−A−1b.
The descent stageGradient descent on a bowl-shaped 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 between the steep walls (factor −0.81) while inching along the floor (factor 0.82). Then switch to exact line search: every turn is exactly 90°.
drag the picture to orbit
—
Orange is the uphill arrow ∇f=Ax+b; green is the step. Each direction shrinks by its own factor 1−γλ, so the stiff one limits the stride and the soft one crawls.
On the lecture's valley the stiffnesses are about 20 across and 2 along. One stride has to serve both springs, and it cannot serve both well. Set γ=0.09 and press ▶. Across the trench, each step multiplies the distance by about −0.81: it flips to the other wall. Along the trench it multiplies by about 0.82: a slow crawl.
The ratio of the two stiffnesses, about 10 here, says how bad the zig-zag gets. On the paper-bill trench of Section 2 it is about 56. Keep this picture. Section 5 puts numbers on it, and Unit 10 is about fixing it.
Two facts make the rule trustworthy. (The proofs are in the drawer below.)
①Stepping against the gradient is the steepest way down. Turn slowly in a full circle: the ground drops fastest when you face exactly away from the uphill arrow.
②A short enough step that way always goes down. Close to your feet, the ground is almost a flat ramp. "Short enough" is the topic of Section 5.
The realization
The rule is xnew=x−γ∇f(x). On a bowl the slope is a spring, ∇f=Ax+b, and the bottom is x∗=−A−1b. A trench has one stiff spring and one soft one, so a single stride bounces across it while crawling along it.
Pause & predict
On the lecture's valley, A=[21120] and b=(−5,−3). The walker starts at x0=(−3,−1), where the slope is ∇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 slope is a spring, ∇f=Ax+b; a trench has a stiff spring and a soft one, so one stride bounces across it while it crawls along it.
5
How long a step — the bouncing ball
Imagine this
You are setting the water in a shower with a mixer tap. It is too cold, so you turn the knob.
🐢Turn it a tiny bit each time: safe, but it takes ages to warm up.
🎯Turn it just right: perfect in one go.
↔Turn it too far: now it is too hot. Turn back too far: too cold. You swing back and forth, but get closer each time.
💥Turn it much too far each time: the swings get worse — hotter, colder, hotter.
The size of your turn is the step size γ. Gradient descent has exactly these four moods.
Take the simplest bowl, one number wide: f(x)=21λx2. Here λ (read it: "lambda") is the stiffness — how sharply the bowl curves. The slope is λx, so one step gives
xnew=x−γλx=(1−γλ)x.
So each step multiplies your distance from the bottom by the same number, 1−γλ. Call it the factor. That one number tells the whole story.
Try it with λ=2, starting at x0=1.8:
Stride γ
Factor 1−γλ
First steps
What happens
0.3
0.4
1.8 → 0.72 → 0.288
smooth, same side
0.5
0
1.8 → 0
one perfect step
0.8
−0.6
1.8 → −1.08 → 0.648
flips sides, but shrinks
1.0
−1
1.8 → −1.8 → 1.8
bounces for ever
1.2
−1.4
1.8 → −2.52 → 3.528
grows: blows up
Now turn the dial below and watch the ball hop.
The step-size dialThe shower-tap dial in 3D — one number, 1 − γλ, decides crawl, perfect step, bounce or blow-up
Try: press sweet, then ▶: with γλ=1 one hop lands exactly at the bottom. Then press edge: γλ=2 hops between the two walls for ever, and blow up flies out of the bowl.
drag the picture to orbit
—
Each hop multiplies the distance to the bottom by 1−γλ. It must stay between −1 and 1, so γ<2/λ; in two dimensions the stiffest direction sets that limit.
The rule hiding in that table: the factor must stay between −1 and 1. That means γλ must stay below 2. This is the speed limit:
γ<λ2.
A stiffer bowl (bigger λ) needs a smaller stride, just as a touchy tap needs gentler turns.
Now switch to the two-dimensional tab. A trench has two stiffnesses, so every step has two factors: one across and one along. The stride must obey the stiff spring's speed limit. At that stride, the soft spring's factor is close to 1, so that direction crawls.
Press zig-zag. With stiffnesses 20 and 2 and γ=0.095, the factors are 1−0.095×20=−0.90 across and 1−0.095×2=0.81 along. Bounce across, crawl along: that pair of numbers is the zig-zag. The ratio of the stiffest to the softest is called the condition number.
In practice, nobody knows the stiffnesses. There are two ways out. Watch the height, and shrink the stride when it goes up (Section 8). Or let the ground choose the stride for you (Section 6).
The realization
Each step multiplies the distance to the bottom by 1−γλ. Keep that between −1 and 1, which means γ<2/λ. With many directions, the stiffest one (λmax) sets the limit for all, and the softest one sets how slowly you crawl.
Pause & predict
A trench has stiffness 20 across and 2 along. You choose γ=0.15. What happens?
Pause & predict
The paper-bill trench of Section 2 has stiffnesses of 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−γλ, so keep γ<2/λmax — and the gap between the stiffest and softest directions is what makes the walk zig-zag.
6
Let the ground choose the stride — line search
Imagine this
You are walking along a straight road that dips under a railway bridge. The road goes down, reaches its lowest point under the bridge, and climbs back up.
You want to stop at the lowest point. You don't need to know in advance where it is. Just keep walking while the road goes down. The moment it starts to rise, stop.
That is the whole idea of this section: pick a direction, then walk along it until the ground stops going down.
Gradient descent picks the direction for you: straight against the slope. Along that straight line, the whole landscape becomes just a dip in a road — height against distance.
Call the distance along the line γ. The height after sliding that far is
h(γ)=f(x−γg),where g=∇f(x).
(Read h(γ) as "the height after sliding a distance gamma".) The best stride, γ∗ (read it: "gamma star"), is simply the bottom of this dip. The method is called line search.
A worked example on f=x2+3y2, starting at (2,2). The slope there is g=(4,12), so the line is (2−4γ,2−12γ). The bottom of the dip is at γ∗=5/28≈0.179. You land at (9/7,−1/7)≈(1.29,−0.14), and the height falls from 16 to 12/7≈1.71. One slide removes 89% of the height.
The line-search lensThe bowl in 3D, sliced along the downhill line — slide a bead to the bottom of the slice
Try: drag γ and watch the glowing height ring at the bead shrink. Press snap to γ*: the ring just touches the line, and the orange slope arrow stands at a right angle to it. Then take the step and repeat.
drag the picture to orbit
—
Along the line the bowl is a simple dip, h(γ)=f(xk−γg), drawn small beside the picture. Its bottom is γ∗=g⊤g/g⊤Ag, where the new slope is perpendicular to the old one.
Watch the orange arrow at the bead as you slide. At first it leans back along the line: you are still going down. Slide too far and it leans forward: you have passed the bottom. At the best point it stands at a right angle to the line.
At that same point, the line just touches a contour ring without crossing it. (A contour is a ring of equal height, like a water line inside a bowl.) This is the same fact seen twice. If the line crossed the ring, it would reach lower ground inside it.
Press take the step a few times. Every corner of the path is a right angle, because each new slope is perpendicular to the last direction. On a long trench this is still a zig-zag, just a neater one. A better stride cannot fix a poor direction; Unit 10 does that.
For a bowl, there is a formula for the best stride:
γ∗=g⊤Agg⊤g.
Check it on our example. Here A=[2006], so g⊤g=16+144=160 and g⊤Ag=2⋅16+6⋅144=896. And 160/896=5/28, as before.
In real training, each height check means a pass over the data, so an exact slide costs too much. Plain gradient descent settles for "far enough that the height clearly dropped". That is the Armijo rule of Section 7.
The realization
Line search fixes the direction −g, then slides to the bottom of h(γ)=f(x−γg). At the best stride, the new slope is at a right angle to the step. On a bowl, that stride is γ∗=g⊤g/g⊤Ag.
Pause & predict
From (2,2) on f=x2+3y2, the exact slide lands at (9/7,−1/7). The slope there is (18/7,−6/7). Take its dot product with the old slope (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 downhill line until the ground stops dropping — there the new slope is at a right angle to your step — and on a bowl the best stride is g⊤g/g⊤Ag.
7
Finding the bottom of one line — higher or lower?
Imagine this
A friend thinks of a number from 1 to 100. You may only ask "higher or lower?". The smart move is to ask about the middle every time. Each answer throws away half of what is left. After 7 questions you know the number, because 27=128 is more than 100.
Finding the bottom of a dip works the same way. At the middle, ask one question: "is the ground still going down here?" If yes, the bottom is to the right. If no, it is to the left.
Section 6 left one job open. On a real loss there is no formula for the best stride. You can only measure the height at any distance you choose. So how do you find the bottom of the dip?
If the dip has just one low point — down, bottom, up, nothing else — you can trap the bottom inside an interval and squeeze. That interval is called a bracket. The widget shows three ways to choose a step:
①Binary search. Check the slope at the middle and keep the half that goes down. The bracket halves every round.
②Golden section. Put two probes inside, at 38% and 62% of the bracket, and keep the side with the lower probe. One old probe is already in the right place for the next round, so each round costs only one new height. The bracket shrinks to 0.618 of its width per round.
③Armijo backtracking. Don't hunt for the bottom at all. Try a bold stride. If the height dropped "enough", take it. If not, halve the stride and try again.
A worked example: h(α)=α2−5α+8 on [0,4], whose bottom is at α=2.5. At the middle, 2, the ground still goes down, so keep [2,4]. At the next middle, 3, it goes up, so keep [2,3]. Then [2,2.5]. Three questions, and the width went 4→2→1→0.5.
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: press one iteration three times on the practice function and read the brackets: [0,4]→[2,4]→[2,3]→[2,2.5]. Then switch to bumpy: the bracket can close in on the wrong dip.
—
Binary halves the bracket using two heights per round; golden section shrinks it to 0.618 using one new height. Armijo only asks: is h(α)≤h(0)+cαh′(0)? If not, it halves α.
After k rounds of binary search the width is (b−a)/2k. To squeeze [0,4] below 0.01 takes 9 rounds, since 4/29≈0.008. Golden section needs 13 rounds. But each of its rounds costs one height instead of two, so it uses fewer measurements overall: 15 against 18.
Armijo is the humble one. Its test for "dropped enough" is
h(α)≤h(0)+cαh′(0),
with a small number c (the widget uses c=0.1). In words: the drop must be at least a small share of what the slope promised. On our example, try α=4. The height is h(4)=4, and the test allows at most 8+0.1×4×(−5)=6. Since 4≤6, accept.
Armijo doesn't need a single dip, and it costs only a few height checks. That is why ordinary gradient descent uses it. On the bumpy function, the bracket methods can trap the wrong dip. That is the price of assuming there is only one.
The realization
Trap the bottom in a bracket and squeeze. Halving leaves width (b−a)/2k after k rounds; golden cuts shrink it by 0.618 per round and reuse a probe. Or skip the hunt: Armijo accepts any stride with h(α)≤h(0)+cαh′(0).
Pause & predict
Binary search on [0,4]. At the middle, 2, the height just 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: when there is no formula for the best stride, trap it in a bracket and squeeze — or, like real training, take Armijo's "good enough" stride.
8
Bold early, careful late — schedules and the bold driver
Imagine this
You are pouring chai from a kettle into a small cup. At first you pour fast: the cup is empty, and there is a long way to go. As the tea nears the brim, you slow right down. Pour fast till the end and it spills over.
The learning rate is your pouring speed. Section 5's bouncing ball poured at one fixed speed: fine at first, spilling near the end. The cure is simple: start fast, finish slow.
A rule for lowering the learning rate over time is called a schedule. We write the rate at time t as αt (read it: "alpha at time t"). Here t counts epochs, which are full passes over the data. So the rate stays fixed during one pass.
Three common schedules, all starting at α0=0.8 with k=0.05, read at epoch t=10:
Schedule
Formula
In everyday words
At epoch 10
Exponential
α0e−kt
lose the same share every epoch
0.485
Inverse
α0/(1+kt)
lose less and less; a long, slow tail
0.533
Step
α0/3⌊t/5⌋
stay flat, then divide by 3 every 5 epochs
0.089
(⌊t/5⌋ means "t/5 rounded down", so it counts the drops so far.) A fourth way ignores the clock and watches the loss itself. It is called the bold driver.
Schedules and the bold driverThree ways to shrink the learning rate on a clock, and one way to let the loss itself drive
Try: with k=0.05, read the three rates at t=10: 0.485, 0.533, 0.089. Then open bold driver and press ▶: the rate grows 5% a step until it overshoots, then halves.
—
Exponential falls by the same share each epoch, inverse falls more and more slowly, and step drops in cliffs. The bold driver ignores the clock: good step, ×1.05; bad step, undo and ×0.5.
Which schedule is best depends on the road ahead. A rate that drops too fast leaves the walker stuck above the bottom. One that drops too slowly keeps it bouncing. Section 11 shows that with noisy slopes, slowing down is the only way the walk ever settles.
The bold driver drives like you do on a new road. While the road is smooth, you speed up a little. Hit a speed-breaker hard, and you brake. In numbers, after each step it looks at the height:
✓Lower than before? Keep the step and raise the rate by 5% (multiply by 1.05).
✗Higher? Undo the step, go back, and halve the rate (multiply by 0.5).
Only downhill steps are kept, so the height never rises. In the widget, the rate creeps up until it hits the speed limit of Section 5, 2/λstiff=0.1. It gets knocked back, then creeps up again. It finds the speed limit by bumping into it.
One warning, for Section 10. If the height you see is only an estimate from a small sample, it can go up by chance. Then the driver brakes for nothing. The fix is to judge over several steps, not after every one.
The realization
Start bold, finish careful. On a clock: α0e−kt, α0/(1+kt), or divide by a constant every few epochs. By feel: the bold driver multiplies the rate by 1.05 after a good step, and undoes and halves after a bad one.
Pause & predict
Training starts at α0=0.8. Step decay 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: the learning rate is a speed that should drop as you near the bottom — on a timer (exponential, inverse, step) or by feel (the bold driver).
9
Is my compass even right? — the known weight
Imagine this
Before a weights-and-measures inspector trusts a vegetable seller's scale, she puts a known 1 kg 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 — written by hand, or by backprop from Unit 7 — is like that scale. It steers the whole walk. If it is off by a sign or a factor of 2, the walk goes confidently to the wrong place. So we test it with a "known weight".
The known weight is the meaning of slope itself: nudge one knob a tiny bit, see how much the height changes, and divide. That gives a measured slope, and it should match the code's slope. This test is called a gradient check. The nudges are called finite differences.
There are two ways to nudge, with a small nudge Δ (read it: "delta"):
Name
How you nudge
Formula
Forward
one way only
ΔJ(w+Δ)−J(w)
Central
a little each way
2ΔJ(w+Δ)−J(w−Δ)
A worked example: J(w1,w2)=w13+w1w22 at (2,1). The true slope in w1 is 3w12+w22=13. The heights we need are J(2,1)=10, J(2.1,1)=11.361 and J(1.9,1)=8.759. With Δ=0.1:
→Forward: (11.361−10)/0.1=13.61. Off by 0.61.
↔Central: (11.361−8.759)/0.2=13.01. Off by only 0.01.
The gradient auditorTest a coded slope against nudges — and see where rounding ruins the test
Try: at Δ=0.1, read the two estimates of ∂J/∂w1: forward 13.61, central 13.01, truth 13. Slide Δ smaller: the central error falls twice as steeply — until, below 10−6, both curves turn back up.
—
Here J=w13+w1w22 at (2,1). The forward error shrinks like Δ, the central one like Δ2 — until rounding noise, divided by a tiny Δ, takes over.
The chart has three zones. On the right, with a big Δ, both guesses are poor. In the middle, shrink Δ tenfold and the forward error drops tenfold, but the central error drops a hundredfold. Nudging both ways cancels the bend of the curve, which a one-sided nudge leaves behind.
On the left, with a tiny Δ, both errors climb back up. A computer keeps only about 16 digits. When Δ is tiny, the two heights agree in almost all of them. What is left is rounding noise, divided by a tiny number. So the best Δ is small, but not too small: around 10−5 for the central nudge.
A gradient check tests a few knobs by hand. It is never how you compute gradients for real. It would be far too slow for millions of knobs.
The realization
To audit a slope, nudge and divide. Forward, ΔJ(w+Δ)−J(w), is off by about 21J′′Δ. Central, 2ΔJ(w+Δ)−J(w−Δ), is off by about 61J′′′Δ2. So use central, with a small but not tiny Δ.
Pause & predict
With Δ=0.1, the central estimate of a slope 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: check a coded slope by nudging a few knobs; the two-way nudge is far more accurate, until the nudge is so tiny that rounding takes over.
10
Three ways to walk — batch, minibatch, stochastic
Imagine this
Your mother is cooking a big pot of sambar and wants to check the salt. She does not drink the whole pot. She stirs, tastes one spoonful, and adds a pinch. Stir, taste again, adjust again.
Each spoonful is a little different from the whole pot. But on average it tells the truth, and it is quick. Many fast tastes beat one perfect but impossible measurement.
A real loss is the pot. It is a sum over every example in the data, so the exact slope needs a pass over all of it — for every single step. The spoonful is the idea of this section.
Three walkers differ in just one thing: how much data they read before each step.
🍲Batch reads everything. Its slope is exact, but each step is expensive.
🥄Minibatch reads a random handful — a spoonful — and steps on the slope of that handful.
💧Stochastic (which means "random") reads one single example and steps on that.
In symbols: the loss is a sum over examples, L=∑nLn, so its slope is a sum too, ∇L=∑n∇Ln. A minibatch S is a random set of ∣S∣ examples (read ∣S∣ as "the size of S"). It uses only its own part of that sum.
A quick count. You have 1,000 examples, and one batch step reads all of them. For the same reading, minibatches of 10 give you 100 steps. The stochastic walker gets 1,000 steps.
Below, all three walk the same bowl, from the same start, with the same stride. Race them. Then change what "fair" means.
Three ways to walk: batch, minibatch, stochasticSame bowl, same start, same stride — one walker reads all 40 points before each step, one reads a handful, one reads a single point
Try: press ▶ in fair per step: batch looks best. Now choose fair per data read: while batch is still reading its first 40 points, the stochastic walker has sprinted to the bottom. It then jitters there until you switch on decay.
drag the picture to orbit
—
A minibatch step costs ∣S∣ reads instead of N, points the right way on average, and has noise shrinking like 1/∣S∣. Per data read — the budget that matters for big data — the noisy walkers win.
The race has two verdicts, and both are true. Judged per step, batch looks best: smooth and straight. The other two wobble.
Judged per data read, it flips. By the time batch has read its first 40 points and taken one step, the stochastic walker has taken 40 steps and is most of the way down. This is why large models are trained on minibatches.
Why can a spoonful steer at all? Because it is right on average. Each example's slope points a little differently, and the true slope is their average. So a random handful's slope scatters around the true one, not off to one side. Over many steps, the scatter cancels out.
A bigger spoon means less scatter. The noise shrinks like 1/∣S∣: four times the batch gives half the noise. In practice, the batch size is often set by what fits in the computer's memory.
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
slope used
exact
right on average; noise ∝1/∣S∣
right on average; noisiest
the path
smooth, but slow per data read
a little wobbly, fast
jittery, fastest early
at the bottom
arrives exactly
hovers in a small cloud, unless the stride shrinks
hovers in a bigger cloud, unless the stride shrinks
use it when
the data is small
almost always — deep learning's default
data arrives one example at a time
The realization
The loss is a sum over examples, so its slope is too. A random handful gives ∇J(S)=∑i∈S∇Li; scaled by N/∣S∣, it is right on average, and its noise shrinks like 1/∣S∣. Step on it: w←w−α∇J(S). That is minibatch gradient descent; ∣S∣=1 is stochastic, and ∣S∣=N is batch.
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, you 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 per step; a random spoonful is right on average, so per data read the noisy walkers win — which is why deep learning uses minibatches.
11
Noise, tamed — and noise as a friend
Imagine this
Try threading a needle on a moving train. Your hands shake a little with every jolt.
Far from the needle, you move boldly; the shaking hardly matters. Close to the needle's eye, big movements plus shaking make you miss again and again. So you make your last movements tiny. Now the shake is a small part of a small movement, and the thread goes in.
A noisy slope is a shaking hand. The step size is the size of your movement.
Near the bottom, the true slope is almost zero. But a spoonful's slope is not. It keeps saying "a bit this way", "a bit that way". So with a fixed stride, the walker never settles. It jitters in a small cloud around the bottom.
The size of that cloud grows with the stride: halve γ, and you roughly halve the cloud. Below, one noisy walker is shown up close. At each step you can see the true slope and the spoonful's slope side by side.
The noisy descentOne noisy walker up close — the true slope and the spoonful's slope, side by side at every step
Try: set the batch size to 1 and press ▶: the walk jitters around the bottom and never settles. Switch to decaying γ: the same noisy steps shrink over time and calm down onto the bottom.
—
A batch of 4 costs 4 points per step instead of 40, and is right on average. Its variance shrinks like 1/∣S∣: bigger batches give calmer but dearer steps.
So the slowing down of Section 8 is not optional here. It is the only way the walk ends. Shrink the stride over time: fast enough to close the cloud, but slowly enough that the walker still reaches the bottom. The decay tab shows exactly this.
Mathematicians write the rule for a good schedule in two parts:
t∑γt=∞andt∑γt2<∞.
In words: the strides must add up to an unlimited distance, so you can travel as far as you need. But their squares must add up to a finite total, so the shaking dies out. The schedule γ0/(1+kt) passes both tests. With such a schedule, the noisy walk settles into a valley bottom (a local minimum) with probability one.
Now a surprise. Remember the two valleys of Section 1 and the shallow valley of Section 3. An exact walker in a shallow dip is stuck for ever, because every exact step goes down. A noisy walker is not. Its shaky steps sometimes go up by chance. On a bumpy landscape, a few lucky upward steps can carry it over the rim into a deeper valley.
So the noise that made the walk messy can also help it find a better answer.
The realization
With a noisy slope and a fixed stride, the walker ends in a cloud around the bottom whose size grows with the stride: its variance is about γσ2/2λ, where σ is the size of the noise. A shrinking stride with ∑γt=∞ and ∑γt2<∞ closes the cloud. The same shake can lift a walker out of a shallow dip.
Pause & predict
You run the one-example walker with a fixed stride. The loss falls fast, then stops improving and wobbles a little 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 slopes; the other uses one-example slopes. 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 slope is a shaking hand — a fixed stride leaves a jitter cloud, a shrinking stride closes it, and the same shake can hop a walker out of a shallow dip.
12
What to carry forward
The whole unit is one habit: feel the slope, step the other way. Each section asked one question about that step. Here they are side by side.
The picture
What it says
The maths it becomes
The misty trek
you only need the slope under your feet — and you reach the valley below your start
x←x−γ∇f
The paper bills
say what "wrong" means and a landscape appears; its slope says which way the line should move
∇L=(−2∑xiri,−2∑ri)
The lemon and the kadhai
level ground is a valley, a hilltop or a saddle — the bend decides
f′=0; sign of f′′ (the Hessian in 2-D)
The road-side drain
a bowl's slope is a spring; a trench has a stiff one and a soft one
∇f=Ax+b; eigenvalues = stiffnesses
The shower tap
each step multiplies the distance by a factor; the stiffest direction sets the speed limit
1−γλ; γ<2/λmax
The road under the bridge
slide until the ground stops dropping; the next slope is at a right angle
γ∗=g⊤g/g⊤Ag
Higher or lower?
trap the bottom and squeeze — or accept "good enough"
(b−a)/2k; 0.618; Armijo
Pouring chai
fast far away, slow near the brim
α0e−kt, α0/(1+kt), step; bold driver
The 1 kg weight
test the slope by nudging; nudging both ways is far better
error ∝Δ vs ∝Δ2
A spoonful of sambar
a random handful's slope is right on average; per data read, noisy wins
E[∇J(S)]∝∇L; noise ∝1/∣S∣
Threading a needle on a train
a fixed stride leaves a cloud; a shrinking stride closes it; the shake can hop a rim
cloud ∝γ; ∑γt=∞,∑γt2<∞
The one thing to remember
Feel the slope, step against it, and let the stiffest direction set your stride. The slope is the only map you get in the fog. A stride under 2/λmax always goes down. A trench makes that stride small and the path a zig-zag. And when the data is too big to read, a random spoonful points the right way on average. The next units — momentum, Newton's method, constraints — are cleverer ways to walk the same fog.
In one sentence: gradient descent is one reflex — step against the slope — and this whole unit is about how big, how exact and how cheap that step should be.
13
Practice arena — twelve problems, solved in full
Twelve problems, from easy to hard. They cover the whole walk: sorting flat spots, tracing steps by hand, schedules, line search, gradient checks, a bowl with a cross term, fitting a line by descent, and a bracket trace. Every number has been checked by computer.
Two habits do most of the work. Trace steps in a table — point, slope, new point, height — so nothing gets lost. And for any stride question, find the stiffest direction first: 2/λmax is the limit, and 1−γλ tells you the rest.
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.
You can now walk downhill, choose a stride, and afford the walk on big data. Unit 10 is about the four ways that walk quietly goes wrong — a bad start, memorising instead of learning, the wrong valley or a salt flat, and a canyon that makes every step bounce — and the cheapest fix in machine learning: change the units of the data, not the model, so the canyon becomes a bowl.