Skip to lesson content
← All algorithms

Lesson 04 · lab

How Gradient Descent Learns

Step through training and connect each parameter movement to a change in dataset MSE.

Equation X-Ray · Read the structure, then explain it

Mathematics as a language

Read a prediction before adjusting it

Estimate a house price from area without confusing the estimate with its observed sale price.

ŷ = wx + b

Read the relationship before trying to solve it. This is an illustrative example, separate from the live experiment.

Choose a building block to inspect its role.

Read the operations: At x=2.5, multiply 42 × 2.5 = 105, then add 140 to get 245.

Build the expression from smaller operations

Choose a valid construction in the stated order. This exercise organizes dependencies; it does not claim every implementation must evaluate independent branches in this order.

    What problem could this relationship represent?

    A rate plus a baseline also describes many non-housing relationships.

    Context we still need: A model relationship does not prove that changing area causes a price change in the world.

    Connect the operations to code
    prediction = coefficient * area + baseline

    Read the code as a map of operations. It is not executed here; imports, shapes and numerical safeguards depend on the actual lesson.

    Explain a changed situation

    Review the five-level equation-reading journey →

    Your notes and reveals are temporary. This reading practice does not change lesson completion or saved assessment evidence.

    The full story · about 14 minutes

    Finding the line by walking downhill

    1 · Observe

    Guess, check, nudge

    Words used in this lesson
    WordWhat it means
    LossOne number saying how wrong the model is right now. Here the mean squared error.
    GradientThe slope of the loss: it says which way, and how steeply, the loss changes if you nudge a weight.
    Gradient descentRepeatedly nudging the weights against the slope so the loss falls.
    Learning rate (η)The size of each step. Too small is slow; too big overshoots.
    StandardiseRescale a feature to average 0 and spread 1, so one learning rate suits every feature.

    We could solve for the best line with algebra. Most models in this course cannot be solved that way, so we use a method that works everywhere: start with a guess, see which direction lowers the error, step that way, and repeat. This is gradient descent.

    2 · Express

    The update

    For the mean squared error, the slope of the error with respect to each parameter is the average of (prediction − actual) times the input, and for the bias, the average of (prediction − actual). Move each parameter against its slope, scaled by the learning rate η.

    w ← w − η · mean[(ŷ − y) · x]
    b ← b − η · mean[ŷ − y]
    Standardising x first (mean 0, spread 1) lets one learning rate work for both parameters.

    3 · Visualize

    Five learning rates

    Same eight flats, area standardised, starting from w = 0 and b = 0. The best possible MSE is 21.0.

    Mean squared error by learning rate
    Learning rate ηAfter 10 stepsAfter 200 steps
    0.012705.379.9
    0.1503.821.0
    0.521.021.0
    1.021.021.0
    2.117905.3diverged

    A tiny rate is safe but slow: after 200 steps it has not arrived. Rates of 0.5 and 1.0 arrive in about ten steps. At 2.1 each step overshoots the valley by more than it gained, and the error explodes.

    Loss against steps, for three learning rates
    01,0002,0003,0000102030StepMean squared error
    • η = 0.01
    • η = 0.1
    • η = 0.5
    See the numbers behind this picture
    Loss against steps, for three learning rates
    Stepη = 0.01η = 0.1η = 0.5
    03,237.6253,237.6253,237.625
    13,173.6132,626.46825.131
    23,110.8762,131.416222.007
    33,049.3871,730.4371.226
    42,989.1211,405.63233.531
    52,930.0551,142.54524.107
    62,872.164929.44521.751
    72,815.425756.83421.162
    82,759.815617.01921.015
    92,705.312503.76920.978
    102,651.894412.03620.969
    112,599.538337.73320.967
    122,548.225277.54720.966
    132,497.932228.79720.966
    142,448.641189.30920.966
    152,400.33157.32420.966
    162,352.98131.41620.966
    172,306.573110.4320.966
    182,261.0993.43220.966
    192,216.51179.66320.966
    202,172.8268.51120.966
    212,129.99859.47720.966
    222,088.02852.1620.966
    232,046.89446.23320.966
    242,006.57841.43220.966
    251,967.06437.54420.966
    261,928.33734.39420.966
    271,890.3831.84320.966
    281,853.17929.77620.966
    291,816.71828.10220.966
    301,780.98226.74620.966
    311,745.95825.64820.966
    321,711.63124.75820.966
    331,677.98624.03820.966
    341,645.01223.45420.966
    351,612.69322.98120.966
    361,581.01822.59820.966
    371,549.97322.28820.966
    381,519.54522.03720.966
    391,489.72421.83320.966

    4 · Discover

    It lands where the algebra does

    With η = 0.5 the walk ends at 0.806 × area − 6.82, the same line as the exact solution, after converting from standardised back to raw units. Gradient descent is not a different model. It is a different way to find the same one.

    5 · Apply

    Two worked examples

    Example 1, a step. At w = 0, b = 0 with standardised x, the average of (ŷ − y) is −53.6, the negative of the average price. With η = 0.5 the bias jumps from 0 to 26.8 in one step.

    Example 2, a stop rule. Stop when the error barely changes, for example by less than 0.001 between steps, or after a fixed budget. Plot the error against the step number: it should fall smoothly.

    Check yourself

    The error rises every step. What is the most likely cause?

    6 · Where it goes wrong

    Three ways it goes wrong

    7 · In Python

    Gradient descent in code

    Here is how to implement it, step by step. The code below does exactly these steps, and every line is editable.

    1. Standardise the area, so one learning rate works.
    2. Start with w = 0 and b = 0, so the model predicts 0 for every flat.
    3. Compute each flat's miss: prediction minus actual.
    4. Update w by minus the learning rate times the average of (miss × input), and b by minus the learning rate times the average miss.
    5. Repeat for many steps while watching the loss fall.
    Five learning rates, and the line gradient descent finds
    import numpy as np
    
    area = np.array([40, 50, 60, 70, 80, 90, 100, 110], float)
    price = np.array([30, 34, 36, 52, 55, 58, 80, 84], float)
    w_exact, b_exact = np.polyfit(area, price, 1)
    
    x = (area - area.mean()) / area.std()           # standardise so one learning rate suits both
    def run(lr, steps=200):
        w = b = 0.0
        hist = []
        for s in range(steps):
            err = (w * x + b) - price
            w -= lr * (err * x).mean()
            b -= lr * err.mean()
            hist.append(float((err ** 2).mean()))
        return w, b, hist
    for lr in [0.01, 0.1, 0.5, 1.0, 2.1]:
        w, b, h = run(lr)
        print("lr", str(lr).ljust(4), "| loss after 10 steps", round(h[9], 1), "| after 200 steps", "diverged" if h[-1] > 1e6 else round(h[-1], 1))
    w, b, h = run(0.5)
    slope = w / area.std()
    intercept = b - w * area.mean() / area.std()
    print("gradient descent line: %.3f x area + %.2f" % (slope, intercept), "| exact: %.3f x area + %.2f" % (w_exact, b_exact))
    lr 0.01 | loss after 10 steps 2705.3 | after 200 steps 79.9
    lr 0.1  | loss after 10 steps 503.8 | after 200 steps 21.0
    lr 0.5  | loss after 10 steps 21.0 | after 200 steps 21.0
    lr 1.0  | loss after 10 steps 21.0 | after 200 steps 21.0
    lr 2.1  | loss after 10 steps 17905.3 | after 200 steps diverged
    gradient descent line: 0.806 x area + -6.82 | exact: 0.806 x area + -6.82
    Edit and run this code in your browser

    Live Python

    Five learning rates, and the line gradient descent finds

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    Want to implement it? Open the step-by-step playbook · How to choose a model

    The big idea

    Lost on a foggy hill

    Imagine standing on a hillside at night, in thick fog. You cannot see the valley, but you can feel the ground tilt under your feet. So you do the only sensible thing: take a small step in the direction that goes downhill, feel the slope again, and repeat.

    That is how a machine learns. The hill is the loss: for every possible setting of the line (a slope w and a baseline b), the height tells us how wrong the predictions are. The valley floor is the best line. The slope under your feet is the gradient, which calculus gives us exactly. No machine ever sees the whole hill, only the slope where it stands, and that is enough.

    Predict before you read on

    Which way is downhill?

    Our line starts at w = 10, b = 100. At that spot the slope of the hill is −892.5 in the w direction. It is negative: loss goes down as w goes up. Which step should we take?

    Problem and motivation

    A training run produces poor estimates. We need a repeatable way to adjust both parameters and diagnose whether learning is working.

    Build the intuition

    Start with one house: a prediction below its recorded price has a negative residual. An update changes the rule, not the home. Inspect one update in the housing lab before reading the derivative formulas below. Loss is a height on a landscape of parameter settings. A derivative measures the local slope in one parameter direction. The gradient collects these slopes; descent takes a small step downhill.

    What you will learn

    Connect derivatives to a training decision

    • Explain why descent moves opposite the gradient.
    • Trace a simultaneous parameter update numerically.
    • Diagnose learning-rate instability from a sequence of losses.
    • Distinguish optimization evidence from generalization.
    Need a quick concept reminder?

    Guided experiment

    Predict the update direction before pressing Step. Inspect iteration 0 and iteration 1 in the table. Compare the small, moderate and excessive presets from the same initial state. Describe the loss evidence before labelling the run.

    Change a lab parameter or inspect another training state or diagnostic scenario, then answer the checks.

    Real-world question

    How do repeated gradient updates move a prediction line toward the data?

    Predict before experimenting

    Which learning rate is most likely to overshoot on this dataset?

    Iteration
    0
    0 is the initial state
    Weight
    10
    Bias
    100
    MSE
    20,163.8333
    Weight gradient
    -892.5
    Status
    running
    floor area (thousand ft²)price (₹ lakh)
    Actual observation Predicted point Regression line
    Loss by parameter state · select any point to inspect it
    Training history from the mathematical runtime
    IterationWeightBiasMSEWeight gradientBias gradientStatus
    01010020,163.8333-892.5-269running
    118.925102.6912,413.8473-699.1719-212.1525running
    225.9167104.81157,652.3527-547.6394-167.5905running
    331.3931106.48744,726.8912-428.867-132.6582running
    435.6818107.8142,929.4299-335.7725-105.2737running
    539.0395108.86671,824.9762-262.8046-83.8053running
    641.6676109.70481,146.2848-205.6124-66.9742running
    743.7237110.3745729.1689-160.7854-53.7777running
    845.3315110.9123472.7575-125.6504-43.4302running
    946.588111.3466315.0784-98.1122-35.3157running
    1047.5692111.6998218.0584-76.5284-28.9516running
    1148.3344111.9893158.306-59.6119-23.9595running
    1248.9306112.2289121.4507-46.3535-20.0427running
    1349.3941112.429398.6635-35.9626-16.9687running
    1449.7537112.59984.5201-27.8191-14.5555running
    1550.0319112.744675.6881-21.4372-12.6602running
    1650.2463112.871270.1201-16.4362-11.1708running
    1750.4106112.982966.5582-12.5173-9.9995running
    1850.5358113.082964.2297-9.4468-9.0777running
    1950.6303113.173662.6598-7.0412-8.3514running
    2050.7007113.257261.5569-5.1568-7.7783completed

    Explain the observation

    Describe only what the trace demonstrates.

    Loss decreased across the inspected states, which shows optimization progress. This short trace alone does not establish convergence or generalization.

    Each point is a complete parameter state. Iteration 0 is before any update; iteration 0 is after 0 successful updates.

    Apply it

    Compare small, moderate, and excessive rates.

    Run each preset from iteration 0. Find one smooth decrease and one sequence that increases, oscillates, or terminates. Use the loss chart and runtime status as evidence.

    Mathematical reasoning

    L = (1/m) Σ rᵢ² is mean squared error over m observations, with rᵢ = ŷᵢ − yᵢ. Squaring stops positive and negative errors cancelling, and emphasizes large misses. A loss function turns many prediction errors into one optimization target.

    For one feature, ∂L/∂w = (2/m) Σ rᵢxᵢ and ∂L/∂b = (2/m) Σ rᵢ. The chain rule supplies the feature factor for the weight derivative; the derivative of a prediction with respect to bias is 1. Gradients describe sensitivity of the entire dataset loss, not one selected residual.

    A positive derivative means increasing that parameter locally increases loss. The gradient points in the direction of steepest local increase under the usual Euclidean distance. Hence w_new = w − α∂L/∂w and b_new = b − α∂L/∂b. Both derivatives must be computed from the same pre-update state. A negative derivative makes the subtraction increase the parameter.

    Separate small-number worked example (not the housing lab settings): for inputs [1,2], targets [3,5], w = 1, b = 0: predictions [1,2], residuals [−2,−3], weight gradient −8, bias gradient −5. With learning rate α = 0.1 the next state is w = 1.8, b = 0.5. The runtime computes the exercise answer from this training trace.

    Learning rate multiplies the update, not the gradient itself. A small rate can make slow progress; a large rate can overshoot, oscillate, increase loss, or eventually produce numerical divergence. A single increase is not proof of divergence. For this convex squared-error linear model an appropriate step can approach a minimum, but a short decreasing trace does not prove convergence or useful unseen predictions. Compare changes over many iterations, finite gradients and held-out error. Differently scaled features can make one shared rate awkward.

    Mathematical symbol key

    L, m, Σ, i
    Dataset loss, observation count, sum, and observation index
    rᵢ, xᵢ, yᵢ, ŷᵢ
    Residual, input, observed and predicted target for row i
    ∂, ∇L
    Partial derivative operator and vector of loss derivatives
    w, b, α
    Weight, bias and positive learning rate
    new
    Parameter after one simultaneous update

    Real-world application

    An energy-demand training job shows exploding loss after a rate change. Reduce the rate, inspect feature scales fitted on training data, and check finite updates. Select model settings using validation data; reserve test data for final evaluation.

    Guided exercises and misconception checks

    Explain the consequence of a change or choose a defensible action. Each check can be retried after the worked feedback. Successful checks remain recorded when you return.

    1. The weight derivative is positive. Which small update locally moves downhill?
    2. For the worked two-row example with w = 1, dw = −8 and α = 0.1, what is the next weight?
    3. Loss goes 100, 40, 60, 150 after increasing the rate. What should you do first?
    4. Training MSE falls steadily. What can you conclude?

    Now you drive

    Code gradient descent yourself

    The lab above ran the algorithm for you. Now write its heart: three lines of Python. The equations from this lesson become code almost word for word:

    Maths and Python, side by side
    MathsPythonIn plain words
    rᵢ = ŷᵢ − yᵢr = predict(w, b, xi) - yiHow wrong is row i? (prediction minus actual)
    ∂L/∂w = (2/m) Σ rᵢ xᵢdw += 2 / m * r * xiThe Σ is the loop; each row adds its share. Steeper where the input xᵢ is large.
    ∂L/∂b = (2/m) Σ rᵢdb += 2 / m * rSame, without xᵢ: the baseline multiplies 1.
    w ← w − α ∂L/∂ww = w - lr * dwStep against the slope. α is lr, the size of the step.

    Fill in TODO 1, TODO 2 and TODO 3, then press Check my code. Before you finish, the program already runs; with the TODOs empty, the line just never moves. Notice what the loss curve does.

    Live Python

    Gradient descent on the housing data

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Quick experiments:

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    Stuck? Show a worked solution (try first!)
    # ── Settings you can change ────────────────────────────
    lr = 0.01            # learning rate: how big each downhill step is
    steps = 20           # how many steps to take
    w0, b0 = 10.0, 100.0 # starting guess:  price = w × area + b
    
    # ── The data: area (thousand ft²) → price (₹ lakh) ──
    X = [1.0, 1.8, 2.5, 3.2, 4.0, 4.8]
    y = [178, 213, 246, 278, 316, 349]
    m = len(X)
    
    def predict(w, b, x):
        return w * x + b
    
    def loss(w, b):
        # mean squared error: the height of the hill at (w, b)
        return sum((predict(w, b, xi) - yi) ** 2 for xi, yi in zip(X, y)) / m
    
    def gradients(w, b):
        dw, db = 0.0, 0.0
        for xi, yi in zip(X, y):
            r = predict(w, b, xi) - yi    # residual: prediction − actual
            dw += 2 / m * r * xi              # (2/m) · rᵢ · xᵢ
            db += 2 / m * r                   # (2/m) · rᵢ
        return dw, db
    
    w, b = w0, b0
    history = [loss(w, b)]                # loss before any step
    for step in range(steps):
        dw, db = gradients(w, b)
        w = w - lr * dw                   # w ← w − α · ∂L/∂w
        b = b - lr * db                   # b ← b − α · ∂L/∂b
        history.append(loss(w, b))
    
    print(f"start:  w = {w0:.2f}   b = {b0:.2f}   loss = {history[0]:.1f}")
    print(f"after {steps} steps:  w = {w:.2f}   b = {b:.2f}   loss = {history[-1]:.1f}")
    

    Experiment, then explain

    Finish the three TODOs, then use the quick experiments (or edit lr and steps yourself). Think about each question before opening its answer.

    With lr = 0.001 the loss barely moves. Is the code broken?

    No. The same correct code takes tiny steps, so after 20 of them the loss has only fallen from 20,164 to about 8,451 and w is still near 25. Try steps = 500: small steps are safe, just slow.

    At lr = 0.05 the loss drops from 20,164 to about 198 in one step. Why does lr = 0.1 blow up?

    At 0.05 the first step already overshoots the valley floor (the slope flips from negative to positive), but each later step lands closer. At 0.1 every overshoot is bigger than the last, so the loss grows instead of shrinking: after 20 steps it is about 8 million. The step size has to be small enough for each correction to shrink the error.

    Why is the slope for b (−269) smaller than for w (−892.5)?

    Each row’s share of ∂L/∂w is multiplied by its area xᵢ (up to 4.8), but ∂L/∂b is multiplied by 1. A feature measured in larger numbers has a steeper slope, which is why the next lessons talk about scaling features.

    Build the evidence

    0 of 4 checks complete. Experiment interaction still required.

    Completion records these objectives locally; it does not award mastery. Independent mastery will require unfamiliar examples and critical-objective evidence in the later assessment.

    Explore the book’s separate housing example

    Move the settings toward smaller error

    For observations (1,2), (2,3), (3,5), a least-squares line has slope 1.5 and intercept 1/3. A formula and an iterative search can solve the same objective under suitable conditions.

    The playground uses a separate dataset. Compare with the illustrated example ↓

    Illustration for Move the settings toward smaller error. The reading guide immediately below explains its parts and conclusion.
    Read the illustration: The horizontal axis varies slope while the intercept is fixed at the book’s chosen value. The vertical axis is training MSE. The bottom is the best slope on this slice, not a complete picture of every parameter combination.

    Source: Linear Regression · Book 8, PDF pages 8–10. Static source illustration; annotated scores belong to this example.

    Open full-size figure for Move the settings toward smaller error (opens a new tab)↗
    Move the settings toward smaller error: compare the quantities
    MethodWhat it does
    Least squaresSolve the linear fit numerically
    Gradient descentRepeat local updates
    ValidationCheck usefulness on unseen cases

    w_new = w_old − learning_rate × loss_gradient

    A gradient is a local sensitivity. Subtracting it proposes a downhill step; the learning rate scales that step.

    Return to the playground and explain how its inputs connect to this relationship. Unscored reading.