Batch Normalization › The Batch Normalization Algorithm
Language AI HandbookAll chapters Batch Normalization › The Batch Normalization AlgorithmOne section · Sections in this chapter3 / 17 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Batch normalization operates on a mini-batch of activations. For a single feature dimension (one neuron's pre-activation), consider a mini-batch of samples. Batch normalization transforms these values through four steps.
This section covers all four steps: computing the batch mean, computing the batch variance, normalizing, and then applying a learned scale and shift.
Step 1: Compute the Batch Mean
First, compute the mean activation across the mini-batch:
where:
- : the mean of activations over the current mini-batch
- : the number of samples in the mini-batch
- : the activation value for sample
This mean captures the "center" of the activation distribution for this batch. If activations are consistently large and positive, the mean will be large and positive. The goal is to shift this center to zero. Note that this is computed independently for each feature dimension: if a layer has 256 neurons, there are 256 separate mean computations, one for each neuron's activation values across the batch.
Step 2: Compute the Batch Variance
Next, measure how spread out the activations are around the mean:
where:
- : the variance of activations over the current mini-batch
- : the squared deviation of each activation from the batch mean
This variance tells us how much the activations fluctuate around their mean. High variance means some activations are very large and others very small. Our goal is to compress this spread to approximately one. Note that this uses the population variance formula (dividing by , not ), which is a deliberate design choice: the original paper uses population statistics, and PyTorch's implementation follows this convention by default.
Step 3: Normalize
With the mean and variance in hand, we normalize each activation:
where:
- : the normalized activation for sample
- : a small positive constant (typically ) added for numerical stability to prevent division by zero when variance is very small
This step produces activations with zero mean and unit variance within the batch. The term prevents numerical problems when the variance is close to zero, which can happen if all activations in the batch are nearly identical. Concretely, if a batch happens to contain nearly identical inputs to a neuron (for example, if the preceding layer has collapsed its outputs to a constant), the variance would be zero and division would produce either infinity or NaN. The ensures the denominator is always at least , which is small enough not to distort the normalization but large enough to prevent numerical failure.
Step 4: Scale and Shift (Learnable Parameters)
Pure normalization would be too aggressive. Consider a layer followed by a sigmoid activation: if all inputs to the sigmoid are normalized to zero mean and unit variance, they land in the linear regime of the sigmoid, essentially turning the sigmoid into a linear function. The network would lose its nonlinearity. More generally, normalization restricts what each layer can represent: a layer that learned to produce large-scale outputs for particular inputs would have that learned information erased by normalization.
To restore representational flexibility, batch normalization introduces two learnable parameters per feature:
where:
- : the scale parameter (learned during training), initialized to 1
- : the shift parameter (learned during training), initialized to 0
- : the final batch-normalized output for sample
These parameters allow the network to learn the optimal output scale and shift for each feature. If the network determines that a particular feature dimension is most useful when centered at 5 with a spread of 2, gradient descent will drive and . In effect, and allow the network to undo the normalization if that is beneficial, making normalization a learnable transformation rather than a rigid constraint.
This might seem paradoxical: why normalize and then allow the network to un-normalize? even if and eventually learn to reproduce the original un-normalized distribution, the gradients flowing through the network during training are better conditioned because of the normalization. The gradient updates to and happen in a well-behaved, smooth optimization landscape, whereas the gradient updates to the weights before batch normalization would have faced the full chaos of internal covariate shift. The normalization changes the geometry of the optimization landscape even if the final learned transformation looks similar to the pre-normalization case.
Each feature dimension in each layer has its own independent and . For a layer with features, batch normalization adds learnable parameters. For large networks with many layers and many features per layer, this adds up, but the count is small relative to the weight matrices.
The Complete Transformation
Putting all four steps together, the full batch normalization transformation for feature dimension across a mini-batch is:
where is the activation of feature for sample . Notice that normalization happens independently per feature dimension. Each neuron's output is normalized using only that neuron's statistics across the batch.
This independence is important. It means that batch normalization does not impose any relationship between different features or neurons. Each feature dimension is treated as its own mini-distribution, normalized independently, and then rescaled with its own learned parameters. This preserves the network's ability to learn arbitrary correlations between features through the weight matrices, while still ensuring each individual feature maintains a stable distribution.