Vanishing and Exploding Gradients

(घटते और फूटते ग्रेडिएंट्स की समस्या)


🔶 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

ProblemSolution
Vanishing GradientReLU Activation Function
He Initialization (weights)
Batch Normalization
Residual Connections (ResNet)
Exploding GradientGradient 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:

IssueCauseEffectFix
VanishingSmall gradients in deep layersNo learningReLU, He init, BatchNorm
ExplodingLarge gradientsNaN lossGradient clipping, Proper init

📝 Practice Questions:

  1. Vanishing Gradient क्या है? इसे कैसे पहचानेंगे?
  2. Exploding Gradient से model पर क्या असर पड़ता है?
  3. Activation functions gradients को कैसे affect करते हैं?
  4. Gradient Clipping क्यों जरूरी होता है?
  5. Batch Normalization इन समस्याओं को कैसे कम करता है?

Deep Neural Networks (DNN)

(डीप न्यूरल नेटवर्क्स)


🔶 1. What is a Deep Neural Network?

📌 परिभाषा:

Deep Neural Network (DNN) एक ऐसा artificial neural network होता है जिसमें एक से ज़्यादा hidden layers होते हैं।

👉 यह shallow network (जैसे simple MLP जिसमें 1 hidden layer हो) से अलग होता है क्योंकि इसमें “depth” होती है — यानी कई layers जो input से output तक data को progressively abstract करती हैं।


🧠 Structure of a DNN:

Input Layer → Hidden Layer 1 → Hidden Layer 2 → ... → Hidden Layer N → Output Layer
  • हर layer neurons का group होता है
  • Each neuron applies:

z=w⋅x+b, a=f(z)

जहाँ f कोई activation function होता है


📊 Example:

मान लीजिए एक DNN जिसमें:

  • Input Layer: 784 nodes (28×28 image pixels)
  • Hidden Layer 1: 512 neurons
  • Hidden Layer 2: 256 neurons
  • Output Layer: 10 neurons (digits 0–9 classification)

🔷 2. Why Use Deep Networks?

❓ क्यों shallow networks काफी नहीं होते?

  • Shallow networks simple problems के लिए ठीक हैं
  • लेकिन complex tasks (जैसे image recognition, NLP, audio classification) में input-output relationship बहुत nonlinear होती है

✅ Deep networks:

  • High-level features को automatically extract कर सकते हैं
  • Abstractions को hierarchy में capture करते हैं

🧠 Hierarchical Feature Learning:

LayerLearns
Layer 1Edges, curves
Layer 2Shapes, textures
Layer 3Objects, faces

🔶 DNN की Architecture क्या होती है?

Architecture का मतलब होता है कि DNN में कितनी layers हैं, हर layer में कितने neurons हैं, activation functions क्या हैं, और input-output data का flow कैसा है।


📊 High-Level Structure:

Input Layer → Hidden Layer 1 → Hidden Layer 2 → ... → Output Layer

हर layer दो चीज़ें करती है:

  1. Linear Transformation z=W⋅x+b
  2. Activation Function a=f(z)

🔷 2. Components of a DNN Architecture

ComponentDescription
Input LayerRaw input data (e.g., image pixels, features)
Hidden LayersIntermediate processing layers (more = more depth)
Output LayerFinal predictions (e.g., class scores)
Weights & BiasesParameters learned during training
Activation FunctionsAdds non-linearity (ReLU, Sigmoid, etc.)
Loss FunctionMeasures prediction error
OptimizerUpdates weights using gradients (SGD, Adam)

🧠 Typical Architecture Example (MNIST Digits):

Layer TypeShapeNotes
Input(784,)28×28 image flattened
Dense 1(784 → 512)Hidden Layer 1 + ReLU
Dense 2(512 → 256)Hidden Layer 2 + ReLU
Output(256 → 10)Digit prediction + Softmax

🧮 3. Mathematical View


🔧 4. PyTorch Code: Custom DNN Architecture

import torch.nn as nn

class DNN(nn.Module):
def __init__(self):
super(DNN, self).__init__()
self.net = nn.Sequential(
nn.Linear(784, 512), # Input to Hidden 1
nn.ReLU(),
nn.Linear(512, 256), # Hidden 1 to Hidden 2
nn.ReLU(),
nn.Linear(256, 10) # Output Layer
)

def forward(self, x):
return self.net(x)

📈 Visualization of Architecture

[Input Layer: 784]

[Dense Layer: 512 + ReLU]

[Dense Layer: 256 + ReLU]

[Output Layer: 10 (classes)]

