PyTorch: Building Neural Networks
How do I actually build a deep learning model?
You will learn
Hands-on: An MLP on tabular data, written without fit().
Engineering: Virtual environments, dependencies, Git/GitHub, project structure.
Lessons
-
Lesson 2.1: Tensors and the Shape Contract
Every tensor carries a three-part contract: shape, dtype and device. Nearly every PyTorch error you will ever see breaks one of those three clauses. This lesson teaches you to read shapes like sentences, reshape without lying, broadcast on purpose, see a...
Not started Start -
Lesson 2.2: Dataset and DataLoader: Feeding the Model
How a table or a folder of files becomes the batches a network trains on. You measure why mini-batches matter even when memory doesn't, split before preprocessing so nothing leaks, encode a real mixed-type table, write your own Dataset (in memory and...
Not started Start -
Lesson 2.3: Building and Training an MLP You Can Defend
You build a real MLP for the Bank Marketing data and defend every choice: how nn.Module registers parameters (and the silent bug when it doesn't), how to compose blocks, how to trace shapes with hooks, why the loss takes logits, a training loop with...
Not started Start -
Lesson 2.4: From Notebook to Project
You turn Lessons 2.1–2.3 into a small repository someone else can clone, install, test and run without opening a notebook: code extracted into importable modules under src/, every experiment described by a YAML config, seeds (and what they cannot promise),...
Not started Start
Module challenge
Module 2 challenge: building a network you can defend
8 scenario questions that mix ideas from all 4 lessons. One question at a time, an explanation after each, and your score at the end. Pass mark 75%, so 6 of 8.
Best after you have practised all 4 lessons.
A regression model outputs shape (64, 1) and the targets are shape (64,). You use nn.MSELoss. The code runs. What is actually being optimised?
Broadcasting aligns from the right to (64, 64) with only a warning (Lesson 2.1 §4). Assert shapes before every loss.
You receive an image batch of shape (16, 224, 224, 3). What is the one-line fix before a PyTorch convolution?
view/reshape would scramble the pixels into the wrong axes; permute reorders the axes to (N, C, H, W) (Lesson 2.1 §3, §7).
You standardise features with statistics computed on the full dataset, then split into train and validation. What is wrong, and how big is the damage likely to be?
Split first, fit preprocessing on training only (Lesson 2.2 §2; Lesson 4.1 §5 measures both cases).
Your training DataLoader uses shuffle=False on a file sorted by date, and the positive rate climbs through the file. What happens during each epoch?
Shuffle training data so each batch is representative; keep validation unshuffled and deterministic (Lesson 2.2 §6).
A model stores its hidden layers in a Python dict: self.layers = {'a': nn.Linear(...), 'b': nn.Linear(...)}. Training runs and the loss moves a little. What is going on?
Registration is what model.parameters(), .to(), state_dict() and train()/eval() rely on (Lesson 2.3 §1). Count parameters to catch it.
A call centre can phone 15% of customers. Two models: A has AUC 0.80 and accuracy 0.88; B has AUC 0.77 and accuracy 0.89. Which evaluation should decide?
The team acts on the top k, so ordering matters, not a 0.5 threshold (Lesson 2.3 §7).
Early stopping keeps best = model.state_dict() (no copy) and training continues for 10 more epochs. What do you restore at the end?
Use copy.deepcopy(model.state_dict()) for a real snapshot (Lesson 2.3 §9).
Running python src/training/train.py fails with No module named 'src', while python -m src.training.train works. What is the robust fix for a real project?
Running a file directly puts its own folder on the path, not the project root. An editable install makes imports work from anywhere (Lesson 2.4 §5).