Lesson 2 · Unit 02
Autograd
Let PyTorch do the calculus, and learn its three habits
What you practise
requires_grad,backward(), reading.grad, gradients accumulate until zeroed,torch.no_grad(),detach
Try a question from this lesson
Pick one
The slope of y = x² at x = 3 is 6. Nobody typed that derivative in here. What does this print?
import torch
x = torch.tensor(3.0, requires_grad=True)
y = x ** 2
y.backward()
print(x.grad.item())Correct answer
6.0
Why
PyTorch recorded that y was made by squaring x, and backward() used that record to compute dy/dx = 2x = 6 and store it in x.grad. 9.0 is the value of y, not its slope.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Record, then rewind
With requires_grad=True, every operation on a tensor is recorded in a graph.
backward() walks that graph from the result back to the inputs, applying the chain rule at each step, and leaves each input's gradient in its .grad. This is backpropagation, done for you.
x = torch.tensor(3.0, requires_grad=True)
y = x ** 2 # recorded: y came from x
y.backward() # rewind: compute dy/dx
print(x.grad) # tensor(6.)11 more exercises in the app
- 4× type the answer
- 3× multiple choice
- 1× fill the blanks
- 1× true or false
- 1× order the lines
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.