🔍 Key Architecture Design Questions

  1. कितनी hidden layers होनी चाहिए?
  2. हर layer में कितने neurons?
  3. कौन सा activation function चुनना है?
  4. क्या dropout, batch norm चाहिए?
  5. Loss function कौन सा है?

🎯 Summary:

ElementRole
LayersInput → Hidden(s) → Output
ActivationNon-linearity लाती है
DepthLayers की संख्या
WidthNeurons per layer
OptimizerGradient से weights update करता है

📝 Practice Questions:

  1. DNN की architecture में कौन-कौन से भाग होते हैं?
  2. Hidden layers कितनी होनी चाहिए — इससे क्या फर्क पड़ता है?
  3. Activation function का क्या महत्व है architecture में?
  4. DNN architecture में overfitting कैसे रोका जाता है?
  5. Architecture tuning कैसे किया जाता है?

🔶 Training a DNN

💡 Standard Process:

  1. Forward Pass: Prediction generate करना
  2. Loss Calculation: Prediction vs ground truth
  3. Backward Pass: Gradient computation
  4. Optimizer Step: Weights update

🚧 Challenges in Training Deep Networks:

ChallengeSolution
Vanishing GradientsReLU, BatchNorm, Residual connections
OverfittingDropout, Data Augmentation
Computational CostGPU acceleration, Mini-batch training

🔧 4. PyTorch Code: Simple DNN for Classification

import torch.nn as nn

class SimpleDNN(nn.Module):
def __init__(self):
super(SimpleDNN, self).__init__()
self.model = nn.Sequential(
nn.Linear(784, 512),
nn.ReLU(),
nn.Linear(512, 256),
nn.ReLU(),
nn.Linear(256, 10)
)

def forward(self, x):
return self.model(x)

🔬 5. Applications of DNNs

DomainUse Case
Computer VisionImage classification, Object detection
NLPText classification, Sentiment analysis
HealthcareDisease prediction from X-rays
FinanceCredit scoring, Fraud detection
RoboticsSensor fusion, control systems

📈 Summary:

TermMeaning
DNNNeural network with 2+ hidden layers
DepthRefers to number of layers
PowerLearns complex mappings from data
ChallengesVanishing gradients, Overfitting, Compute cost

📝 Practice Questions:

  1. DNN और shallow network में क्या फर्क है?
  2. DNN के training में कौन-कौन सी steps होती हैं?
  3. Vanishing gradient क्या होता है और इसे कैसे solve किया जाता है?
  4. PyTorch में DNN implement करने का तरीका बताइए।
  5. DNN किन-किन क्षेत्रों में प्रयोग किया जाता है?

Overfitting, Underfitting and Regularization

(ओवरफिटिंग, अंडरफिटिंग और रेग्युलराइजेशन)


🔶 1. Underfitting क्या है?

📌 परिभाषा:

Underfitting तब होता है जब model training data को भी सही से नहीं सीख पाता।

🔍 संकेत:

  • High training loss
  • Low accuracy (train & test दोनों पर)
  • Model simple है या data complex

🧠 कारण:

  • Model बहुत छोटा है
  • कम training epochs
  • Features अच्छे से represent नहीं किए गए

🔶 2. Overfitting क्या है?

📌 परिभाषा:

Overfitting तब होता है जब model training data को बहुत अच्छे से याद कर लेता है, लेकिन test data पर fail हो जाता है।

🔍 संकेत:

  • Training loss बहुत low
  • Test loss बहुत high
  • Accuracy train पर high, test पर low

🧠 कारण:

  • Model बहुत complex है (बहुत सारे parameters)
  • कम data
  • ज़्यादा epochs
  • Noise को भी सीख लिया model ने

📈 Summary Table:

TypeTrain AccuracyTest AccuracyError
UnderfittingLowLowHigh Bias
OverfittingHighLowHigh Variance
Just RightHighHighLow Bias & Variance

🔧 3. Regularization Techniques

🔷 Purpose:

Regularization techniques model को generalize करने में मदद करते हैं — यानी unseen (test) data पर बेहतर perform करना।


📌 Common Regularization Methods:

✅ A. L1 & L2 Regularization:

optimizer = torch.optim.SGD(model.parameters(), lr=0.01, weight_decay=0.001)  # L2

✅ B. Dropout:

  • कुछ neurons को randomly deactivate कर दिया जाता है training के दौरान
  • इससे model सभी features पर ज़रूरत से ज़्यादा निर्भर नहीं करता
nn.Dropout(p=0.5)

✅ C. Early Stopping:

  • जैसे ही validation loss बढ़ना शुरू हो जाए — training रोक दी जाती है
  • इससे overfitting रोका जाता है

✅ D. Data Augmentation:

  • Image, text, या audio data को थोड़ा modify करके training set को बड़ा बनाना
  • इससे model को general patterns सीखने में मदद मिलती है

