Why Transformers Choose Layer Normalization Over Batch Normalization
This article explains why Transformer models use Layer Normalization instead of Batch Normalization. It reviews the role of normalization in deep networks, the problem of internal covariate shift, how Batch Normalization behaves during training and inference, why batch-based statistics are difficult to apply to variable-length text and autoregressive generation, and how Layer Normalization provides stable, sample-wise normalization that is better suited to sequence modeling.
1. Introduction: A Simple Question That Is Often Misunderstood
When reading the Transformer architecture, almost everyone notices that each sublayer, including the self-attention layer and the feed-forward network layer, is paired with Layer Normalization (LN) rather than Batch Normalization (BN), which has been highly effective in computer vision. Why is that?
The complete answer can be broken into two parts:
- Why is normalization needed? What problem does it solve, and what is the actual mechanism that makes it useful?
- Why Layer Normalization instead of Batch Normalization? In sequence modeling, the essential differences between the two determine which one is more appropriate.
The following sections explain this step by step.
2. Background: Why Normalization Was Introduced
2.1 Internal Covariate Shift
Training a deep network can be viewed as stacking a series of mappings layer by layer. For layer L, the linear transformation and nonlinear activation can be written as:
Z[L] = W[L] · A[L-1] + b[L] # Linear transformationA[L] = g[L](Z[L]) # Activation function
During training, as gradient descent repeatedly updates W[L] and b[L], the distribution of Z[L] keeps shifting. This in turn changes the distribution of A[L], which is the input to layer L+1. This phenomenon, where the input distribution of each layer keeps changing during training, is called Internal Covariate Shift (ICS).
ICS has two direct consequences:
- Slower convergence: Every layer must constantly adapt to changes in the input distribution coming from upstream layers, which weakens the learning signal.
- Higher risk of saturation: For saturating activation functions such as sigmoid and tanh, the gradient becomes close to zero when the input magnitude is large, causing vanishing gradients. If the input distribution shifts significantly, the probability of falling into the saturation region increases.
2.2 Early Mitigation Methods and Their Limitations
Before normalization methods became common, typical ways to address this problem included using a smaller learning rate, carefully designing parameter initialization, and using non-saturating activation functions such as ReLU. These methods could only alleviate the problem, not eliminate it, and they were highly sensitive to hyperparameters.
This was one of the motivations for Batch Normalization: instead of passively adapting to distribution shift, it actively pulls the input of each layer back to a more stable distribution.
3. Batch Normalization: Design and Practice
3.1 Training Phase
The idea of BN is to compute the mean and variance for each feature dimension within a mini-batch, and then normalize the values:
μ = mean(Z, axis=batch) # Compute mean along the batch dimensionσ² = var(Z, axis=batch) # Compute variance along the batch dimensionZ_norm = (Z - μ) / sqrt(σ² + ε)Z_out = γ · Z_norm + β # Learnable scale and shift parameters
The key point is that the statistics are computed along the batch dimension. In other words, the same feature is aligned across different samples. The final γ and β allow the network to recover the original representational capacity when needed.
3.2 Inference Phase
During inference, samples often arrive one by one, so there may be no batch to rely on. Therefore, BN maintains a moving average of the mean and variance during training and uses these fixed statistics during inference.
This introduces a subtle issue: inference behavior depends on the statistical properties of the batches seen during training, creating a slight mismatch between training and inference.
3.3 Advantages of BN
In practice, BN provides several benefits:
- It allows the use of larger learning rates and significantly speeds up convergence.
- It reduces sensitivity to initialization.
- The statistics computed within a batch introduce slight noise, which has a mild regularization effect.
3.4 An Important Reversal: The Real Reason BN Works
Later studies suggested that the effectiveness of BN may not come mainly from eliminating ICS. Experiments found that even when distribution perturbations were artificially injected between layers, the network could still train well. Conversely, even when ICS was removed, training was not necessarily faster.
A more widely accepted explanation is:
The real role of BN is to smooth the loss landscape, improve the Lipschitz properties of gradients, make gradient descent more stable, and therefore allow larger learning steps.
This conclusion is important because it reminds us that the value of normalization lies in stabilizing optimization, not necessarily in aligning batch statistics. Once this is clear, there is room to reconsider which dimension the statistics should be computed along. This is exactly where Layer Normalization comes in.
4. Layer Normalization: The Transformer Choice
4.1 Why NLP Is Not a Good Fit for BN
In image tasks, the input size is fixed, and pixel features within a batch are highly homogeneous, so computing statistics along the batch dimension is meaningful. In NLP, however, the situation is different:
- Sequence lengths vary: Different sentences contain different numbers of tokens. Padding tokens mixed into the statistics can contaminate the mean and variance.
- Batch-dimension statistics are unstable: Sentences can differ greatly in meaning, and the same position in different sentences may represent completely different semantics. Statistics computed across samples have weak physical meaning.
- Small-batch scenarios fail: When training large models, GPU memory limits often force the batch size to be small, making BN’s statistical estimates very noisy.
- Training and inference become inconsistent: Autoregressive generation decodes token by token, which naturally conflicts with BN’s moving-average mechanism.
4.2 How LN Works
LN changes the dimension along which statistics are computed. Instead of computing them along the batch dimension, it computes the mean and variance along the feature dimension of a single sample:
μ = mean(Z, axis=feature) # Compute mean over the feature dimension of each tokenσ² = var(Z, axis=feature)Z_norm = (Z - μ) / sqrt(σ² + ε)Z_out = γ · Z_norm + β
As a result:
- The normalization of each token depends only on its own features and is completely independent of batch size, sequence length, and other samples.
- Training and inference behave consistently, with no need to maintain moving statistics.
- It is naturally friendly to variable-length sequences, small batches, and token-by-token decoding.
4.3 Evolution of Normalization Placement in Transformers
Early Transformers used the Post-LN structure, where normalization was applied after the output of each sublayer. This required techniques such as warmup to stabilize training. Later large models generally moved to the Pre-LN structure, where normalization is placed before the input of each sublayer. This makes gradient propagation smoother and makes deep networks easier to train.
In addition, simplified variants such as RMSNorm have been proposed for multi-head attention. RMSNorm removes the mean-centering step and performs only scaling normalization, reducing computational overhead while maintaining performance. This further confirms that the core value of normalization lies in stabilizing numerical scales and the optimization process.
5. Conclusion
Returning to the original question, the logic behind Transformer’s choice of Layer Normalization is as follows:
- Deep networks need normalization to smooth the loss landscape and stabilize gradient descent, which is the source of its necessity.
- BN relies on batch-dimension statistics, which are neither precise nor convenient in NLP scenarios involving variable-length sequences, small batches, and autoregressive inference.
- LN changes the statistics to the feature dimension, making normalization self-contained for each token and consistent between training and inference, which fits sequence modeling well.
In one sentence: Normalization solves the problem of training stability, and the statistical approach of LN naturally matches the structural characteristics of text data. This is the fundamental reason Transformers choose Layer Normalization.
