PyTorch Setup Map
Every training script in this course is built from the same four objects. Learn their names once and the rest of the material becomes readable.
Four objects, and what each is for
A Dataset holds your examples and knows how to return one. A DataLoader groups them into batches and shuffles them. A model maps inputs to outputs. An optimiser updates the model's parameters using their gradients.
That is the entire vocabulary:
import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset
# The four pieces every training script in this course uses.
data = TensorDataset(torch.randn(64, 4), torch.randn(64, 1)) # 1. data
loader = DataLoader(data, batch_size=16) # 2. batching
model = nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 1)) # 3. model
optimizer = torch.optim.Adam(model.parameters()) # 4. optimiser
print("dataset size :", len(data))
print("batches :", len(loader), "of size", loader.batch_size)
print("parameters :", sum(p.numel() for p in model.parameters()))
xb, yb = next(iter(loader))
print("one batch :", tuple(xb.shape), "->", tuple(model(xb).shape))
# Learn these four names and every script in the course is readable.
Note the shapes at the end: a batch of 16 rows with 4 features goes in, and 16 predictions come out. The batch dimension travels through the whole pipeline unchanged — losing it is the most common shape bug you will hit.
The mistake this prevents
Treating the DataLoader as optional and feeding the whole dataset at once. It works on 64 rows and runs out of memory on 64,000. Batching is not an optimisation you add later; it is how the loop is designed to work.
Takeaway
Dataset, DataLoader, model, optimiser. Every script in the course is those four objects and a loop.
