What is Batch Normalization

Batch Normalization (BatchNorm) एक technique है जो training को stabilize, accelerate और improve करने के लिए इस्तेमाल होती है।

यह हर layer के output (activations) को normalize कर देती है ताकि उनका distribution mean=0 और variance=1 के आस-पास रहे।

इससे gradients ज़्यादा smooth होते हैं और training तेज़ होती है।


🔁 2. क्यों ज़रूरी है?

Deep networks में, जैसे-जैसे layers बढ़ती हैं, activations का distribution shift होने लगता है — इस समस्या को कहते हैं:

📉 Internal Covariate Shift

BatchNorm इसका समाधान है — यह हर batch के output को rescale और re-center करता है।


🧮 3. Mathematical Explanation

मान लीजिए किसी layer का output x है।

Step 1: Mean और Variance निकालना

Step 2: Normalize

Step 3: Scale and Shift

यहाँ:

  • γ, β सीखने योग्य parameters हैं
  • ϵएक छोटा constant है stability के लिए

🔧 4. PyTorch Implementation

import torch.nn as nn

model = nn.Sequential(
nn.Linear(128, 64),
nn.BatchNorm1d(64), # BatchNorm for 1D input
nn.ReLU(),
nn.Linear(64, 10)
)

For images, use nn.BatchNorm2d(num_channels)


📈 5. Benefits of BatchNorm

BenefitExplanation
✅ Faster TrainingSmoother gradients → fast convergence
✅ Higher Learning RatesWithout instability
✅ Reduced Need for DropoutActs as light regularizer
✅ Mitigates Vanishing/Exploding GradientsKeeps activations in check
✅ Generalization ImprovesBetter test accuracy

🔍 6. Where to Apply?

TypeApply BatchNorm After
Linear (Dense)Linear → BatchNorm1d → Activation
Conv2D LayerConv2d → BatchNorm2d → Activation

⚠️ 7. Training vs Inference

  • During training → mean & variance per-batch
  • During inference → running average of mean & variance

PyTorch automatically handles this internally using .train() and .eval() modes.


🔁 With and Without BatchNorm (Effect on Accuracy):

EpochWithout BatchNormWith BatchNorm
562%79%
1071%87%
2076%91%

📝 Practice Questions:

  1. Batch Normalization का मुख्य उद्देश्य क्या है?
  2. Internal Covariate Shift किसे कहते हैं?
  3. PyTorch में BatchNorm1d और BatchNorm2d में क्या अंतर है?
  4. BatchNorm में γ और βका क्या role है?
  5. क्या BatchNorm dropout की तरह regularization भी करता है?

🎯 Summary:

FeatureBatchNorm Impact
Stability⬆️ Improves
Speed⬆️ Faster Training
Generalization✅ Helps prevent overfitting
Gradient Flow✅ Prevents vanishing/exploding