Skip to main content
Open In Colab

title: Batch Normalization in ResNets description: PyTorch implementation showing how batch normalization enables deeper residual networks, with CIFAR-10 training experiments comparing models with and without BN.

Batch normalization (BN) is a critical ingredient of modern residual networks. In this section we:
  1. Build a minimal ResNet block with and without batch normalization
  2. Train both variants on CIFAR-10 and compare convergence speed, final accuracy, and gradient health
  3. Visualize how BN stabilizes the distribution of intermediate activations across training
The canonical ResNet block from He et al. (2016) is: y=f(x)+x,f(x)=W2โˆ—ReLU(BN(W1โˆ—x))y = f(x) + x, \quad f(x) = W_2 * \text{ReLU}(\text{BN}(W_1 * x)) where โˆ—* denotes convolution and BN\text{BN} normalizes the pre-activation tensor to have zero mean and unit variance, then scales and shifts with learnable ฮณ\gamma and ฮฒ\beta: x^=xโˆ’ฮผBฯƒB2+ฯต,y=ฮณx^+ฮฒ\hat x = \frac{x - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}, \quad y = \gamma \hat x + \beta

CIFAR-10 data loaders

We use standard CIFAR-10 normalisation (channel mean and std computed from the training set).

ResNet building blocks

We implement two variants of the basic residual block:
  • ResBlock โ€” with batch normalization (use_bn=True, default)
  • ResBlock โ€” without batch normalization (use_bn=False)
The skip connection uses a 1ร—11\times 1 convolution when the spatial dimensions or channel count change (the projection shortcut).

Training loop

We train both variants for the same number of epochs with the same SGD + cosine-annealing schedule and compare:
  • Training loss and test accuracy per epoch
  • Gradient norms at the stem layer (a proxy for gradient health)

Results: training loss, test accuracy, and gradient norms

The three plots below summarise the effect of batch normalisation on a residual network trained on CIFAR-10.
Output from cell 5

Activation distribution across training

To see why BN helps, we capture the distribution of activations at the output of layer1 at epochs 1, 15, and 30. Without BN the distribution drifts and widens; with BN it stays anchored near zero.
Output from cell 6

Summary

These results confirm why every modern ResNet variant (ResNet-50, ResNeXt, Wide-ResNet) applies batch normalization after each convolution before the non-linearity.

PyTorch reference


Found an error on this page? Report an issue.