๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #140

Layer Normalization

Layer Normalization (LayerNorm) normalizes across a single example's own features, entirely independent of the batch โ€” fixing BatchNorm's batch-size dependency, and becoming the standard normalization choice inside Transformer architectures as a direct result.

Formula

\[ \mu = \frac{1}{d}\sum_{j=1}^d x_j, \qquad \sigma^2 = \frac{1}{d}\sum_{j=1}^d (x_j-\mu)^2, \qquad \hat x_j = \frac{x_j-\mu}{\sqrt{\sigma^2+\epsilon}}, \qquad y_j = \gamma_j\hat x_j+\beta_j \]

\(d\) is the number of features in one example. Critically, \(\mu\) and \(\sigma^2\) here are computed per individual example, across that example's own \(d\) features โ€” with no dependence whatsoever on any other example in the batch, or even on there being a "batch" at all.

The Key Structural Difference from BatchNorm

BatchNormLayerNorm
Statistics computed acrossAll examples in the batch, per featureAll features within one example, independently per example
Depends on batch size?YesNo
Same behavior in training and inference?No โ€” uses running averages at inferenceYes โ€” identical computation regardless of mode
Works with variable-length sequences?Awkward โ€” batch statistics get complicated with paddingNaturally โ€” each example (e.g. each token's representation) is normalized independently

Why Transformers Use LayerNorm, Not BatchNorm

Transformers (covered in the Transformers category) process sequences of tokens, often of varying length, and are frequently trained and deployed with widely varying batch sizes โ€” sometimes a batch size of 1 at inference. LayerNorm's complete independence from batch composition and size makes it a natural fit; it behaves identically whether processing a batch of 512 examples during training or a single example during real-time inference, with no separate "running average" bookkeeping required at all.

Numerical Example

One example's 4 features: \([2, 4, 4, 6]\) โ€” notice this is the exact same numbers as BatchNorm's example, but here they represent one example's own features, not one feature across 4 different examples. The computation is identical either way: \(\mu=4\), \(\sigma^2=2\), giving \(\hat x \approx [-1.41, 0, 0, 1.41]\) โ€” the formula is the same; only which values get grouped together for computing \(\mu\) and \(\sigma^2\) has changed.

Code

import torch
import torch.nn as nn

layer_norm = nn.LayerNorm(normalized_shape=64)   # normalizes across the last dimension (64 features)
x = torch.randn(32, 64)   # batch of 32 examples, 64 features each
output = layer_norm(x)

# Behaves identically regardless of batch size -- unlike BatchNorm
x_single = torch.randn(1, 64)
output_single = layer_norm(x_single)   # no train/eval discrepancy, no running averages needed

Common Mistakes

  • Assuming LayerNorm always outperforms BatchNorm โ€” for standard CNNs with consistently large batch sizes, BatchNorm often remains competitive or preferable; LayerNorm's advantages are most pronounced specifically for sequence models and variable-batch-size scenarios.
  • Forgetting that LayerNorm still needs its learnable \(\gamma\) and \(\beta\) parameters, just like BatchNorm โ€” the normalization step alone isn't the complete picture.

Interview Relevance

Q: "Why do Transformer architectures use LayerNorm instead of BatchNorm?" Transformers process sequences that often vary in length and are trained/deployed across a wide range of batch sizes, including batch size 1 at inference. LayerNorm normalizes each example's own features independently, with no dependence on batch composition or size, and behaves identically in training and inference โ€” avoiding BatchNorm's batch-size sensitivity and its separate running-average bookkeeping, both of which are awkward fits for how Transformers are actually used.

Practice Question

Why doesn't LayerNorm need a separate "running average" mechanism for inference, the way BatchNorm does?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Layer Normalization โ€“ FAQs

Quick answers about learning Layer Normalization in Deep Learning.

This free note from CodingNow 2.0 explains Layer Normalization in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Layer Normalization, is 100% free with no signup required.
With focused practice, most students grasp Layer Normalization in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now