Lesson 23 — Beginner

Deep Learning Foundations: CNN, RNN & LSTM

Deep learning ko formulaon ki list mat samjho — ye ek kahani hai: neurons → layers → task ke hisaab se architecture, aur saath mein wahi purana training loop (loss → gradients → updates) jo har model mein chalta hai. Is lesson mein tum CNN (image pe ek pattern detector), RNN (sequence ki running memory), LSTM (gates ke saath memory) dekhoge, aur sabse end mein wo line jisse transformers ne sequence tasks cheen liye — attention ne sequential bottleneck tod diya.

⏱ 20 min• Beginner• Machine Learning Foundations

Neural network ek kahani hai, formula list nahi

Kya hai?

Deep learning = bahut chhote calculators (neurons) ko layers mein jod kar data se khud features sikhna. Ek neuron: inputs × weights ka sum + bias, phir activation — output ek number. Hazaron chhote neurons milkar wo patterns pakadte hain jo tum haath se define nahi kar sakte (edge, texture, word order).

Kab kaam aata hai?

Jab features likhna mushkil ho — image pixels, audio samples, token sequences. Tabular data (rows/columns) pe chhote classical models aksar kaafi hote hain; deep tab jab raw input hi bahut bada aur structure-rich ho.

Kahan milta hai?

CNN image classifiers mein, RNN/LSTM purane speech, translation aur time-series models mein, aur aaj ke transformers ke andar bhi wahi neuron-layer-loss loop. Purane papers aur legacy code samajhne ke liye yahi lesson kaafi hai.

Ek hi loop har jagah chalta hai: forward pass (prediction) → loss (galti naapi) → backward pass (gradients nikaale) → update (weights thoda adjust). CNN, RNN, LSTM, transformer — ye sab sirf "kaunsi layer, kis order mein" ka farq hain. Isliye formulas ratne ki zarurat nahi; shapes aur data flow padho — wahi interview mein bhi poochha jaata hai.

Architecture ka rule: Data ki shape hi architecture chunti hai — grid (image) pe convolution, sequence (token/time) pe recurrent/attention, table pe classical. Pehle input shape likho, architecture khud saamne aayega.

Neuron se network tak — training loop ek story

# ek neuron ka forward + backward pass (haath se ginne layak)
x, y = 2.0, 1.0        # input, target
w, b = 0.5, 0.2        # weight, bias

pred = w * x + b       # 0.5*2 + 0.2 = 1.2
loss = (pred - y) ** 2  # (0.2)^2 = 0.04  <- squared error

# gradient: loss ko w se kaise badla (dL/dw = 2*(pred-y)*x)
g = 2 * (pred - y) * x  # 2 * 0.2 * 2 = 0.8
w = w - 0.01 * g        # chhota step: 0.5 -> 0.492 (loss kam ki taraf)
# yahi loop hazaar examples pe ghoomta hai — wahi "seekhna" hai
Gradient ka matlab: Gradient sirf ye bataata hai — weight badhao toh loss kitna badlega (aur kis taraf). Us direction mein ulta chhota kadam = loss kam. Backprop bas chain rule hai jo ye direction har weight tak pahunchata hai.

CNN — ek pattern detector, poore image pe

ek chhoti CNN ki tensor shapes (hand-checkable):

input        32 x 32 x 3     (RGB image)
conv 3x3 x8  30 x 30 x 8     (no padding: 32-3+1 = 30; 8 filters)
maxpool 2x2  15 x 15 x 8     (har 2x2 se ek max -> aadhi jagah)
flatten      1800            (15 * 15 * 8)
dense        10              (10 classes ke logits)

har conv layer: filter chhoti window liye poore image pe slide karta hai,
har position pe dot product -> feature map (kahan wo pattern mila)

Convolution ka mechanics simple hai: ek chhoti filter (3×3) image ke upar slide hoti hai aur har jagah dot product karti hai — output batata hai "wo pattern yahan kitna tha". Pehle layers edges/textures pakadti hain, aage jaake wo features objects ban jaate hain. Pooling size ghataati hai aur thoda position tolerance deti hai — maxpool mein "kahin bhi ek strong edge" kaafi hai, exact pixel nahi.

Weight sharing hi asli taakat: Agar har position ke liye alag weights hote (dense layer) toh har jagah ka pattern alag se seekhna padta — params explode. CNN mein ek hi detector sab jagah, isliye chhote data pe bhi generalise karta hai.

RNN ka memory — aur LSTM ke gates

RNN ka ek step:
  h_t = tanh(W_x @ x_t + W_h @ h_{t-1})   # aaj ka input + pichhla memory
  y_t = W_o @ h_t

sequence: "the cat sat ..." -> h1 -> h2 -> h3 -> ...   (ek-ek karke!)

problem: vanishing context — pichhle steps ka asar chhota hote jaata hai,
         door ka word yaad nahi rehta, long sequence train karna mushkil

