Module 4
Module 4 - Deep Learning Foundations: PyTorch From the Tensor Up
You will be able to build and train a neural network in PyTorch, having written its training loop by hand first and checked every piece of it against a number you computed yourself.
A neural network is a stack of matrix products, each followed by a simple nonlinear function, with a loss at the end, and training it is one cycle repeated: predict, measure the error, compute the derivative of the loss with respect to every parameter, move each parameter a small step against its derivative, clear the derivatives. PyTorch gives you two things that make the cycle writable in a few lines: the tensor, an array that knows which device holds it, and autograd, which records the operations on a tensor that asks for it, so the derivatives come back from one call. This module covers the tensor and the derivative, the loop written out by hand and then refactored into PyTorch's own modules and optimizers, networks of several layers and classification with the loss that goes with it, mini-batches and the choice of optimizer and learning rate, the tools that fight overfitting and how to measure what each one did, saving a model, and images as tensors, where a fully connected network trains, scores, and shows exactly where it fails.
This is the part of the work where the model stops being a library call. Module 3 fitted its models through a fit method that did its work out of sight, which is the right abstraction until the thing you need to debug is the fitting itself. Here you write the step that method performed, on a single neuron fitting the price of a listing, then on a real table, then on images, and every convenience you adopt afterwards, nn.Linear, an optimizer, a data loader, is one you have already implemented and can read back into the code it replaced.
Without it, deep learning stays a stack of calls you cannot debug. Every common failure in the field is a loop failure: derivatives never cleared, so each step moves by the sum of every gradient so far, a learning rate that raises the loss instead of lowering it, a loss handed an output of one shape and a target of another, which it broadcasts into a table and averages under a warning, a model left in training mode while it is scored, so its dropout keeps dropping and the gap looks exactly like overfitting. None of these stops the program, and a student who has only ever called fit cannot see one of them. Module 3 gave you an honest score for a model you did not build. This module teaches you to build the model, and to read its training curve the way Module 3 taught you to read a learning curve.
What you will build
One project, the capstone, at the end of the module.
The Overfit Journal (PAID, $49, or $24 at the half price), where you make a network memorize a small dataset on purpose, then apply each regularization tool alone and document, with each run's own numbers as evidence, what each one did, and close with a recommendation that weighs cost against quality rather than taking the highest score. It starts from an empty directory and its bootstrap writes everything it starts from, so nothing you did or skipped earlier changes where you begin.
The sections
- Tensors and the Derivative - 4 lessons. Installing PyTorch and the tensor, tensor operations and the device pattern, derivatives and the chain rule computed by hand, then autograd checked against your own derivative.
- The Training Loop, Built by Hand - 3 lessons. One neuron fit by hand,
nn.Moduleandoptim(PyTorch's building block for a model and its optimizers, the rules that turn a gradient into a step) as the honest refactor of that code, then train and validation discipline and the loss curve. - Real Networks, MLPs and Classification - 4 lessons. From one neuron to a network, the MLP (a multilayer perceptron, layers of neurons with a nonlinear function between them), classification with logits (a network's raw outputs, before they become probabilities) and cross-entropy (the loss for classification),
DatasetandDataLoaderfor mini-batches, then SGD, Adam and the learning-rate sweep. - Regularization and the Playbook - 3 lessons. Overfitting on purpose with capacity, then weight decay, dropout and early stopping (three ways to keep a network from memorizing its training rows), then saving and loading models.
- The Image Bridge, FashionMNIST - 3 lessons. torchvision (PyTorch's library of image datasets and transforms) and image tensors, an MLP on images with flatten and train, then where MLPs fail and the case for convolution (the operation that reads an image in small windows, Module 5's subject).
Prerequisites
Module 3. You are assumed to be able to split a table into training and test rows and say what a held-out row must be, fit a model and read its loss and its metrics, standardize features on the training rows alone, encode a category as numbers, read a learning curve and name overfitting from the gap between its lines, and classify with a threshold you chose. Module 2's NumPy is assumed as well, arrays with their dtype and shape, broadcasting, axis, the dot product and X @ w, and so are Module 1's Python and Module 0's terminal, filesystem and git.
What the exam proves
PAID, $49, or $24 at the half price. That you can trace a training loop's state step by step, parameters, gradients and loss, name the cause of a stated training failure from its symptoms, a loss that climbs, a gap that opens, a gradient that doubles, and predict the dtype and shape of a tensor at every point of a network's forward pass. The pass covers the capstone and the exam with every attempt, for the life of the product.
Set up your machine first
Lesson 4.1.1 installs PyTorch into (bootcamp) and Lesson 4.5.1 installs torchvision, so before Lesson 4.1.1 starts you need the machine Module 3 left: a Unix terminal (WSL on Windows), uv, Python 3.12.8 inside a virtual environment at ~/dev/bootcamp holding Modules 2 and 3's libraries, VS Code opened on that folder, and Module 3's dataset, listings.csv, which Module 4 keeps using. Module 4's setup page checks that machine, gives the one install line that brings (bootcamp) up to what Module 4 runs on if you are starting here, names the lesson that performs each install and the processor-only build of PyTorch every lesson was run on, says what to do on an Intel Mac, which PyTorch no longer ships a build for, and ends with a checklist that must pass before Lesson 4.1.1. Read it even if your prompt already shows (bootcamp).