Lesson deck
Training deep networks: normalisation, regularisation, AdamW and debugging flashcards
Reviewing needs JavaScript. You can read every card in the list below.
Loading your cards...
Back to the lesson12 cards in this deck
Read all 12 cards as a list
- Front
- Batch norm versus layer norm?
- Back
- Batch norm takes mean and variance per feature across the batch, so one example's output depends on its batch, the statistics are noisy for small batches (spread about 1/sqrt(B)), and eval mode uses running estimates. Layer norm takes them per example across features and is the same in train and eval, which suits sequences and small batches.
- Front
- What changes between training and evaluation mode?
- Back
- Dropout is on in training and the identity in eval. Batch norm uses batch statistics in training and running estimates in eval. Call model.eval() for validation and serving; torch.no_grad() only turns off gradient tracking and is a separate switch. Forgetting eval() leaves noise and batch dependence in your results.
- Front
- How does dropout work in modern libraries?
- Back
- Training: zero each activation with probability p and multiply survivors by 1/(1-p), so the expected activation is unchanged. Inference: identity. The original paper scaled weights at test time instead; the two agree in expectation. In the lesson's tiny two-moons test, dropout 0.3 helped on only 3 of 8 seeds, so treat the rate as a setting to tune.
- Front
- L2 penalty versus weight decay?
- Back
- The L2 form adds lambda x W to the gradient. Under plain SGD it is equivalent to weight decay once the decay is scaled by the learning rate. Under Adam it is not, because the penalty gets divided by the gradient scale; that is why AdamW decouples the decay. Usually applied to weights, not biases.
- Front
- AdamW versus Adam with L2, in numbers?
- Back
- Adam with L2 divides the penalty by sqrt(v-hat), so its strength follows about 1/G for loss gradient scale G: with decay 0.01 and learning rate 0.001, the extra shrinkage per step was 1e-3 for G = 0, 7.07e-4 for 0.01, 1e-5 for 1 (equal to AdamW) and 1e-7 for 100. AdamW applies w <- w - lr x decay x w before the moment updates: 1e-5 at every G (a stylised setup with w fixed at 1).
- Front
- What does early stopping do, and what must you watch when reporting results?
- Back
- It keeps the weights from the epoch with the lowest validation loss. The best epoch ranged from 2 to 188 over 8 seeds, so it must be found on held-out data. The chosen validation loss is optimistic, so report the final number on a separate test set.
- Front
- What did the two-moons experiment show (30 points, 8 seeds)?
- Back
- No regularisation: train loss 0.123, test loss 0.616, accuracy 0.826. Weight decay 0.01: test loss 0.417 (better on 7 of 8 seeds), accuracy 0.828. Dropout 0.3: 0.595 (better on 3 of 8, within noise), accuracy 0.822. Early stopping beat the last epoch on 8 of 8 seeds (test loss 0.380). Weight decay improved the loss, not the accuracy, and dropout changed little, in this one tiny problem.
- Front
- What do warmup and cosine decay do?
- Back
- Warmup ramps the rate from near 0 to the peak over the first steps (used for large-minibatch SGD and the original transformer: 4,000 steps). Cosine decay then follows eta_min + 0.5 (peak - eta_min)(1 + cos(pi t/T)); a quarter of the way down it is still at 85% of the peak. Why warmup helps Adam is still studied.
- Front
- How does clipping by global norm work?
- Back
- Compute one norm over all parameter gradients as if concatenated; if it exceeds max_norm, multiply every gradient by max_norm/norm. The direction is kept and the length capped; gradients below the limit are untouched. (3, 4) has norm 5 and becomes (0.6, 0.8) at limit 1. It guards against rare spikes.
- Front
- Loss is NaN: what is your checklist?
- Back
- Find the first non-finite step. NaN at step 0: inspect data and labels for NaN or inf, then the loss code (softmax overflow, log of 0; use logsumexp). After a rise: learning rate too high (lower it, warm up, clip). Sudden: a bad batch, zero variance in a norm layer, exploding activations. Then overfit 4 examples and run a gradient check.
- Front
- Loss is flat from the start: what do you check, in order?
- Back
- The initial loss against ln C; whether the model can overfit four examples (if not, a bug in data, loss or backward pass); the learning rate, including a schedule that already decayed to zero; the initial weight scale (tiny init or dead units). The classic slip is a softmax before a loss that expects logits.
- Front
- Softmax before CrossEntropyLoss: what goes wrong?
- Back
- PyTorch's CrossEntropyLoss expects unnormalised logits. Feeding it softmax outputs applies softmax twice; with 3 classes the true-class probability can never exceed e/(e+2), about 0.58 (derived), so learning is weak while the code still runs.