What is batch normalization?
Batch normalization standardizes each feature using the mean and variance of the current mini-batch, then rescales it with two learned parameters, gamma and beta. It lets deep networks train with higher learning rates and makes them less sensitive to initialization, and at inference time it switches to running averages collected during training. On TensorTonic you implement batch normalization and layer normalization from scratch, then add them inside a ResNet and a Transformer.
Batch Normalization (BN), introduced by Ioffe and Szegedy (2015), revolutionized deep learning training. Before BN, training very deep networks was notoriously difficult: gradients would vanish or explode during backpropagation, requiring careful initialization and low learning rates.
BN solves this by normalizing the inputs to each layer, making training faster, more stable, and less sensitive to initialization. It's now a standard component in most modern architectures.
The Impact
With BatchNorm, you can use much higher learning rates (10-100x), train deeper networks (100+ layers), and often remove dropout completely. It's not an exaggeration to say BN enabled the deep learning revolution of 2015-2020.
BatchNorm vs LayerNorm vs GroupNorm
All three standardize activations and then apply a learned scale and shift, and they differ only in which values they average over. That choice decides whether the layer depends on the batch size, which is why convolutional networks and Transformers use different ones.
| BatchNorm | LayerNorm | GroupNorm | |
|---|---|---|---|
| Averages over | Each feature across the batch, and across positions in a CNN | All features of a single example | Groups of channels within a single example |
| Depends on batch size | Yes, and it becomes unreliable with small batches | No | No |
| Behaves differently at inference | Yes, it switches to running averages | No | No |
| Typical use | CNNs such as ResNet | Transformers and RNNs | CNNs trained with small batches, such as detection models |
The Problem: Internal Covariate Shift
Consider a deep network. Each layer transforms its input:
During training, as we update weights W(l-1) in earlier layers, the distribution of h(l-1) changes. This means layer l constantly receives inputs from a shifting distribution, a phenomenon called Internal Covariate Shift (ICS).
Problem 1: Vanishing/Exploding Gradients
If activations grow unbounded, gradients explode. If they shrink, gradients vanish. Without normalization, you're forced to use tiny learning rates and careful initialization.
Problem 2: Saturated Activations
Activations like sigmoid/tanh saturate (gradient ≈ 0) when inputs get too large. Without BN, activations drift into saturation zones, killing learning.
Problem 3: Slow Convergence
Each layer must continuously adapt to changing input distributions. This slows down learning significantly, especially in deep networks.
Interactive: See Covariate Shift in Action
Internal Covariate Shift
Without BatchNorm, earlier layer weight updates cause later layer distributions to shift. This is Internal Covariate Shift.
Without BatchNorm
- • Distributions drift unpredictably
- • Later layers must constantly adapt
- • Gradients vanish/explode
- • Requires low learning rate
With BatchNorm
- • Distributions stay normalized
- • Each layer sees consistent inputs
- • Stable gradient flow
- • Can use 10-100x higher LR
The Algorithm
Batch Normalization normalizes each feature to have zero mean and unit variance across the mini-batch. For a mini-batch of size m with feature vector x:
Batch Normalization Transform
Step 1: Compute batch statistics
Step 2: Normalize
ε (typically 10-5) prevents division by zero
Step 3: Scale and shift (learnable)
γ and β are learned parameters that restore representational power
Why Scale and Shift?
Normalizing to mean 0, variance 1 might be too restrictive. The γ and β parameters let the network learn the optimal scale and shift for each feature. If the network wants the original distribution back, it can learn γ = σB and β = μB.
Interactive: Distribution Stability
Watch how BatchNorm keeps activation distributions stable during training. Toggle it off to see distributions shift dramatically.
Batch Normalization Effect
Distribution Metrics
Stable Distribution
Distributions remain centered and scaled. Gradients flow efficiently, and training converges faster.
Training vs Inference
There's a crucial difference between training and inference:
During Training
Use batch statistics:
- Compute μB and σ²B from current mini-batch
- Normalize using these statistics
- Provides regularization (each sample sees slightly different normalization)
During Inference
Use population statistics:
- Use running averages computed during training
- Ensures deterministic output for same input
- Works for batch size = 1
Running Statistics Update
During training, maintain exponential moving averages:
Typical α (momentum) = 0.1
Why It Works
The original paper claimed BN reduces internal covariate shift, but recent research suggests the real benefits are more nuanced:
1. Smooths the Loss Landscape
BN makes the optimization landscape smoother (less Lipschitz constant), enabling larger learning rates. The loss surface becomes more predictable and easier to navigate.
2. Prevents Gradient Vanishing/Explosion
By keeping activations in a reasonable range, BN prevents gradients from becoming too small or too large as they backpropagate through many layers.
3. Acts as Regularization
Each sample is normalized differently depending on its batch. This noise acts as a regularizer, reducing overfitting (similar to dropout).
4. Reduces Sensitivity to Initialization
With BN, you can use simpler initialization schemes. The normalization brings activations to a standard scale regardless of initial weights.
Practical Impact
With BN, you can typically:
- Use 10-100x larger learning rates
- Train 2-3x faster with SGD
- Often eliminate dropout
- Train much deeper networks (ResNet-152, etc.)
Variants & Extensions
Layer Normalization (LayerNorm)
Normalize across features (not batch). Used in Transformers and RNNs where batch size is problematic.
Instance Normalization
Normalize each sample independently. Popular in style transfer and GANs where batch dependencies are undesirable.
Used in: Style transfer, real-time image generation
Group Normalization
Divide channels into groups and normalize within each group. Works well with small batch sizes.
Used in: Object detection, video understanding (where batch size is small)
Weight Normalization
Normalize weights instead of activations. Decouples magnitude and direction of weight vectors. Related to weight decay in AdamW.
Practical Considerations
Where to Place BN?
Before or after activation? Original paper suggested before (after linear transform, before activation). Modern practice often uses after activation. Both work, but before is more common.
Batch Size Matters
BN requires reasonably large batches (≥8-16) for good statistics. Very small batches lead to noisy estimates and hurt performance. For small batches, use Group Norm or Layer Norm instead.
Computational Cost
BN adds ~10-15% computation overhead. The trade-off is usually worth it for the training speed improvement, but it can be a bottleneck during inference.
Floating Point Precision
With mixed precision training (FP16), BN can be numerically unstable. Keep BN parameters in FP32.
Limitations & Alternatives
Problem 1: Batch Dependency
Training and inference use different statistics, which can cause train/test mismatch. Small batches give poor statistics.
Problem 2: RNNs
Applying BN to RNNs is tricky because sequence lengths vary. Layer Normalization works better for sequential models and Transformers.
Problem 3: Distributed Training
With very small per-GPU batch sizes in distributed settings, BN statistics become unreliable. Solutions: sync BN across GPUs (slow) or use Group Norm.
Modern Alternatives
Recent architectures sometimes replace BN:
- Transformers: Use Layer Normalization (batch-independent)
- Vision Transformers: Sometimes skip normalization entirely with proper initialization
- NFNets: Normalizer-Free Networks use adaptive gradient clipping instead of BN
- YOLO/Detectron: Use Group Normalization for small batch object detection
Frequently asked questions about batch normalization
How does batch normalization behave differently at inference?
During training batch normalization uses the mean and variance of the current mini-batch, while at inference it uses running averages collected during training. This keeps the output deterministic for the same input and lets it work with a batch size of 1.
When should you use layer normalization instead of batch normalization?
Layer normalization averages over all features of a single example, so it does not depend on the batch size. That makes it the better choice for Transformers, RNNs and training with very small batches, where batch statistics become unreliable.
Practice this
On TensorTonic you implement both normalizations from scratch and use them inside real architectures.
- Batch Normalization to implement the forward pass with batch statistics and the learned scale and shift.
- Layer Normalization to normalize across features instead and compare the two.
- BatchNorm in ResNet to add batch normalization inside a residual network, step by step from the paper.
- Layer Normalization in the Transformer to build the normalization layer used in every Transformer block.