Skip to content
BIASED

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 r=f(x)−yr = f(x) - y. For KK classes, logits zz, probabilities pk=ezk/∑jezjp_k = e^{z_k}/\sum_j e^{z_j}, one-hot eye_y. Parameters θ\theta, learning rate η\eta, weight-decay coefficient λ\lambda, dropout probability pp, label-smoothing ε\varepsilon.

Regression losses

Three common regression losses in terms of the residual:

ℓsq(r)=r2,ℓabs(r)=∣r∣,ℓδ(r)={12r2,∣r∣≤δ,δ(∣r∣−12δ),∣r∣>δ.\ell_{\mathrm{sq}}(r)=r^2,\quad \ell_{\mathrm{abs}}(r)=|r|,\quad \ell_\delta(r)=\begin{cases}\tfrac12 r^2,&|r|\le\delta,\\ \delta(|r|-\tfrac12\delta),&|r|>\delta.\end{cases}

Derivatives w.r.t. the prediction: 2r2r, sign⁡(r)\operatorname{sign}(r), and clip⁡(r,−δ,δ)\operatorname{clip}(r,-\delta,\delta). Squared error pulls in proportion to the residual; absolute error with constant force; Huber like squared near zero and absolute beyond δ\delta.

Worked example. Residuals r=(0.5,−2,10)r=(0.5,-2,10). Batch means: squared 34.7534.75, absolute 4.1674.167, Huber (δ=1\delta=1) 3.7083.708. Per-example derivatives (1,−4,20)(1,-4,20), (1,−1,1)(1,-1,1), (0.5,−1,1)(0.5,-1,1). Under squared error the outlier supplies 2020 of 2525 in absolute gradient mass — 80%. Under Huber it supplies 11 of 2.52.5.

Stable cross-entropy

For classification the loss and its gradient are

ℓ(z,y)=−zy+log⁡∑jezj,∂ℓ∂z=p−ey.\ell(z,y)=-z_y+\log\sum_j e^{z_j},\qquad \frac{\partial\ell}{\partial z}=p-e_y.

Each logit gradient lies in [−1,1][-1,1]: unlike squared error, cross-entropy cannot explode from one coordinate. It can overflow if computed naively. In float32, eze^z overflows above z≈88.7z\approx 88.7, and tiny probabilities round to 0 so log⁡0=−∞\log 0=-\infty.

Log-softmax never forms pp. With m=max⁡jzjm=\max_j z_j,

log⁡pk=zk−m−log⁡∑jezj−m.\log p_k = z_k - m - \log\sum_j e^{z_j-m}.

Every exponent is ≤0\le 0; the sum is in [1,K][1,K]. For z=(100,0,−20)z=(100,0,-20) and true class 3: naive softmax is nan; shift-then-log still gives −∞-\infty for class 3; log_softmax gives loss 120120 and gradient (1,0,−1)(1,0,-1). Rule: pass logits to the loss, never probabilities.

Label smoothing

Cross-entropy with a one-hot target is never satisfied: its minimum 0 needs py=1p_y=1, an infinite logit gap. Label smoothing mixes the one-hot with uniform:

q=(1−ε)ey+εK1,ℓε(z,y)=−∑kqklog⁡pk.q=(1-\varepsilon)e_y+\frac{\varepsilon}{K}\mathbf 1,\qquad \ell_\varepsilon(z,y)=-\sum_k q_k\log p_k.

The gradient p−qp-q vanishes at p=qp=q, a reachable finite gap. For K=10K=10, ε=0.1\varepsilon=0.1: qy=0.91q_y=0.91, others 0.010.01; the best gap is log⁡91≈4.511\log 91\approx 4.511, where the loss equals the entropy of qq, about 0.5000.500. 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 64→256→256→1064\to 256\to 256\to 10 net reaches training accuracy 1.0 and validation ≈0.12\approx 0.12. With 55 wrong labels among 300 true ones it again fits all 300 training labels, including the wrong ones, at validation accuracy 0.8070.807 and a large gap (train loss 0.0050.005, val loss 0.9140.914). 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 λ2∥θ∥2\tfrac\lambda2\|\theta\|^2 turns a step into θ←(1−ηλ)θ−η∇L\theta\leftarrow(1-\eta\lambda)\theta-\eta\nabla L. With η=0.1\eta=0.1, λ=0.01\lambda=0.01, a zero-gradient weight falls as 0.9991000≈0.3680.999^{1000}\approx 0.368 in 1000 steps. Under Adam, L2 inside the adaptive step decays unevenly; AdamW applies the factor 1−ηλ1-\eta\lambda outside. Biases and normalization scales are usually excluded.

Dropout. In training, zero units with probability pp and scale survivors by 1/(1−p)1/(1-p) (inverted dropout), so E[h~]=h\mathbb E[\tilde h]=h. At test time the layer is the identity (model.eval()). Typical p=0.5p=0.5 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 nn 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 (λ=1\lambda=1 in AdamW) and dropout (p=0.5p=0.5) 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.