01The training loop
Every supervised network trains with the same loop, repeated over epochs (passes through the training set) in mini-batches:
- Forward pass — compute predictions for a batch.
- Loss — measure how wrong they are; for classification, cross-entropy = −log(probability of the true class).
- Backward pass — backpropagation computes the gradient of the loss with respect to every weight.
- Update — an optimiser moves the weights against the gradient: SGD with momentum, or AdamW (Adam with decoupled weight decay).
for x, y in loader:
opt.zero_grad()
loss = F.cross_entropy(model(x), y)
loss.backward()
opt.step()
The learning rate is the most important hyper-parameter: too high and the loss jumps around or diverges; too low and training crawls. Warm-up plus cosine decay is a robust default schedule.