3.4.9 · AI-ML › Convolutional Neural Networks
Residual Networks (ResNet) ne deep learning mein revolution la diya tha, kyunki inhone degradation problem ko solve kiya—yeh ek aisa counterintuitive observation tha ki deeper networks shallow ones se bhi worse perform kar sakti hain, training data par bhi. Iska key innovation hai skip connections (ya residual connections ), jo gradients ko directly network ke through flow karne deta hai.
Intuition Hum Bas Zyada Layers Stack Kyun Nahi Kar Sakte?
Tumhara sochna hoga: zyada layers → zyada capacity → better performance. Lekin experimentally, plain 56-layer networks ne 20-layer networks se bhi worse perform kiya training data par bhi . Yeh overfitting nahi hai (jo sirf test accuracy ko hurt karta)—deeper network literally training set utna achha nahi seekh sakta.
Kyun? Do compounding issues hain:
Vanishing gradients : Backprop mein, gradients layers se multiply hote hain. Sigmoid/tanh ke saath, gradients exponentially shrink hote hain: ( ∂ w 1 ∂ L = ∂ a L ∂ L ⋅ ∏ i = 2 L ∂ a i − 1 ∂ a i ⋅ ∂ w 1 ∂ a 1 ) . Agar har ∂ a i − 1 ∂ a i < 1 ho, toh early layers ko negligible updates milte hain.
Optimization difficulty : ReLU ke saath bhi (jo gradients mein help karta hai), bahut deep networks complex error surfaces create karte hain jisme kaafi saddle points aur local minima hote hain. Optimization stuck ho jaata hai.
Definition Degradation Problem
Degradation problem woh phenomenon hai jisme ek plain neural network mein zyada layers add karne se training accuracy pehle saturate hoti hai aur phir rapidly degrade ho jaati hai. Yeh overfitting se alag hai—yeh ek optimization failure hai, generalization failure nahi.
Maan lo tum chahte ho ki network ek mapping H ( x ) seekhe (input x diya hua desired output). Layers ko H ( x ) directly seekhane ki bajay, unhe residual F ( x ) = H ( x ) − x seekhao, phir x wapas add karo:
H ( x ) = F ( x ) + x
Yeh easier kyun hai? Agar optimal mapping identity ke kareeb hai (H ( x ) ≈ x ), toh F ( x ) ≈ 0 . Layer weights ko zero ki taraf push karna (yeh seekhna ki "kuch mat badlo") kaafi zyada easier hai, rather than nonlinear transformations ke through precise identity mapping seekhna.
Analogy : Agar tum apne goal ke 99% tak pahunch chuke ho, toh final 1% adjustment describe karna, poore 100% path ko scratch se redescribe karne se kaafi zyada aasaan hai.
Definition ResNet Block Types
Basic Block (ResNet-18, ResNet-34):
Bottleneck Block (ResNet-50, ResNet-101, ResNet-152):
Teen layers: 1 × 1 (dims reduce karo) → 3 × 3 → 1 × 1 (dims restore karo)
Kyun? 1 × 1 convolutions pehle lower dimensions par project karke computational cost reduce karte hain
Structure: Conv1×1 → BN → ReLU → Conv3×3 → BN → ReLU → Conv1×1 → BN → Add → ReLU
Model
Layers
Blocks
Parameters
Top-5 Error
ResNet-18
18
Basic
11.7M
10.76%
ResNet-34
34
Basic
21.8M
10.12%
ResNet-50
50
Bottleneck
25.6M
7.13%
ResNet-101
101
Bottleneck
44.6M
6.44%
ResNet-152
152
Bottleneck
60.2M
6.16%
Worked example Example 1: Basic Residual Block Forward Pass
Setup : Input x hai 64 × 56 × 56 (64 channels, 56×56 spatial). Block mein do 3 × 3 convs hain, dono 64 filters ke saath.
Forward pass :
Pehla conv: x → Conv 3 × 3 , 64 ( x ) → BN → ReLU
Output: 64 × 56 × 56
Yeh step kyun? 3 × 3 spatial filters se features extract karo, activations normalize karo, nonlinearity apply karo
Doosra conv: → Conv 3 × 3 , 64 → BN
Skip connection add karo: y = F ( x ) + x
Dono F ( x ) aur x hain 64 × 56 × 56 , toh direct addition kaam karta hai
Yeh step kyun? Seekhe gaye residual ko original input ke saath combine karo
Final activation: y → ReLU ( y )
Yeh step kyun? Residual aur identity ko combine karne ke baad nonlinearity apply karo
Result : Output hai 64 × 56 × 56 , input ke same. Block ne complete transformation ki bajay input mein adjustments seekhe.
Worked example Example 2: Projection Shortcut (Dimension Mismatch)
Setup : Input x hai 64 × 56 × 56 . Hum spatially downsample karna chahte hain (stride 2) aur channels 128 tak badhana chahte hain.
Problem : Agar F ( x ) output karta hai 128 × 28 × 28 , toh hum directly x add nahi kar sakte jo ki 64 × 56 × 56 hai.
Solution : Projection shortcut W s ⋅ x use karo
Forward pass :
Residual path F ( x ) :
Conv 1 × 1 , 64 filters: 64 × 56 × 56 → 64 × 56 × 56
Yeh step kyun? Bottleneck: expensive 3 × 3 conv se pehle computation reduce karo
Conv 3 × 3 , 64 filters, stride 2 : 64 × 56 × 56 → 64 × 28 × 28
Yeh step kyun? Stride 2 ke saath spatial downsampling dimensions ko half kar deta hai
Conv 1 × 1 , 128 filters: 64 × 28 × 28 → 128 × 28 × 28
Yeh step kyun? Target channel count tak expand karo
Shortcut path W s ⋅ x :
Conv 1 × 1 , 128 filters, stride 2 : 64 × 56 × 56 → 128 × 28 × 28
Yeh step kyun? F ( x ) se dono spatial dimensions (stride ke zariye) aur channels (128 filters ke zariye) match karo
Add karo: y = F ( x ) + W s ⋅ x
Dono ab 128 × 28 × 28 hain
Key insight : Projection W s seekha jaata hai lekin simple hai (sirf 1 × 1 conv). Zyaadatar computation F ( x ) mein hota hai.
Worked example Example 3: Gradient Flow Comparison
Setup : Ek 20-layer plain network aur 20-layer ResNet mein gradient flow compare karo.
Plain network :
∂ x 1 ∂ L = ∂ x 20 ∂ L ∏ i = 2 20 ∂ x i − 1 ∂ x i
Agar har ∂ x i − 1 ∂ x i = 0.8 ho (ReLU networks ke liye typical):
∂ x 1 ∂ L ≈ ∂ x 20 ∂ L ⋅ ( 0.8 ) 19 ≈ ∂ x 20 ∂ L ⋅ 0.0144
Yeh bura kyun hai : Gradient apni original magnitude ka sirf 1.44% reh gaya. Layer 1, layer 20 se ~70× slower seekhta hai.
ResNet with 10 residual blocks (2 layers each):
Har block contribute karta hai: ∂ x ∂ y = ∂ x ∂ F + I
Simplified gradient path (shortcuts consider karte hue):
∂ x 1 ∂ L = ∂ x 20 ∂ L ⋅ [ 1 + ∑ i = 1 10 ∂ x 1 ∂ F i ]
Chahe sab ∂ x 1 ∂ F i = 0 bhi ho, phir bhi hamare paas hai:
∂ x 1 ∂ L = ∂ x 20 ∂ L ⋅ 1
Yeh kyun matter karta hai : Gradient apni magnitude maintain karta hai. Layer 1 ko layer 20 ke jaisi hi signal strength milti hai, jo bahut deep networks mein effective learning enable karta hai.
Common mistake Mistake 1: "Skip connections sirf gradients ko backward flow karne mein help karte hain"
Yeh sahi kyun lagta hai : Gradient flow explanation sabse zyada commonly cited benefit hai, aur yeh sach bhi hai.
Kya incomplete hai : Skip connections forward propagation mein bhi help karte hain:
Ensemble effect : ResNets ko exponentially many shallow networks ka ensemble mana ja sakta hai (residual blocks ka har subset ek valid path banata hai)
Feature reuse : Early features (edges, textures) baad ki layers ko re-learn kiye bina directly accessible hoti hain
Implicit deep supervision : Har layer effectively loss ka shortcut rakhti hai, jisse ek implicit form of deeply-supervised learning create hoti hai
Fix : Skip connections dono forward aur backward flow problems solve karte hain. Yeh sirf ek gradient trick nahi hai—yeh fundamentally change kar deta hai ki information network ke through kaise propagate karti hai.
Common mistake Mistake 2: "Identity mapping hamesha kuch nahi seekhne se better hai"
Yeh sahi kyun lagta hai : Agar koi layer F ( x ) = 0 seekhti hai, toh y = x (identity), jo "default safe option" lagta hai.
Kya galat hai : Yeh reasoning initialization ko optimization ke saath confuse kar rahi hai. Claim yeh nahi hai ki identity optimal hai, balki yeh hai ki:
Identity kisi bhi specific transformation se seekhna easier hai jab random weights se shuruat karte hain
Residuals optimize karne mein easier hain kyunki function space mein identity ek special case hai (jab F = 0 ho)
Network training ke dauran blocks ko selectively use ya ignore kar sakta hai
Socho: Agar layer 50 ke paas already perfect features hain, toh layer 51 ke liye F = 0 (kuch mat karo) seekhna kaafi easier hai, rather than ek plain network ke liye exact identity mapping H ( x ) = x ko through seekhna.
Residual block y = F x + x