Fitting by Walking
Rather than solving for a fitted line all at once, we can improve an initial guess through small adjustments. The gradient tells us which direction increases the error, so we can try moving the other way.
17 sections · 15 questions · 4 coding challenges
The arrow keys move between sections too.
What If Solving Everything at Once Is Too Difficult?
BOTIE
Cauchy raised this difficulty in 1847 while describing a general method for solving simultaneous equations. Instead of relying entirely on elimination, he considered progressively reducing a function that measures the discrepancy. This is an early source for the idea of steepest descent.
For regression, we start with a line and measure its error. We ask how a small change to each coefficient would change that error, then adjust in a direction that locally reduces it. We repeat because the useful direction can change after a step. The step size matters: a local slope is guidance near the current position, not permission to jump an arbitrary distance.
Sources for this history
Finding the Line Without Knowing the Answer
Suppose a model has adjustable settings and an error meter, but no convenient formula giving the best settings immediately. We can still improve it by finding a direction, taking a small step, and checking the result.
For a line, the settings control its tilt and vertical position. First connect changing those settings with changing a visible prediction.
A setting identifies one candidate line
The observations stay fixed while the prediction rule changes. Choosing one line means choosing its settings. We need a way to judge that candidate before deciding how to move it.
Vertical: Weight
Horizontal: Height
The derivative chooses a direction; the learning rate sets the distance
The detailed lesson puts the line's parameters on their own coordinate plot. Error becomes a surface, and moving downhill corresponds to changing prediction lines.
We will calculate one update before running many. Convergence, stopping, and feature scaling become easier to interpret once that step is concrete.
Explore the full example
Follow the current line and its error together. Compare a small learning rate with a larger one, and watch whether the error falls or grows.
…
Part 1. Why Fit by Walking?
1. The answer we already know
Three people, at 165 centimetres and 62 kilograms, 170 and 68, and 175 and 68. The regression page’s formula gives their best line in one step, and it is the dashed line in the box above.
The destination
| value | |
|---|---|
| slope | 0.6 kg per cm |
| level, the weight predicted at the mean height of 170 | 66 kg |
| intercept, the same line at a height of zero | −36 kg |
| mean squared error, the lowest any line can reach | 2.0 |
Nothing on this page will improve on those numbers. They are the destination, and everything the walk does can be judged by how close to them it gets and how long it takes. Do not start the walk yet.
2. Why fit it iteratively anyway?
Some models have a direct least-squares solution. Many of the important ones do not, and for those the only route to a fit is to start somewhere, ask which way makes the loss smaller, move a little that way, and repeat. That method needs four things and nothing else. A current set of parameters, a loss, the gradient of that loss, and a rule for the update.
So treat this page as a controlled experiment. Gradient descent is being used where it is not needed, on purpose, because the known answer lets us check whether the method arrived, how fast, and what went wrong when it did not. On the logistic page and every page after it, that check will not be available.
Keep in mind
The direct solution is a system of linear equations, the normal equations from the polynomial page. It does not mean computing a matrix inverse, and a careful implementation solves the system through a QR or singular value decomposition rather than forming one. The walk’s advantage on large problems is against that solve, not against an inversion nobody should be doing.
Part 2. Every Line Is a Point in Parameter Space
3. Level and slope as two adjustable settings
The regression page wrote a line as an intercept and a slope. This page writes the same line a different way.
ŷ = α + β·x intercept and slope
ŷ = level + slope·(x − x̄) level at the mean, and slopeThe level is the prediction at the mean height, which is 170 centimetres here. The slope is the predicted change per centimetre. A level of 66 and a slope of 0.6 describe the same line as an intercept of −36 and a slope of 0.6.
intercept = level − slope × mean height
= 66 − 0.6 × 170
= −36Only the reference point used to describe the line has changed.
| intercept and slope | level and slope | |
|---|---|---|
| the best line for the three people | α = −36, β = 0.6 | level = 66, slope = 0.6 |
| a flat line at zero | α = 0, β = 0 | level = 0, slope = 0 |
| what the first setting means | weight at a height of 0 cm | weight at the mean height |
Two names for each line, and the same predictions under both. Section 24 shows why the second naming is the one worth walking on.
4. Searching through possible lines
Before any formula and before any walk, hunt for the line by hand. The two sliders below set a level and a slope, the picture draws the line they make with its three residuals, and the readout gives the mean squared error. Try to get the error as low as you can, then press the button to see how close you came.
The line those settings make
Every combination of level and slope defines one candidate line, and every candidate line earns one loss. That is the whole of what the walk is going to search through, and doing it by hand first makes the next section’s picture obvious rather than abstract.
5. The loss surface
Now put the two settings on two axes. Level across, slope up, and at every pair the mean squared error the line there earns, shaded darker where it is larger.
(level, slope) ⟼ MSEThe line those settings make
The same settings as a point on the loss
…
Darker is a larger loss.
Move a slider and both panels move together. That is the central picture of the page. Fitting a line can be seen as moving a line through the data, or as moving a point across an error surface, and those are the same act. The regression page found the bottom of this surface by algebra. This page is going to walk down to it.
Part 3. The Gradient as a Local Direction
6. Changing only the level
Before moving in two directions, freeze the slope and move the level alone. The loss becomes a curve over one number, and at the current level that curve has a tangent whose slope says which way is down.
In the left panel the tangent tilts downward to the right whenever the level is below 66 and upward to the right whenever it is above, so its slope, the derivative of the loss with respect to the level, is negative on one side and positive on the other and is what tells the walk which way to shift the line up or down.
7. Changing only the slope
Now the other way round, in the right panel. Freeze the level and rotate the line. The loss is again a curve over one number, its tangent again says which way is down, and the derivative with respect to the slope says whether to tilt the line steeper or flatter.
The two panels ask the same kind of question of two different settings. How does the loss change if this one number moves a little? A derivative is the answer to that question, and a model with two settings has two of them.
8. The gradient
Only now put the two derivatives together.
∇L = ( ∂L/∂level , ∂L/∂slope )Each component describes one direction. Together they are an arrow in the plane of section 5, and it points the way the loss increases fastest. Its negative points the way the loss decreases fastest, and that is the direction a pass steps.
The same settings as a point on the loss
…
Darker is a larger loss. Red points uphill along the gradient, green downhill, and the hollow ring is where one pass at a rate of 0.02 would land.
The red arrow is uphill, the green arrow is downhill, and the hollow ring is where one pass at a rate of 0.02 would land. Drag the sliders around the surface and the arrows always cross the contours at right angles, because the direction along a contour is the direction in which the loss does not change at all. Gradient descent moves both settings at once using one arrow assembled from their two partial derivatives.
Part 4. One Pass from Start to Finish
9. Anatomy of one pass
A pass is a fixed sequence, and it is worth seeing it as one before seeing any of its arithmetic. Use the current settings. Make predictions. Take residuals. Square and average them into the loss. Read the gradient. Scale it by the learning rate. Update both settings. Begin again from the new ones.
| centred height x′ | weight y | prediction ŷ |
|---|
The inspector starts where the fit starts, a flat line at zero, which is deliberately poor rather than the worst line there is, since arbitrarily bad lines exist. Step through the stages with the buttons, then make the new line current and step through again. The next four sections are the arithmetic of the first pass, and the inspector’s numbers are the ones to check them against.
10. Starting predictions and loss
At pass zero the level and the slope are both 0, so every prediction is 0 and every residual is the person’s whole weight.
Worked example
| centred height x′ | weight y | prediction ŷ | residual e | e² |
|---|---|---|---|---|
| −5 | 62 | 0 | 62 | 3844 |
| 0 | 68 | 0 | 68 | 4624 |
| 5 | 68 | 0 | 68 | 4624 |
L = (3844 + 4624 + 4624) / 3 = 13092 / 3 = 436411. The level gradient
Take one person. Their residual depends on the level, and their squared residual depends on the residual, so the level reaches the loss through a chain of two steps.
eᵢ = yᵢ − level − slope·x′ᵢ
Lᵢ = eᵢ²
level → eᵢ → Lᵢ›The chain rule, one person at a time
| Expression | What changed |
|---|---|
| ∂Lᵢ/∂eᵢ = 2eᵢ | the derivative of a square |
| ∂eᵢ/∂level = −1 | raising the level by one lowers every residual by one |
| ∂Lᵢ/∂level = 2eᵢ · (−1) = −2eᵢ | the chain rule multiplies the two |
| ∂L/∂level = −(2/n) Σ eᵢ | the loss is the mean over people, so average the shares |
Only now put in the numbers. The three residuals are 62, 68 and 68, so the level gradient is minus two thirds of their sum.
∂L/∂level = −(2/3)(62 + 68 + 68) = −13212. The slope gradient
Same chain, one change. The slope reaches a residual through that person’s centred height, so the middle step is −x′ᵢ instead of −1.
∂eᵢ/∂slope = −x′ᵢ
∂Lᵢ/∂slope = −2eᵢ·x′ᵢ
∂L/∂slope = −(2/n) Σ eᵢ·x′ᵢWorked example
| person | residual e | x′ | level share −(2/3)e | slope share −(2/3)e·x′ |
|---|---|---|---|---|
| 165 cm | 62 | −5 | −41.33 | +206.67 |
| 170 cm | 68 | 0 | −45.33 | 0 |
| 175 cm | 68 | 5 | −45.33 | −226.67 |
| sum | −132 | −20 |
The person at the mean height contributes to the level gradient and nothing at all to the slope gradient, because rotating the line about the mean does not move it where they stand.
Every observation contributes to the gradient, and what each contributes depends on both its residual and its input. The gradient stage of the inspector shows these two share columns for whatever line it is standing on.
13. The first update
The update rule is the same for both settings.
θ_new = θ_old − η·∇LWith η = 0.02, stepping against the two gradients gives
level = 0 − 0.02·(−132) = 2.64
slope = 0 − 0.02·(−20) = 0.4Run the new line forward and its loss is 4017.16, down from 4364. One pass used the gradient at the old line to produce the new one, and the next pass will read its gradient at the new line rather than the old, which is what makes the walk a walk. The second pass reads residuals of 61.36, 65.36 and 63.36, a level gradient of −126.72 and a slope gradient of −6.667, and lands at 5.1744 and 0.5333.
Check yourself
Questions on Parts 1 to 4
Question 1 of 5
The regression page’s formula already gives these three people their best line in one step. Why walk to it?
Part 5. The Complete Walk
14. Following the complete walk
Every pass of one walk is recorded, and the dashboard below reads that record off every instrument at once. The scrubber picks a pass and every panel follows it.
The path across the loss
…
Level and slope, pass by pass
Each drawn on its own scale from zero to its target. The dashed lines are the closed form.
Loss, log scale
The green dashes are the closed form’s loss, the floor nothing can go below.
Gradient size and movement, log scale
Distance from the closed form, per parameter, log scale
A straight line here is geometric shrinking, and the steeper line is the direction with the larger curvature.
What this run can claim
…
Four things are visible here that a single loss curve hides. The two settings move at very different speeds. The slope is at 0.6 to four decimals by pass 10, while the level is at 22.1 then, 64.9 at pass 100 and 65.98 at pass 200. The loss drops fast at first and slowly later, and on the log scale the late part is a straight line, which is geometric approach.
The gradient shrinks with it. And the fit agrees ever more closely with the closed form, without ever being told what the closed form is.
15. What convergence means
The status board on the dashboard separates claims that are easy to run together. Loss is finite. Loss fell during the last pass. The gradient is small. The largest movement is under the tolerance. The distance from the known answer is small. The pass limit was reached. The convergence criterion was met.
Those are different facts, and a run can hold some without the rest. Set the passes allowed to 100 and the loss is finite, falling and near its floor, while the pass limit was hit and the criterion was not met, and the level is still a kilogram from home. A run ending is not the same as a run converging, and converging is not the same as being right.
Keep in mind
One of those instruments is a luxury. The distance from the closed form can be read here because the closed form is known. On the logistic page and every page after it, there is no known optimum to measure against, and the remaining instruments, the gradient, the movement, the loss change and the budget, are all a run has.
Part 6. What the Learning Rate Changes
16. Comparing learning rates
Same three people, same flat start, same sixty passes, four rates.
rate 0.005, small, still walking
…
rate 0.02, productive, direct
…
rate 0.055, overshoots, still shrinks
…
rate 0.07, past the threshold
…
The green dot is the closed form, the black dot where the walk stood when the record ended. A run past the threshold is recorded only until its numbers could overflow.
At 0.005 the walk is heading the right way and is nowhere near arriving, level 29.9 after sixty passes with a loss of 1306. At 0.02 it is at 60.3 with a loss of 34.5. At 0.055 it is at 65.94 with a loss of 2.004, close enough that the difference is the fourth decimal. At 0.07 the slope is at −19 million and the record had to stop before the numbers overflowed. The table below asks for a plain fit at eight rates and reports what came back.
| step size | what happened | passes | slope reached | error kept per pass |
|---|
…
17. Direct, oscillating, slow, and divergent paths
The small parabolas under each contour plot are the slope direction on its own, the loss along the slope with the level held at its optimum, and the dots are the first eight passes hopping along it. They show four behaviours.
| Behaviour | What the hops do | Rate here |
|---|---|---|
| small but convergent | every hop closes a little of the gap from the same side, and the budget may run out first | 0.005 |
| directly convergent | hops close in without ever crossing the bottom | 0.02 |
| oscillating but convergent | each hop crosses the bottom, and each crossing is smaller than the last | 0.055 |
| divergent | each crossing is larger than the last, until the numbers stop being usable | 0.07 |
At 0.055 the slope crosses 0.6 on every pass and still arrives, because the crossings shrink. At 0.07 they grow. The boundary between those two is the threshold the rest of the page is about, and it is at 0.06 for these people.
18. Parameter error versus loss
The readouts on this page quote a factor per pass, 0.96 at a rate of 0.02, and it matters what that factor measures. For one direction with curvature c, a pass turns the distance from the optimum into
e_{k+1} = (1 − ηc)·e_kso the distance, in the parameter’s own units, keeps a share of |1 − ηc| per pass. The loss contribution from that direction is quadratic in the distance, so it keeps the square.
| the level direction at η = 0.02 | |
|---|---|
| share of the parameter error kept | |1 − 0.02 × 2| = 0.96 |
| share of that direction's loss contribution kept | 0.96² = 0.9216 |
The 0.96 the box at the top of the page reports is the error-distance factor. It is not the share of loss remaining.
Part 7. Why the Threshold Exists
19. Curvature and step size
Two bowls, one wide and shallow, one narrow and steep. Start the same distance from the bottom of each and take one pass at the same rate.
wide, curvature 2
narrow, curvature 100/3
Both walks start one unit from the bottom, the black dot, and the green dot is where one pass lands.
At the same distance from its minimum the steeper bowl has the larger derivative, so the same rate takes a larger step on it. A rate that creeps politely down the shallow bowl can leap clean across the steep one and land farther from the bottom than it started. A learning rate is safe or unsafe only relative to a curvature.
20. Deriving the stability bound
Take the bowl to be a quadratic in the distance from its minimum.
| Expression | What changed |
|---|---|
| L(e) = ½·c·e² | a bowl of curvature c, distance e from its bottom |
| L′(e) = c·e | its derivative, the pull toward the bottom |
| e_new = e − η·c·e = (1 − ηc)·e | one pass, stepping against the derivative |
So every pass multiplies the remaining distance by the same fixed factor, and what the walk does is entirely a question of where that factor sits.
| Factor 1 − ηc | Behaviour |
|---|---|
| between 0 and 1 | same side, closer |
| exactly 0 | lands on the optimum in one pass |
| between −1 and 0 | crosses, and still closer |
| exactly −1 | hops between two places forever |
| less than −1 | crosses, and farther away |
| greater than 1 | farther away without crossing |
|1 − ηc| < 1
0 < η < 2/cShrinking needs the size of the factor below one, and solving that for η gives the bound. The walk arrives for any rate between zero and two over the curvature, and runs away above it.
21. Several curvature directions
The surface of section 5 has two directions, and on centred heights each has its own curvature.
c_level = 2
c_slope = 2·Var(x′) = 2 × 50/3 = 100/3›Where the two curvatures come from
A curvature is how quickly a gradient changes as its own setting moves, so the two gradients of Part 4 are what to differentiate once more.
| Expression | What changed |
|---|---|
| ∂L/∂level = −(2/n) Σ eᵢ | the level gradient of section 11 |
| ∂eᵢ/∂level = −1 | raising the level by one lowers every residual by one |
| ∂²L/∂level² = (2/n) Σ 1 = 2 | every person contributes the same 2/n, and there are n of them |
| ∂L/∂slope = −(2/n) Σ eᵢ·x′ᵢ | the slope gradient of section 12 |
| ∂²L/∂slope² = (2/n) Σ x′ᵢ² = 2·Var(x′) | ∂eᵢ/∂slope is −x′ᵢ, and a centred input has mean zero, so the average of x′² is its variance |
| (2/3)(25 + 0 + 25) = 100/3 | the three people, at centred heights of −5, 0 and 5 |
The level’s gradient changes by the same amount per person whatever the heights are, which is why its curvature is 2 for any dataset handed over this way. The slope’s gradient changes by a person’s squared centred height, so its curvature is set by how spread out the inputs are. That is the fact section 25 uses when it scales the input, and it is the reason the two directions of one surface can be curved so differently.
Each has its own factor, and the rate has to be safe for both. The two number lines below move together as η changes, and the slope’s marker reaches −1 first.
Level same side, closer. Slope same side, closer. Both inside, so the walk arrives.
η = 2 / (100/3) = 0.06The steepest direction sets the largest safe learning rate. The level direction would tolerate any rate below 1, and it never gets the chance, because the slope direction runs away at 0.06.
22. Why one direction converges more slowly
A rate safe for the steep direction is small for the shallow one. At η = 0.02 the two factors are
|1 − 0.02 × 2| = 0.96
|1 − 0.02 × 100/3| ≈ 0.333so the slope error is cut to a third every pass and the level error is cut by four percent. The slope has thrown away two thirds of its error before the level has thrown away four percent of its, and after the first dozen passes the walk is entirely a walk in the level direction.
The distance panel on the dashboard in section 14 draws both errors on a log scale, where each is a straight line and the slope’s is far steeper. The 477 passes are the level’s doing, and nothing is wrong with the slope at all.
23. The best fixed-rate compromise
For a single direction with curvature c, there is a rate that removes all the remaining error in one step.
rate η = 1/c
remaining-error factor = 1 − ηc = 1 − (1/c)c = 0With two different curvatures, one rate cannot do that for both. The best fixed rate must balance the two directions.
›Where the compromise sits
Call the smallest curvature μ and the largest L. The slower direction determines the worst remaining-error factor. The best fixed rate makes the two extreme factors equal in size and opposite in sign.
worst factor = max(|1 − ημ|, |1 − ηL|)
balance condition: 1 − ημ = −(1 − ηL)η* = 2 / (L + μ)
for the three people: η* = 2 / (2 + 100/3) ≈ 0.0566
worst factor there: 1 − 0.0566 × 2 ≈ 0.8868That is a little below the threshold of 0.06, not immediately under it. A rate just under the threshold has a slope factor near −1 and spends its passes hopping across the bottom, while η* balances the two directions so neither is the bottleneck. Press η* on the number lines in section 21 to see both markers land at 0.887 from opposite sides.
Check yourself
Questions on Parts 6 and 7
Question 1 of 5
At a rate of 0.02 the page quotes a factor of 0.96 per pass for the level direction. What does that factor measure?
Part 8. Centering, Scaling, and Conditioning
24. Centering the input
The page has walked on centred heights throughout, and the reason can now be shown. Below are the same three people handed over three ways, with the loss surface and a walk at the same rate drawn for each.
…
The lattice is centred on the optimum and wide enough to hold the start at zero. The contours cannot show a valley fifty million to one at this resolution, which is itself the point about raw heights.
On raw heights the two settings are an intercept at a height of zero and a slope, and they are strongly coupled. Rotating the line about the origin swings its height near 170 by hundreds of kilograms, so almost any change in slope has to be repaid by a change in intercept to keep the predictions where they were, and the contours are a valley tilted so steeply that its two curvatures are 0.00115 and 57,835. A rate of 0.02 runs away in four passes, and the largest safe rate is 0.0000346.
Centred, the level is the prediction at the mean input, rotating the line about the mean leaves that prediction untouched, and the two settings stop pulling against each other. The contours line up with the axes, the curvatures are 2 and 33.3, and the same rate arrives in 477 passes. Same fit, same loss of 2.0 at the optimum, a different parameterisation of it. Centring removes the coupling.
25. Scaling the input
Centring does not make the two curvatures equal. The bowl is still seventeen times steeper along the slope than along the level, which is the whole of section 22. Scaling the centred height changes that.
zᵢ = (xᵢ − x̄) / sₓDividing by the standard deviation changes the numerical spread of the input, and the slope’s curvature is twice the variance of the input, so it changes that curvature directly. Switch the playground to centred and scaled. Both curvatures are now 2, the contours are circles, and the slope is 2.449 per standard deviation instead of 0.6 per centimetre, which is the same line described in a different unit.
Set the rate to 0.5 and the walk lands on the optimum in one pass, because 1/c is the same for both directions and section 23’s compromise no longer has anything to compromise between.
Centring and scaling are related and distinct. Centring removes the coupling between the settings. Scaling evens out the curvature across them. Each helps on its own, and the feature scaling page does both routinely for reasons this page has now earned.
26. Conditioning
The two curvatures do two different jobs. The largest sets stability, since it decides the threshold. The smallest sets the late-stage speed, since its direction is the one still crawling when the others have arrived. Their ratio describes how uneven the surface is, and it has a name.
κ = L / μ| Heights handed over | μ | L | κ | largest safe rate |
|---|---|---|---|---|
| raw | 0.00115 | 57,835 | 50,000,000 | 0.0000346 |
| centred | 2 | 33.3 | 16.7 | 0.06 |
| centred and scaled | 2 | 2 | 1 | 1.0 |
Near 1, every direction is curved alike and one rate serves all of them. Large, the surface is a narrow valley and any fixed rate is a poor compromise between crawling along it and bouncing off its walls. Centring and scaling took these three people from fifty million to one, and that is not a promise. They often improve the conditioning a great deal, and they do not always repair it, since features can be coupled in ways that a shift and a stretch per column cannot undo.
Part 9. Stopping Honestly
27. Iteration limits, divergence, and numerical failure
A walk can stop for four different reasons, and the reason is part of the result.
| Stopped because | Evidence | What can be claimed |
|---|---|---|
| it converged | small gradient, small movement, small loss improvement | the walk is at a minimum to within the tolerance |
| the budget ran out | the pass limit was reached while the numbers were still moving | nothing about convergence; the numbers are where it happened to be |
| it was diverging | loss rising pass after pass, parameters and gradient growing | the rate is too large for this surface |
| the arithmetic failed | a NaN or an infinity appeared | the computation cannot continue |
The verdicts in section 16’s table draw these lines exactly where they fall. At 0.07 and 0.1 the walk is doomed and has not yet overflowed, so it runs out of its 500 passes holding numbers that are large and useless, and the verdict is that the passes ran out. Only at 0.2 do the coefficients stop being finite inside the budget, and only then is the fit refused by name.
Non-finite arithmetic detects the wreck, not the beginning of the skid, which can be many passes earlier. A run should be read by its loss and gradient over time, which is the dashboard’s middle row, and not by whether it happened to crash.
28. Honest stopping criteria
The walk stops when no coefficient moved more than 10⁻⁸ in a whole pass. That is a movement tolerance, and a movement is the rate times the gradient.
Δθ = −η·∇LSo a tiny movement can mean the gradient is genuinely small, which is what the test is for. It can also mean the rate is extremely small, so that a large gradient produces a tiny step and the walk declares itself finished while standing halfway down the hill. And it depends on the scale of the parameters, since 10⁻⁸ is a different demand on a level of 66 than on a coefficient of 0.0004.
In a model
Keeping a movement tolerance is fine. What makes it honest is exposing the evidence alongside it, which the dashboard does. The movement, the gradient size and the loss change at every pass, the pass limit, and the reason the run stopped. With those in view a reader can tell a walk that arrived from a walk that gave up, and on the pages that follow, where no closed form waits at the bottom to check against, those instruments are the only way to know.
Check yourself
Questions on Parts 8 and 9
Question 1 of 5
On raw heights a rate of 0.02 runs away in four passes and the largest safe rate is 0.0000346. What has gone wrong?
Try it with Botie
Build with Botie
Write and run Python