Lesson 2 of 86 min
The derivative
A derivative is the slope of the loss at one point, found by a tiny nudge: its sign says which way to turn a weight, and its size how much the loss changes when you turn it.
After this lesson you can read a derivative as a slope and as a sensitivity.
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: dL/dw
Papers and docs write the derivative of the loss L as you turn w as dL/dw, said "the derivative of the loss with respect to w". PyTorch can also hand it to you without filling w.grad: torch.autograd.grad takes the loss and the weight. x, mass and loss are the video's.
w = torch.tensor(0.0, requires_grad=True)
print(torch.autograd.grad(loss(w), w))(tensor(-1.9595),)
`.grad` adds up: zero it first
Call backward() a second time and w.grad does not start again: the new derivative is added to the old one, so you get twice the number. A training loop clears it before every step, with w.grad.zero_() here, or optimizer.zero_grad() when an optimizer holds the weights.
w = torch.tensor(0.0, requires_grad=True)
loss(w).backward()
print(w.grad)
loss(w).backward()
print(w.grad)
w.grad.zero_()
loss(w).backward()
print(w.grad)tensor(-1.9595) tensor(-3.9189) tensor(-1.9595)
Older code: `Variable`
Tutorials from a few years ago wrap a tensor in Variable before asking for its derivative. You don't need it: PyTorch's docs say the Variable API is deprecated, and a tensor with requires_grad=True is all it takes. Old code still runs, because Variable now just gives back a tensor.
from torch.autograd import Variable
v = Variable(torch.tensor(0.0), requires_grad=True)
print(type(v))<class 'torch.Tensor'>
A nudge on both sides
The video's nudge went one way, from 0 up to 0.01, and gave −1.94. Nudge both ways, down to −0.01 and up to 0.01, and divide by the whole width. That is much closer to the derivative. People use it to check a derivative that code worked out.
h = 0.01
print((loss(h) - loss(0.0)) / h)
print((loss(h) - loss(-h)) / (2 * h))tensor(-1.9397) tensor(-1.9595)
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: Functions and loss
A line gives a penguin with a 20 cm flipper 4 kg, and adds half a kilo for each centimeter more. A penguin's flipper is 22 cm, and it weighs 5.5 kg. What is the line's miss for it?
2 of 3
At w = 0, after loss.backward(), PyTorch prints w.grad as tensor(-1.9595). What does that tell you?
3 of 3
You nudge a weight from 3 to 3.01, and the loss goes from 5.00 to 4.97. About what is the derivative there?
The ideas in this lesson
- Derivative
- The slope of a curve at one point: how fast the loss changes as you nudge a weight.
- Reading a derivative
- Its sign says which way to turn a weight, and its size how much that changes the loss; zero at the bottom.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
A puzzle about w
Here's a puzzle. A line makes a guess for each bird. Its weight w turns it. At a w of zero, it's flat. Now turn w all the way up to one. The loss is almost the same. So does turning w change the loss much at all?
A tiny nudge
Here's the flat line again, with the line's other weight, b, at four point two.
And here's the loss for every w, with b held there.
From zero, it goes down to a bottom near a half, then comes back up. We want the loss low. So nudge w. Turn it just a tiny bit, from zero up to a tenth.
The line tilts a little... and the loss falls, from zero point six four to zero point four six five.
So here, at zero, turning w up lowers the loss.
A rate
How fast does it fall? The loss fell by zero point one seven six, while w moved a tenth. Divide the change by the nudge, and you get a rate: minus one point seven six.
It's rise over run, like how steep a road is. Here, it's how steep the loss is at a w of zero. Not how much the penguins' line tilts.
So the loss falls one point seven six times as fast as w rises.
A big nudge
So why not nudge it all the way to one, like at the start? What rate will that give?
Plus zero point zero two. Nearly flat... even a little uphill. But at zero, the curve goes down. The big nudge went down past the bottom, and back up the other side. It's like a road that goes down into a valley and back up: its two ends are at about the same height. On a curve that bends, a big nudge measures the wrong thing.
Smaller nudges
So make the nudge smaller. A nudge of a tenth gives minus one point seven six. A hundredth gives minus one point nine four. A thousandth gives minus one point nine six. The rates stop changing. They settle on one number, about minus two.
The derivative
Why do they settle? Zoom in at zero. The closer you look, the straighter the curve gets... until it's a straight line, with a slope of minus one point nine six.
A road is steep over a stretch. This curve bends, so you read its slope at one point. The slope of the curve at one point is called its derivative.
The answer
So at zero, the derivative is about minus two.
Turn w up a little, and the loss falls about twice as much as you turned it.
Turning w changes the loss a lot there.
If you said "hardly at all", that was the turn to one. It went down, and came back up.
Reading it
So there's a derivative at every w: one number. What does that number tell you to do?
Its sign
A derivative is a slope at one point, and it has a sign: plus or minus. When the derivative is below zero, the loss falls as w rises, so turn w up.
When the derivative is above zero, turn w down.
At a w of one, which way?
Plus two. The loss rises with w... so turn w down.
Its size
And its size says how much a small turn of w changes the loss. At zero, about minus two. At a quarter, about minus one. At a half, nearly zero. The farther the derivative is from zero, the more a small turn changes the loss. At the bottom, it's zero. The curve is flat there, and turning w either way only raises the loss. A derivative of zero... is how you know you're at the bottom.
In code
Now in real code. Here are the penguins as PyTorch tensors. This function works out the loss for any w. Nudge w by a hundredth, and divide the change in the loss by that hundredth. You get minus one point nine four.
Now PyTorch's own way. Make w with requires grad set to true. That asks PyTorch to follow everything done with w. Then call backward, which works out the derivative. What will w dot grad hold?
Minus one point nine six. The derivative itself, with no nudge. So in real code, you don't nudge: backward gives you the derivative.
Learning needs this number for every weight, at every step... and PyTorch works it out.
The slope at one point
A derivative is the slope of the loss at one point. Its sign says which way to turn a weight. Its size says how much a turn changes the loss.
Which way to turn
So a derivative tells you which way to turn a weight. Now try it on this lesson's page, and take the quiz. Make a big turn give the wrong sign.
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 .
