Gradient LabStart the lesson

Lessons › Chapter 2: Math toolkit

Derivatives and slopes

Here is f(x) = x², drawn as a row of bars. The middle bar is x = 0. Lower bars are smaller outputs.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

Which way is better?

Maya asks a sharp question: if we nudge one of the model’s numbers, does the error go up or down? That is exactly what a slope tells you.

Concept

A curve

Here is f(x) = x², drawn as a row of bars. The middle bar is x = 0. Lower bars are smaller outputs.

Concept

Slope at a point

Put a ruler against the curve at one point. Its tilt is the slope, also called the derivative. At x = 2 the curve is climbing fast, so the slope is 4.

Build

Negative slope

At x = −2 the curve goes down as x increases, so the slope is −4.

Key idea

Zero slope at the bottom

At the very bottom the tangent is flat: slope 0. Finding where the slope is zero is how we find a minimum. Training a model means finding the minimum of its error.

Insight

Go opposite the slope

If the slope is positive, stepping left lowers f. If negative, step right. Always moving against the slope walks you downhill. That is gradient descent in one sentence.

Build

Many inputs: the gradient

Real models have many knobs to turn, not just one. With two knobs the curve becomes a bowl. The slope in every direction together is the gradient. It points uphill, so we walk the opposite way.

Real life

In real life: your speedometer

A car’s speedometer shows a derivative: how fast the distance travelled is changing right now. On a distance-over-time chart, a steeper slope means a faster car. Flat means stopped.

Quick check: The slope at your point is positive. Which way lowers f?

A positive slope means f rises to the right, so moving left goes downhill.

Pseudocode

f(x) = x × x
# a U-shaped curve, lowest at x = 0

slope(x) = how much f changes
           for a tiny step to the right

slope at x = 2    →  4    (going up)
slope at x = -2   → -4    (going down)
slope at x = 0    →  0    (flat: the bottom)

Key terms

slope
How steep something is. Positive goes up to the right, negative goes down.
derivative
The slope of a curve at one point: how fast it goes up or down.
gradient
The slope in every direction at once. It points uphill.

Build it: Gradient descent from scratch (30 min, runs in your browser)

See the project