LSTM ke teen gates (har step pe):
  forget gate  -> kya bhoolna hai (purana memory ka kitna rakhna)
  input gate   -> kya naya andar lena hai
  output gate  -> ab kya dikhana hai
  cell state   -> lambi highway: gradient ko lambi doori tak le jaati hai
Memory bottleneck yahi hai: Poora context ek chhote state se hokar guzarta hai, aur har step line mein. Ise hi "sequential bottleneck" kehte hain — iski wajah se RNN long sequences pe dheere aur kamzor the. Agle section mein dekhoge ise attention ne kaise toda.

Embeddings — transformers se pehle ki NLP duniya

purani duniya (word2vec era):
  "king" -> [0.21, -0.44, 0.90, ...]    # 100-300 dims, har shabd ek vector
  idea: jo cheezein meaning mein paas hain wo jagah bhi paas

pipeline: text -> tokens -> embedding lookup -> RNN/CNN -> label

one-hot kyun nahi: 50,000 shabd -> 50,000 dim, aur do vectors orthogonal
  (koi similarity nahi — "laptop" aur "notebook" zero milti hain)

embedding table ab bhi hai: transformer ka input bhi pehle embedding hota hai,
bas uske baad attention context jod deta hai
Embeddings maa hue nahi: Transformers ne static embedding tables ko replace nahi kiya — unhe context wale bana diya. Vector search, dedupe aur similarity clusters aaj bhi isi idea pe chalte hain, isliye purana samajhna aaj bhi useful hai.

Transformers ne kya replace kiya

RNN/LSTM                         TRANSFORMER
-------------------------------- ----------------------------------
step-by-step (serial)             saare tokens ek saath (parallel)
memory = ek chhota hidden state   attention: har pair ko direct path
long range -> dheere/dhundhla     long range -> ek hop, siddha
sequence lamba = training dheere  hardware pe batch parallel

jo NAHI badla:
  forward -> loss -> gradient -> update   (wahi purana loop)
  embeddings, layers, checkpoint/eval kaam karna
Legacy code se daro mat: Aaj bhi production mein CNN classifiers aur LSTM-based models chal rahe hain. Ab jo concepts seekhe — shapes, gates, memory bottleneck — tum un codebases bina dar ke padh paoge. Ye level isi liye course ke foundations mein rakha gaya hai.

Exercise

Question: Ek neuron ka output inputs ke weighted sum ke saath kya judta hai? (ek shabd)

Question: 32×32 input pe 3×3 filter chali (no padding) — conv ke baad side kitni? Sirf number daalo.

Question: RNN mein chal rahi running memory ka kya naam hai? (do shabd)

Question: LSTM ka kaunsa gate decide karta hai ki purane memory ka kitna rakhna hai?

Question: CNN mein ek hi filter image ke har position pe same weights kyun use karta hai — us property ka naam? (do shabd)

Common mistakes

Desi example: Waiter ka order — line mein vs board pe

Ek waiter order sunta hai ek-ek karke (RNN) — beech mein dhyan bhatka toh pichhla order gaya; isi liye usne checklist (LSTM gates) banayi: kya bhoolna hai, kya rakhna hai. Doosra waiter order board pe likh leta hai aur kabhi bhi kisi bhi line pe nazar daal sakta hai (attention) — sab ek saath. Wahi farq RNN se transformer tak hai:

text
RNN (waiter):   line 1 -> line 2 -> line 3    (ek-ek, kabhi bhoolta hai)
LSTM:           checklist gates (kya bhoolna, kya naya)
transformer:    poora board ek nazar          (sab ek saath, direct paths)
loop wahi rehta: prediction -> loss -> gradient -> update
Interview angle — yahan se sawaal aate hain
  • Q: Convolution hota kya hai? — Chhoti filter window poore image pe slide hoti hai, har jagah dot product se feature map banta hai; ek hi filter ka weights sab positions pe same (weight sharing).
  • Q: Pooling kyun lagate hain? — Size kam, thoda position tolerance, overfit kam — par detail khota hai. Isliye zaroorat se zyada pooling nahi.
  • Q: RNN ki vanishing context problem kya hai? — Pichhle steps ka asar gradient ke saath ghutta jaata hai, door ka context nahi seekh paaata; LSTM ne cell state + gates se sambhala.
  • Q: Transformers ne RNN ko kyun replace kiya? — Parallel training aur attention se direct long-range paths — dono taraf se RNN ka structure haarta tha. Loss loop wahi rehta hai.
  • Q: LSTM ke teen gates ka kaam batao? — Forget (purana kitna rakhna), input (kya naya andar), output (kya dikhana) — teeno learned sigmoids.
Lesson complete?

Architectures ki history clear — ab agla lesson Classical NLP: Cleaning, Tokenization & TF-IDF mein dekhenge text ko numbers mein badalne ka purana, sasta raasta — aur wo numbers haath se kaise bante hain.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course