✅ E. Batch Normalization:

nn.BatchNorm1d(num_features)

🔁 PyTorch Example with Dropout:

import torch.nn as nn

model = nn.Sequential(
nn.Linear(100, 50),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(50, 10)
)

🧠 Diagnostic Plot:

Epochs →📉 Train Loss📈 Test Loss
1–5High → LowHigh → Low
6–20LowStarts rising → Overfitting starts

🎯 Summary:

ConceptDefinitionSolution
UnderfittingModel कम सीखता हैBigger model, more training
OverfittingModel बहुत ज़्यादा सीख लेता हैRegularization
RegularizationGeneralization सुधारने की तकनीकDropout, L2, Data Augmentation

📝 Practice Questions:

  1. Underfitting और Overfitting में क्या अंतर है?
  2. Dropout कैसे काम करता है?
  3. L2 Regularization का loss function में क्या योगदान है?
  4. Early stopping क्यों काम करता है?
  5. Data augmentation overfitting से कैसे बचाता है?


Learning Rate, Epochs, Batches

(लर्निंग रेट, एपॉक्स, और बैचेस)


🔶 1. Learning Rate (सीखने की रफ़्तार)

📌 Definition:

Learning Rate (η) एक hyperparameter है जो यह नियंत्रित करता है कि training के दौरान weights कितनी तेज़ी से update हों।

यह Gradient Descent के update rule का हिस्सा होता है:


🎯 Learning Rate की भूमिका:

ValueEffect
बहुत छोटा (<0.0001)Slow learning, stuck in local minima
बहुत बड़ा (>1.0)Overshooting, unstable training
सही मध्यमSmooth convergence to minimum loss

📈 Visual Explanation:

  • Low LR: धीरे-धीरे valley में पहुंचता है
  • High LR: आगे-पीछे कूदता रहता है, valley मिस कर देता है
  • Ideal LR: सीधे valley में पहुँचता है

📘 PyTorch में Learning Rate:

optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

🔶 2. Epochs (Training Iterations over Dataset)

📌 Definition:

Epoch एक cycle होती है जिसमें पूरा training dataset once neural network में pass किया जाता है — forward + backward pass दोनों।

अगर आपके पास 1000 images हैं और आपने 10 epochs चलाए, तो model ने dataset को 10 बार देखा।


🎯 अधिक Epochs का मतलब:

  • Model को सीखने का ज्यादा मौका मिलता है
  • लेकिन overfitting का खतरा बढ़ता है

🔶 3. Batches और Batch Size

📌 Batch:

Dataset को छोटे-छोटे टुकड़ों (chunks) में divide करके training करना batch training कहलाता है।

हर batch पर forward और backward pass किया जाता है।

  • Batch Size: कितने samples एक साथ process होंगे
  • Common sizes: 8, 16, 32, 64, 128

🎯 Why Use Batches?

AdvantageExplanation
Memory Efficientपूरा dataset memory में लोड करने की ज़रूरत नहीं
Faster ComputationGPU पर vectorized तरीके से काम होता है
Noise helps generalizationStochastic updates model को overfitting से बचाते हैं

🔁 Relationship Between All Three:

ConceptDefinition
EpochOne full pass over the entire dataset
Batch SizeNumber of samples processed at once
IterationOne update step = One batch

Example:

  • Dataset size = 1000
  • Batch size = 100
  • Then, 1 epoch = 10 iterations
  • If we train for 10 epochs → total 100 iterations

🔧 PyTorch Code:

from torch.utils.data import DataLoader, TensorDataset
import torch

# Dummy data
X = torch.randn(1000, 10)
y = torch.randint(0, 2, (1000, 1)).float()
dataset = TensorDataset(X, y)

# DataLoader with batch size
dataloader = DataLoader(dataset, batch_size=64, shuffle=True)

# Training loop
for epoch in range(5): # 5 epochs
for batch_x, batch_y in dataloader:
# Forward pass, loss calculation, backward, step
...

📝 Summary Table:

TermMeaningTypical Value
Learning RateStep size for weight updates0.001 – 0.01
EpochOne full pass over dataset10 – 100
Batch SizeSamples per update32, 64, 128
IterationOne weight update stepdataset_size / batch_size

🎯 Objectives Recap:

  • Learning Rate = Weights कितना move करें
  • Epoch = Dataset कितनी बार pass हो
  • Batch Size = एक बार में कितने samples process हों
  • इन तीनों का tuning model performance के लिए critical है

