Lesson 3 of 86 min
Partial derivatives and the gradient
The gradient is every weight's slope written as one arrow: it points the steepest way up the loss, and it is longer where the loss is steeper.
After this lesson you can find the uphill direction of a function with many inputs.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
How it is written: ∂L/∂w and ∇L
Papers write a partial derivative with a curly d: ∂L/∂w is the slope of the loss L along w, with b held still. The gradient is written ∇L, said "grad L", the list of all of them. PyTorch can hand you that list without filling .grad: give torch.autograd.grad the loss and both weights. x and mass are the video's.
w = torch.tensor(0.0, requires_grad=True)
b = torch.tensor(0.0, requires_grad=True)
loss = ((w * x + b - mass) ** 2).mean()
print(torch.autograd.grad(loss, (w, b)))(tensor(-2.7282), tensor(-8.4035))
Why the arrow is the steepest way
Back on the hillside. A step of length 1 at an angle from east goes part east and part north, so it lifts you part of 3 and part of 4. Try a few angles: the lift is largest at about 53 degrees, which is exactly where the arrow [3, 4] points, and it is 5, the arrow's length.
import math
for degrees in [0, 30, 53.13, 70, 90]:
a = math.radians(degrees)
print(degrees, round(3 * math.cos(a) + 4 * math.sin(a), 2))0 3.0 30 4.6 53.13 5.0 70 4.78 90 4.0
A slope for every weight, in its weight's shape
A real layer keeps its weights in a grid and its added numbers in a list. After backward(), each one's .grad has exactly its shape, one slope in each place. The video counted the grid alone; with the 3,072 added numbers, the layer has 2,362,368 slopes.
layer = torch.nn.Linear(768, 3072)
layer(torch.randn(768)).sum().backward()
for name, p in layer.named_parameters():
print(name, p.shape, p.grad.shape)
print(sum(p.numel() for p in layer.parameters()))weight torch.Size([3072, 768]) torch.Size([3072, 768]) bias torch.Size([3072]) torch.Size([3072]) 2362368
The arrow's length: the grad norm
Training logs often print a "grad norm". It is the arrow's length, over every weight at once. torch.linalg.vector_norm works it out, and clip_grad_norm_ gives it back too, after it shrinks the arrow to the length you allow. Shrinking keeps one huge step from throwing a model off.
loss = ((w * x + b - mass) ** 2).mean()
loss.backward()
print(torch.linalg.vector_norm(torch.stack([w.grad, b.grad])))
print(torch.nn.utils.clip_grad_norm_([w, b], max_norm=1.0))
print(w.grad, b.grad)tensor(8.8353) tensor(8.8353) tensor(-0.3088) tensor(-0.9511)
Run on Python 3.13, PyTorch 2.15.0, pandas 3.1.1 and scikit-learn 1.9.1. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: Vectors
A walk is written east first, then north: [3, 2] is three blocks east and two north. Where does the walk [1, -2] end?
2 of 3
At some weights, the slope along w is 3 and the slope along b is −1. Which way lowers the loss the fastest?
3 of 3
At one point on the bowl, the slope along w is 6 and the slope along b is 8. How long is the gradient's arrow there?
The ideas in this lesson
- Partial derivative
- The slope along one weight, while every other weight is held still.
- Gradient
- Every weight's slope in one list: an arrow that points the steepest way up, longer where it's steeper.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
A hillside
Here's a question. You're standing on the side of a hill. Every step you take is the same length. A step east lifts you three. A step north lifts you four. So which way takes you up the fastest? North, or somewhere in between?
A bowl
Back to a line that guesses a penguin's mass. It has two weights, the two numbers it can change: w tilts it, and b lifts it.
Every pair of weights gives a loss, the number for how wrong the line is. So put the two weights on the floor, one in each direction... and the loss as the height. You get a bowl. Start where a model knows nothing: both weights at zero. It guesses zero kilos for every penguin. The loss is eighteen point three, high up on the wall. Unlike a hillside, nobody can see a real model's bowl. It's worked out, one point at a time.
A slope along each weight
So how do you find a slope on a bowl?
Hold b still, at zero, and nudge w. Along w alone, the loss is a curve. Here it falls two point seven three for each unit of w: a slope of minus two point seven three. Now hold w still, and nudge b. Minus eight point four.
A slope along one weight, with every other weight held still, is called a partial derivative.
Both below zero
Both slopes are below zero, so turning either weight up lowers the loss. And b's slope is about three times w's. The line guesses zero kilos, and penguins weigh about four. b lifts every guess at once.
One way
Two slopes. But you can only go one way. So which way is up?
Back to the hillside.
The gradient
Write the two slopes as one list, in order: east first, then north. Three, four.
A list of numbers in order is a vector, so draw it as an arrow.
That arrow points between east and north, leaning north. It's five long. A step of the same length that way lifts you five centimeters... steeper than either.
If you said north: the arrow does lean north, toward the steeper slope. But east counts too, so the steepest way is in between.
The bowl works the same way. Its arrow is its two slopes: minus two point seven three along w, and minus eight point four along b.
Both are below zero, so it points toward smaller weights... up the wall. It points the steepest way up the bowl.
This arrow is called the gradient.
It points up
So take a small step along the arrow. Will the loss go up, or down?
Up. From eighteen point three to nineteen point one. The gradient points uphill, toward a higher loss. To go down, you go the other way. Many people think the gradient points downhill. It points up.
Its length
And its length is how steep the bowl is that way. At zero, zero, it's about eight point eight. That's steeper than either slope alone, like on the hillside. Move the point to b equals three, and the arrow is shorter: about three point two.
At w equals a half and b equals four point one six, where the line runs through the penguins, it's almost nothing. At the bottom, it's zero. The bowl is flat there, so there's no steepest way.
One slope for every weight
A real model has millions of weights. Take one layer the size of one in GPT-2. Nearly eight hundred numbers go in, and about three thousand come out. It has about two point four million weights.
Its gradient has one slope for each: a list of two point four million numbers.
One arrow, in that many dimensions.
Nobody can draw it. But it works the same way: it points the steepest way up the loss.
In code
Now in real code. Both weights are PyTorch tensors, each with requires grad set to true, like with one weight.
Work out the loss, call backward once... and both slopes are filled in.
Minus two point seven three, and minus eight point four.
Now that layer of GPT-2's size.
We give it a made-up loss, just its outputs added up, and call backward once.
How many slopes does its weight get?
About two point four million. One for every weight, all at once.
Finding them with nudges would take that many runs.
Every slope, one arrow
A partial derivative is the slope along one weight. The gradient is all of them, as one arrow. It points the steepest way up, and it's longer where it's steeper.
One arrow
So the gradient is one arrow that points the fastest way up. Now try it on this lesson's page, and take the quiz. Find a spot where the arrow points along w alone.
You use this lesson in Lab 1: Find the Gentoos' middle, by walking downhill, after Gradient descent and the learning rate.
Facts last checked on .
