Losses and regularization
Regression losses and cross-entropy with log-softmax, label smoothing, networks' ability to memorize random labels, and four standard regularizers — weight decay, dropout, early stopping, augmentation.
From the pipeline to the loss
The topic PyTorch and automatic differentiation built a training pipeline and used cross-entropy in it without asking why. Two parts of that pipeline decide what the network actually learns. The loss says what counts as a good prediction and, through its gradient, how hard each example pulls on the weights. Regularization decides what happens once the network can fit its training set exactly — which a deep network almost always can. This topic covers both.
Notation. Residual . For classes, logits , probabilities , one-hot . Parameters , learning rate , weight-decay coefficient , dropout probability , label-smoothing .
Regression losses
Three common regression losses in terms of the residual:
Derivatives w.r.t. the prediction: , , and . Squared error pulls in proportion to the residual; absolute error with constant force; Huber like squared near zero and absolute beyond .
Worked example. Residuals . Batch means: squared , absolute , Huber () . Per-example derivatives , , . Under squared error the outlier supplies of in absolute gradient mass — 80%. Under Huber it supplies of .
Stable cross-entropy
For classification the loss and its gradient are
Each logit gradient lies in : unlike squared error, cross-entropy cannot explode from one coordinate. It can overflow if computed naively. In float32, overflows above , and tiny probabilities round to 0 so .
Log-softmax never forms . With ,
Every exponent is ; the sum is in . For and true class 3: naive softmax is nan; shift-then-log still gives for class 3; log_softmax gives loss and gradient . Rule: pass logits to the loss, never probabilities.
Label smoothing
Cross-entropy with a one-hot target is never satisfied: its minimum 0 needs , an infinite logit gap. Label smoothing mixes the one-hot with uniform:
The gradient vanishes at , a reachable finite gap. For , : , others ; the best gap is , where the loss equals the entropy of , about . Past that optimum the smoothed loss rises again.
Capacity and memorization
Deep networks can memorize. Zhang et al. (2017) trained image classifiers on random labels and still reached (near) zero training error. On 300 bundled digits with fully random labels, a net reaches training accuracy 1.0 and validation . With 55 wrong labels among 300 true ones it again fits all 300 training labels, including the wrong ones, at validation accuracy and a large gap (train loss , val loss ). Training accuracy alone says nothing about what was learned.
Regularizers
Key idea. A regularizer makes the training set harder to fit exactly: penalize large weights, add noise, stop early, or transform inputs without changing the label.
Weight decay. Adding turns a step into . With , , a zero-gradient weight falls as in 1000 steps. Under Adam, L2 inside the adaptive step decays unevenly; AdamW applies the factor outside. Biases and normalization scales are usually excluded.
Dropout. In training, zero units with probability and scale survivors by (inverted dropout), so . At test time the layer is the identity (model.eval()). Typical on large fully connected layers.
Early stopping keeps the checkpoint with best validation loss. Data augmentation trains on random label-preserving transforms so the net cannot memorize fixed inputs.
The gap experiment
On 300 digits with 55 wrong labels, AdamW, 200 epochs:
| configuration | train acc | val acc | gap | wrong labels fitted |
|---|---|---|---|---|
| none | 1.000 | 0.807 | 0.193 | 1.000 |
| weight decay | 0.979 | 0.842 | 0.137 | 0.885 |
| dropout | 0.989 | 0.843 | 0.146 | 0.939 |
| both | 0.887 | 0.881 | 0.006 | 0.400 |
Together, weight decay ( in AdamW) and dropout () cut memorized wrong labels from 100% to 40% and raise validation accuracy from 0.807 to 0.881. Strengths were chosen on this same validation set — slightly optimistic; a closed gap is not a goal if both sides are poor.
Even with a good loss and regularizer, a deep stack can still misbehave: signals and gradients shrink or explode through many layers. The next topic, Normalization and initialization, restores usable scale and keeps examples apart.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.