(घटते और फूटते ग्रेडिएंट्स की समस्या)
🔶 1. Problem Statement:
जब DNN को train किया जाता है (backpropagation के ज़रिए), तो gradients को layers के बीच backward propagate किया जाता है।
लेकिन बहुत गहरी networks में, ये gradients:
- बहुत छोटे (near-zero) हो सकते हैं → Vanishing Gradients
- बहुत बड़े (extremely high) हो सकते हैं → Exploding Gradients
🔷 2. Vanishing Gradient Problem
📌 क्या होता है?
Gradient values इतनी छोटी हो जाती हैं कि weights effectively update ही नहीं हो पाते।
Training slow या completely stuck हो जाती है।
❗ क्यों होता है?
- जब activation functions (जैसे Sigmoid या Tanh) के derivatives हमेशा < 1 होते हैं
- और बहुत सी layers multiply होती हैं:

🧠 Impact:
- Deep layers almost learn nothing
- Early layers freeze
- Training fails
🔷 3. Exploding Gradient Problem
📌 क्या होता है?
Gradients बहुत तेजी से बड़े हो जाते हैं
→ Weights extremely large
→ Model becomes unstable
→ Loss: NaN या infinity
❗ क्यों होता है?
- जब weight initialization गलत हो
- या large derivatives repeatedly multiply होते हैं
🧠 Impact:
- Loss suddenly बहुत बड़ा
- Model unstable
- Numerical overflow
🔁 4. Visual Representation:
❌ Vanishing Gradient:
Layer 1 ← 0.0003
Layer 2 ← 0.0008
Layer 3 ← 0.0011
...
Final layers learn nothing
❌ Exploding Gradient:
Layer 1 ← 8000.2
Layer 2 ← 40000.9
Layer 3 ← 90000.1
...
Loss becomes NaN
✅ 5. Solutions and Fixes
Problem | Solution |
---|---|
Vanishing Gradient | ReLU Activation Function |
He Initialization (weights) | |
Batch Normalization | |
Residual Connections (ResNet) | |
Exploding Gradient | Gradient Clipping |
Proper Initialization | |
Lower Learning Rate |
✔ Recommended Practices:
- Use ReLU instead of Sigmoid/Tanh
- Initialize weights with Xavier or He initialization
- Add BatchNorm after layers
- Use gradient clipping in training loop:
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
🔧 PyTorch Example (Gradient Clipping):
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
📈 Summary:
Issue | Cause | Effect | Fix |
---|---|---|---|
Vanishing | Small gradients in deep layers | No learning | ReLU, He init, BatchNorm |
Exploding | Large gradients | NaN loss | Gradient clipping, Proper init |
📝 Practice Questions:
- Vanishing Gradient क्या है? इसे कैसे पहचानेंगे?
- Exploding Gradient से model पर क्या असर पड़ता है?
- Activation functions gradients को कैसे affect करते हैं?
- Gradient Clipping क्यों जरूरी होता है?
- Batch Normalization इन समस्याओं को कैसे कम करता है?