📝 Practice Questions:

  1. Learning Rate क्या होता है और इसका काम क्या है?
  2. Batch Size और Iteration में क्या संबंध है?
  3. Overfitting का खतरा किस स्थिति में अधिक होता है: कम epochs या ज़्यादा epochs?
  4. PyTorch में DataLoader का क्या काम है?
  5. Batch training क्यों करना ज़रूरी होता है?

Back Propagation :Backward Pass (Gradient Descent)

Backward Pass (Gradient Descent)

(बैकवर्ड पास और ग्रेडिएंट डिसेंट)


🔶 1. Backward Pass क्या है?

Backward Pass (या Backpropagation) एक ऐसी प्रक्रिया है जिसमें neural network द्वारा की गई गलती (loss) को input की दिशा में “वापस” propagate किया जाता है — ताकि यह पता लगाया जा सके कि network की किस weight ने कितनी गलती की।

यह gradient information तब use होती है weights को सही दिशा में adjust करने के लिए ताकि अगली बार prediction बेहतर हो सके।


🎯 उद्देश्य:

“Neural network की prediction में हुई गलती को mathematically trace करके यह पता लगाना कि model के कौन-कौन से weights इस गलती के ज़िम्मेदार हैं, और उन्हें कैसे सुधारना है।”


🔄 2. Process Overview: Forward → Loss → Backward → Update

पूरा Training Loop:

Input → Forward Pass → Output → Loss Calculation → 
Backward Pass → Gradient Calculation →
Optimizer Step → Weight Update

🧮 3. गणितीय दृष्टिकोण: Chain Rule से Gradient निकालना

मान लीजिए:

  • y=f(x)
  • L=Loss(y,y^)

तो:

जहाँ:

  • w: model parameter (weight)
  • z=w⋅x+b
  • y=f(z) (activation function)

यहाँ हम chain rule का उपयोग कर एक neuron से अगले neuron तक derivative propagate करते हैं — यही कहलाता है backpropagation.


📘 4. Gradient Descent: Training की Core Algorithm

Weight Update Rule:

जहाँ:

  • η: learning rate (सीखने की रफ़्तार)
  • ∂L/∂w: loss का gradient उस weight के respect में
  • यह बताता है weight को किस दिशा और मात्रा में adjust करना है

⚠️ यदि Learning Rate बहुत बड़ी हो:

  • Model overshoot कर जाता है
  • Training unstable हो जाती है

⚠️ यदि बहुत छोटी हो:

  • Model बहुत धीरे सीखता है
  • Local minima में अटक सकता है

🔧 5. PyTorch Implementation Example:

import torch
import torch.nn as nn
import torch.optim as optim

# Model
model = nn.Sequential(
nn.Linear(2, 3),
nn.ReLU(),
nn.Linear(3, 1),
nn.Sigmoid()
)

# Data
x = torch.tensor([[1.0, 2.0]])
y = torch.tensor([[0.0]])

# Loss and Optimizer
criterion = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)

# ----------- Training Step ------------
# Step 1: Forward Pass
y_pred = model(x)

# Step 2: Loss Calculation
loss = criterion(y_pred, y)

# Step 3: Backward Pass
optimizer.zero_grad() # Clear old gradients
loss.backward() # Backpropagate
optimizer.step() # Update weights

🧠 6. Visual Explanation:

Training Flowchart:

          Prediction

Forward Pass

Loss Calculation

Backward Pass

Gradient w.r.t. Weights

Optimizer Step

Weights Updated

🔍 7. Roles of Key PyTorch Methods:

MethodPurpose
loss.backward()Gradient calculate करता है loss से सभी weights तक
optimizer.step()Calculated gradients को use करके weights update करता है
optimizer.zero_grad()पुराने gradients को clear करता है

💡 उदाहरण: Gradient कैसे काम करता है?

मान लीजिए model ने y^=0.8 predict किया और true label था y=1, तो loss होगा: L=(y−y^)2=(1−0.8)2=0.04

इसका gradient: dL/ dy =2(y^−y)=2(0.8−1)=−0.4

यह negative gradient बताता है कि prediction कम था, weight को बढ़ाने की ज़रूरत है।


📝 अभ्यास प्रश्न (Practice Questions):

  1. Backward Pass क्या करता है neural network में?
  2. Gradient Descent का update rule लिखिए
  3. PyTorch में loss.backward() किसका काम करता है?
  4. Chain Rule क्यों ज़रूरी है backpropagation में?
  5. Learning Rate अधिक होने से क्या खतरा होता है?

🎯 Objectives Recap:

  • Backward Pass = Loss से gradient निकालने की प्रक्रिया
  • Gradient Descent = Weights update करने की तकनीक
  • Chain Rule = Gradient को propagate करने का आधार
  • PyTorch ने इस पूरे process को automate किया है