3.2.9 · HinglishTraining Deep Networks

Layer normalization

1,686 words8 min readRead in English

3.2.9 · AI-ML › Training Deep Networks


YE EXIST KYU KARTA HAI?

Deep networks kai layers stack karte hain. Layer apne weights sikhti hai yeh assume karke ki inputs ki ek certain distribution hogi. Lekin woh inputs layer ke outputs hain, jinke weights bhi change ho rahe hain. Isliye layer ka input distribution training ke dauran shift hota rehta hai — informally ise internal covariate shift kehte hain. Bade ya lopsided activations se exploding/vanishing gradients bhi hote hain aur learning slow ho jaati hai.

BatchNorm ne yeh batch dimension across normalize karke fix kiya. Lekin isse har example ki statistics uske batch-mates se tie ho jaati hain. Yeh tab break hota hai jab:

  • batch tiny ho (noisy statistics),
  • aap ek time pe ek token process kar rahe ho (RNNs / autoregressive Transformers),
  • train vs. inference statistics alag hon.

KAISE: scratch se derive karo

Humare paas ek example ka pre-normalization vector hai.

Step 1 — features pe mean. Kyun? Zero pe recenter karne ke liye taaki layer high ya low biased na ho.

Step 2 — features pe variance. Kyun? Spread measure karne ke liye taaki hum ek controlled size pe rescale kar sakein.

Step 3 — standardize karo. Kyun? subtract karna ise center karta hai; se divide karna variance ko karta hai. (jaise ) division by zero se bachata hai jab ek vector near-constant ho.

Step 4 — affine restore. Kyun? Mean 0 / var 1 force karna bahut rigid ho sakta hai (jaise ek sigmoid ko wider range mein inputs chahiye). Isliye hum har feature ko ek learnable scale aur shift dete hain. Agar best cheez yeh ho ki normalize na karen, toh network , seekh sakta hai aur original recover kar sakta hai.

Figure — Layer normalization

BatchNorm vs LayerNorm — crucial axis

Normalize over Doosre examples pe depend karta hai? Train ≠ Inference?
BatchNorm batch (same feature, sab examples) Haan Haan (running stats)
LayerNorm features (sab features, ek example) Nahi Nahi — identical

Worked examples


Common mistakes (steel-manned)


The 80/20

  • LayerNorm = ek example ke features across normalize karo → mean 0, var 1 → phir learnable .
  • Yeh batch-independent hai ⇒ size-1 batches, RNNs, Transformers ke liye kaam karta hai, aur test time pe identical hai.
  • divide-by-zero prevent karta hai; expressiveness restore karte hain.

Flashcards

LayerNorm kis dimension pe normalize karta hai?
Ek single example ke features across (ek "row"), batch across nahi.
BatchNorm kis dimension pe normalize karta hai?
Har fixed feature ke liye batch across (ek "column").
LayerNorm mein normalized value ka formula
jahan us example ke features pe hain.
Learnable aur kyun add karte hain?
Expressiveness restore karne ke liye — network rescale/shift kar sakta hai ya normalization fully undo kar sakta hai agar next layer ke liye better ho.
LayerNorm mein ka purpose?
Numerical stability; ~0 se division avoid karta hai jab feature vector almost constant ho.
Transformers/RNNs mein LayerNorm ko BatchNorm ke upar kyun prefer kiya jaata hai?
Yeh batch-independent hai, isliye batch size 1 aur per-token processing ke liye kaam karta hai, aur train aur test time pe identically behave karta hai.
Standardization ke baad (ε=0), features pe mean aur variance kya hain?
Mean 0 aur variance exactly 1.
Kya LayerNorm pure input vector ko scale karne ke liye invariant hai?
Haan — sab features ko ek constant se scale karna ko unchanged chhod deta hai ( se pehle).
Kya LayerNorm ko inference pe running statistics chahiye?
Nahi; har example apne khud ke stats use karta hai, training jaisi hi.

Recall Feynman: 12-saal ke bacche ko explain karo

Socho har student (ek example) quiz ke questions ki ek row answer karta hai (features). Ek student ke scores sab huge ho sakte hain, doosre ke tiny — yeh teacher ka kaam messy bana deta hai. LayerNorm har student ko apne khud ke scores ki row rescale karne deta hai taaki average 0 ho aur spread 1 ho. Ab har row "normal" lagti hai aur next teacher fairly grade kar sakta hai. Yeh students ko kabhi mix nahi karta — har koi sirf apni row fix karta hai. Phir do knobs () class ko final scale adjust karne dete hain agar plain version bahut strict tha.


Connections

  • Batch normalization — batch-axis sibling; normalization dimension contrast karo.
  • Internal covariate shift — woh problem jise normalization mitigate karta hai.
  • Transformers — LayerNorm attention & feed-forward sublayers ke around apply hota hai.
  • Residual connections — LayerNorm ke saath Add & Norm ke roop mein combine hota hai.
  • RMSNorm — ek sasta variant jo mean-centering drop karta hai.
  • Vanishing and exploding gradients — kyun stable activation scales help karte hain.
  • Activation functions — kyun next nonlinearity ke liye matter karte hain.

Concept Map

motivates

fails on small batch and RNNs

Step 1

Step 2

centers

rescales

prevents div by zero

yields

Step 4

can undo normalization

Internal covariate shift

BatchNorm

Layer normalization

Mean over features mu

Variance over features

Standardize x-hat

Epsilon guard

Affine restore gamma beta

Output y

Zero mean unit variance