Deep networks stack many layers. Layer ℓ learns weights assuming a certain distribution of inputs. But those inputs are the outputs of layer ℓ−1, whose weights are also changing. So the input distribution to layer ℓ keeps shifting during training — informally called internal covariate shift. Big or lopsided activations also cause exploding/vanishing gradients and slow learning.
BatchNorm fixed this by normalizing across the batch dimension. But that ties every example's statistics to its batch-mates. This breaks when:
the batch is tiny (noisy statistics),
you process one token at a time (RNNs / autoregressive Transformers),
We have one example's pre-normalization vector x∈RH.
Step 1 — mean over features.Why? To recenter to zero so the layer isn't biased high or low.
μ=H1∑i=1Hxi
Step 2 — variance over features.Why? To measure spread so we can rescale to a controlled size.
σ2=H1∑i=1H(xi−μ)2
Step 3 — standardize.Why? Subtracting μ centers it; dividing by σ2+ϵ makes variance ≈1. The ϵ (e.g. 10−5) prevents division by zero when a vector is near-constant.
x^i=σ2+ϵxi−μ
Step 4 — affine restore.Why? Forcing mean 0 / var 1 might be too rigid (e.g. a sigmoid wants inputs in a wider range). So we give each feature a learnable scale γi and shift βi. If the best thing is not to normalize, the network can learn γi=σ2+ϵ, βi=μ and recover the original.
yi=γix^i+βi
Across the features of a single example (a "row"), not across the batch.
BatchNorm normalizes across which dimension?
Across the batch for each fixed feature (a "column").
Formula for the normalized value x^i in LayerNorm
x^i=σ2+ϵxi−μ with μ,σ2 over that example's features.
Why add learnable γ and β?
To restore expressiveness — the net can rescale/shift or even fully undo normalization if that fits the next layer better.
Purpose of ϵ in LayerNorm?
Numerical stability; avoids division by ~0 when a feature vector is nearly constant.
Why is LayerNorm preferred in Transformers/RNNs over BatchNorm?
It's batch-independent, so it works for batch size 1 and per-token processing, and behaves identically at train and test time.
After standardization (ε=0), what are the mean and variance over the features?
Mean 0 and variance 1 exactly.
Is LayerNorm invariant to scaling the whole input vector?
Yes — scaling all features by a constant leaves x^ unchanged (before γ,β).
Does LayerNorm need running statistics at inference?
No; each example uses its own stats, same as training.
Recall Feynman: explain to a 12-year-old
Imagine each student (an example) answers a row of quiz questions (the features). One student's scores might all be huge, another's tiny — that makes the teacher's job messy. LayerNorm makes each student rescale their own row of scores so the average is 0 and the spread is 1. Now every row looks "normal" and the next teacher can grade fairly. It never mixes students together — each fixes only their own row. Then two knobs (γ,β) let the class adjust the final scale if the plain version was too strict.
Dekho, deep network mein har layer apne se pehle wali layer ka output leti hai. Jaise-jaise weights update hote hain, ye activations ka scale kabhi bahut bada, kabhi bahut chhota ho jaata hai — isse training slow ho jaati hai aur gradients phat ya mar jaate hain. Layer normalization ka kaam simple hai: har ek example ke andar jitne bhi features (neurons) hain, unka mean nikaal ke subtract karo aur standard deviation se divide karo, taaki har example ka row ban jaye "mean 0, variance 1". Isse next layer ko hamesha ek controlled scale ke numbers milte hain.
Sabse important baat: LayerNorm batch ke doosre examples ko nahi dekhta. Ye sirf ek example ke apne features par kaam karta hai (row-wise). Yahi wajah hai ki Transformers aur RNNs mein ye use hota hai — chahe batch size 1 ho ya aap ek-ek token process kar rahe ho, koi problem nahi. Aur training vs testing mein behaviour bilkul same rehta hai, BatchNorm ki tarah running-average ka jhanjhat nahi.
Normalize karne ke baad hum do learnable knobs lagate hain: γ (scale) aur β (shift). Kyun? Kyunki forced mean-0 var-1 hamesha best nahi hota — kabhi next activation ko wider range chahiye. In knobs se network khud decide karta hai kitna scale/shift chahiye, aur zarurat pade to normalization ko poora undo bhi kar sakta hai. Ek chhota sa ϵ bhi add karte hain taaki agar variance zero ke paas ho to divide-by-zero se NaN na aaye. Bas itna yaad rakho: Layer = line/row par normalize, Batch = column par.