Bitpath

Lesson 2 · Unit 02

Autograd

Let PyTorch do the calculus, and learn its three habits

13 exercisesabout 3 min

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?

Python
import torch

x = torch.tensor(3.0, requires_grad=True)
y = x ** 2
y.backward()
print(x.grad.item())

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.

Python
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.

One email when Bitpath is ready to try. Nothing else. Privacy