Module 02 β Autograd βοΈ
The engine that makes neural networks learn. Once you see how it works, "training a model" stops feeling like magic.
In Module 01 you learned that a tensor is a supercharged NumPy array. Here's the superpower that NumPy can't touch: a PyTorch tensor can remember every operation performed on it, then automatically compute the gradients β the derivatives β of some final output with respect to every input. That process is called automatic differentiation, and PyTorch's implementation is autograd.
Why does this matter? Training a neural network means repeatedly nudging millions of weights in the direction that reduces error. To know which direction, you need the gradient of the loss with respect to each weight. Computing those derivatives by hand would be hopeless. Autograd does it for you: you write the forward computation as ordinary Python, call .backward(), and every gradient appears. Master this module and the training loop in Phase 2 will feel almost inevitable.
This module is split into three sub-modules β work through them in order.
π Sub-Modules
| # | Sub-Module | What you'll learn |
|---|---|---|
| 01 | The Computational Graph | How PyTorch records operations into a dynamic graph, and the role of requires_grad |
| 02 | Backward Pass & Gradients | The forward vs. backward pass, calling .backward(), and reading gradients from .grad |
| 03 | Turning Autograd Off | torch.no_grad(), .detach(), and why inference and evaluation need them |
| 04 | Gradient Gotchas | Why .grad accumulates (and must be zeroed), leaf vs. non-leaf tensors, and retain_graph |
π― By the end of this module, you'll be able to...
- Explain what the computational graph is and how
requires_gradputs a tensor on it. - Run a forward pass, call
.backward(), and interpret the gradients stored in.grad. - Correctly disable gradient tracking for inference to save memory and avoid subtle bugs.
- Understand why gradients accumulate and how zeroing them prepares you for the training loop.
β Prerequisites
Comfort with tensors from Module 01 β creating them, doing math, and moving them across devices.
β¬ οΈ Prev module: 01 Β· Tensors Β· β‘οΈ Next module: 03 Β· Neural Networks