PythiloLesson 2 of 18 · progress kept in this browser

Autograd

Training means changing numbers so that a loss gets smaller, and for that you need the slope of the loss with respect to each number. PyTorch works the slopes out for you.

Mark a tensor with requires_grad=True, compute something from it, call .backward() on the result, and the slope lands in .grad:

import torch

x = torch.tensor(3.0, requires_grad=True)
y = x ** 2 + 2 * x
y.backward()
print(x.grad)  # 2x + 2 at x = 3: tensor(8.)

backward() needs a single number. For a function of several inputs, every input that requires a gradient gets its own .grad.

Your task

Write slope(f, x): the derivative of the one-argument function f at the float x, as a float, computed with autograd. Write gradients(w, b, x, y): for the loss ((w * x + b - y) ** 2).mean() over tensors x and y, return the pair of floats (dloss/dw, dloss/db).

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.