Why Transformers use LayerNorm instead of BatchNorm

Normalization isn’t glamorous, but it keeps deep learning models from collapsing under their own complexity. The difference between BatchNorm and LayerNorm lies in a single, critical choice: which numbers do you average over? For Transformers, the answer is clear—LayerNorm wins by normalizing each token independently, ensuring consistency across any batch size or sequence length.
The shared math behind two very different approaches
Both BatchNorm and LayerNorm follow the same core formula: they standardize activations to mean zero and variance one, then apply learnable scale and shift parameters. The math is identical—x̂ = (x − μ) / √(σ² + ε), followed by y = γx̂ + β. What changes is the axis of computation. BatchNorm averages across the batch dimension, treating each feature column as a single unit. LayerNorm, on the other hand, normalizes each row—the features of a single token—regardless of batch size or sequence length.
Why BatchNorm fails where LayerNorm thrives
BatchNorm’s reliance on batch statistics makes it fragile in sequence models. With a batch size of one, variance collapses to zero, rendering normalization undefined. Even in larger batches, fluctuating sizes or variable-length sequences disrupt its calculations. Worse, BatchNorm behaves differently during training (using live statistics) and inference (using stored running averages), introducing inconsistency. LayerNorm avoids these pitfalls entirely. By normalizing per token, it remains stable whether processing one example or a thousand, during training or inference. This reliability is why Transformers—with their variable-length sequences—depend on LayerNorm.
Placement matters: pre-norm vs post-norm
The choice of normalization isn’t just about the layer itself, but where it sits in the architecture. Original Transformers used post-norm, applying normalization after the sublayer, which required careful learning rate warmup and struggled with deep networks. Modern designs favor pre-norm, normalizing inputs before the sublayer. This approach creates a cleaner residual pathway, making training more stable and scalable.
Transformers demand consistency, and LayerNorm delivers it. While BatchNorm once revolutionized convolutional networks, its batch-dependent nature clashes with the flexibility of transformers. The result is a quiet but essential shift—one that keeps models stable, no matter the data.
Source: DEV Community. AI-assisted editorial synthesis — TechnoExpress.

