PyTorch, from first principles
PyTorch tensors support shapes and broadcasting much like NumPy arrays. PyTorch can also record operations to calculate derivatives automatically. This is autograd. We’ll use it to fit a line by repeatedly calculating a loss, finding its gradient, and updating the parameters.
Data, device, and gradients
Section titled “Data, device, and gradients”A tensor’s device identifies where its data lives, such as CPU or GPU memory. In ordinary gradient mode, differentiable operations involving an input with requires_grad=True are recorded for backward calculation. no_grad and inference mode disable this recording.
Why remember? Because slopes multiply
Section titled “Why remember? Because slopes multiply”Recall what the math book ended on: learning is gradient descent — every parameter needs its slope on the loss, and the chain rule says slopes multiply through composed steps. A loss is always a composition: multiply, add, activate, compare. So to get every slope, you need to know which steps made the loss, in what order.
The computational graph records these dependencies. Autograd uses them to calculate derivatives:
- Forward: calculate the loss and record the operations needed for differentiation.
loss.backward(): apply the chain rule backward, multiplying along paths and adding where they meet. Parameter gradients accumulate in.grad.
The training loop
Section titled “The training loop”An optimizer updates the model’s parameters using their gradients. Basic gradient descent uses w ← w − lr·grad; other optimizers use different update rules. Here is the loop for this regression example:
for epoch in range(200): pred = model(x) loss = F.mse_loss(pred, y) opt.zero_grad() loss.backward() opt.step()The math book, running
Section titled “The math book, running”The loop connects several ideas from the earlier lessons:
loss = ...— this regression example usesF.mse_loss. Classification models often useF.cross_entropy, connected to average surprise.- training on batches — the sampling license: the batch gradient is a noisy estimate whose expectation is the true one.
loss.backward()— the chain rule with a tape, this article.opt.step()with its learning rate — the stride.
Go deeper: why the tape is rebuilt every pass
Eager-mode autograd builds a fresh graph as each forward pass runs, including the branches taken by Python control flow. By default, backward frees saved intermediate values. Another backward pass through the same calculation may therefore fail unless you retain the graph or recompute the forward pass. See PyTorch’s autograd explanation.
The example uses three core operations: calculate a loss, differentiate it, and update the parameters. Larger programs also use nn.Module to organize model components and parameters, and DataLoader to provide batches of data.