3.5.1 · HinglishSequence Models

Recurrent Neural Networks (RNN) architecture

2,924 words13 min readRead in English

3.5.1 · AI-ML › Sequence Models

Overview

Recurrent Neural Networks (RNNs) wo neural network architectures hain jo sequential data process karne ke liye design ki gayi hain — ye ek internal hidden state maintain karti hain jo memory ki tarah kaam karta hai. Feedforward networks ke ulat, jo har input ko alag se treat karte hain, RNNs mein connections hote hain jo khud par wapas loop karte hain, jisse time steps ke across information persist ho sakti hai.

Figure — Recurrent Neural Networks (RNN) architecture

Core Architecture Components

Derivation from First Principles

YE architecture kyun hai?

Problem se shuru karo: hum chahte hain ek function jo sequence ko outputs se map kare, lekin function ko pehle ke inputs "yaad" rehne chahiye.

Step 1: Memory ko ek vector ke roop mein encode karo

  • Maano hamaari time par memory hai
  • ko current input AUR pehli memory dono par depend karna chahiye

Step 2: Inputs ko linearly combine karo

  • Sabse general linear combination:
  • Linear kyun? Kyunki linear maps ko non-linearities ke saath compose karke hum koi bhi non-linear transformation seekh sakte hain

Step 3: Non-linearity add karo

  • Activation function apply karo:
  • kyun? Output mein hota hai, backprop ke dauran sigmoid ke comparison mein gradients zyada healthy rehte hain, aur ye zero-centered hai

Step 4: Output produce karo

  • Linear projection:
  • Hidden state se alag kyun? Isse hidden dimension output dimension se alag ho sakta hai; hidden state zyada rich ho sakta hai

Unfolding Through Time

"Recurrence" ko time ke through network ko unfolding karke visualize kiya ja sakta hai:

x^(1)    x^(2)    x^(3)
 |        |        |
 ↓        ↓        ↓
[RNN] → [RNN] → [RNN]
  ↓        ↓        ↓
 y^(1)   y^(2)   y^(3)

Har box SAME weights share karta hai. Boxes ke beech ke arrows represent karte hain.


Training: Backpropagation Through Time (BPTT)

ke liye Gradient Derive karna:

Chain rule se:

Time step ke liye:

Lekin depend karta hai par, jo depend karta hai par, ... tak wapas!

Full gradient:

Jahan:

Ye kyun matter karta hai? Jacobians ka ye product vanishing/exploding gradients ka karan banta hai.


RNN Variants and Types


Common Mistakes and Fixes


Key Properties

Property Description
Parameter Sharing Har time step par same weights → variable-length sequences handle karta hai
Hidden State memory ki tarah kaam karta hai, time tak sequence ka summary
Inductive Bias Ye assume karta hai ki paas ke elements door walon se zyada relevant hain
Limitation Vanishing gradients → poor long-term dependencies (LSTM/GRU se fix hota hai)

Computational Complexity

Length ki sequence ke liye:

  • Forward pass: (har step par matrix multiplications)
  • Backward pass (BPTT): (same, lekin gradients backward flow karte hain)
  • Space: (backprop ke liye hidden states store karo)

Feedforward se comparison:

  • Feedforward: Ek input ke liye , koi sequential dependency nahi
  • RNN: sequential operations, time ke across parallelize nahi ho sakta

Ye sequential nature RNN ki Transformers ke comparison mein key limitation hai.


Recall Ek 12 saal ke bachche ko samjhao

Socho tum ek detective story padh rahe ho. Har page tumhe ek clue deta hai. Ek normal robot brain (feedforward network) har page ko alag se dekhega aur jo pehle padha usse bhool jaayega — mystery solve karne ke liye bilkul bekar!

Ek RNN ek detective ki notebook ki tarah hai. Jab tum page 1 padhte ho, tum apni notebook mein important cheezein likhte ho. Page 2 par, tum NAYA clue padhte ho AUR apni notebook check karte ho ki pehle kya seekha tha. Tum is combined information se notebook update karte ho. Page 3? Same cheez — naya clue padho, notebook check karo, update karo.

Notebook "hidden state" () hai. Detective ka dimaag har page ke liye same thinking rules (weights ) use karta hai, lekin notebook ke contents badlte rehte hain jaise tum zyada seekhte ho. Last page tak, teri notebook mein POORI kitaab ke clues hain, jo tumhe kehne mein madad karte hain "The butler did it!"

Problem: Agar kitaab 1000 pages lambi hai, toh notebook page 1000 tak page 1 ki cheezein bhool sakti hai — yahi vanishing gradient problem hai. Behtar detectives (LSTM, GRU) ke paas special notebooks hoti hain jo zyada lambe time tak yaad rakh sakti hain!


Connections


#flashcards/ai-ml

RNNs mein hidden state ka kya purpose hai? :: Hidden state memory ki tarah kaam karta hai, jo saare pehle time steps ke baare mein information encode karta hai, jisse network sequence ke across context maintain kar sakta hai.

RNNs time steps ke across weights kyun share karte hain?
Weight sharing RNNs ko fixed number of parameters ke saath variable-length sequences handle karne deta hai, aur ek inductive bias provide karta hai ki sequence ke rules time ke saath nahi badalte (CNNs mein spatial weight sharing ke analogous).
RNN hidden state update equation likho
jahan recurrent weights hain, input weights hain, aur activation function hai.
RNNs mein vanishing gradients kya cause karta hai?
Backpropagation through time ke dauran, gradients mein Jacobians ke products hote hain . Agar , toh repeated multiplication gradients ko exponentially shrink kar deta hai, jisse long-term dependencies seekhna impossible ho jaata hai.
Many-to-one aur many-to-many RNN architectures mein kya fark hai?
Many-to-one ek input sequence process karta hai aur ek single output produce karta hai (jaise final hidden state use karke sentiment classification). Many-to-many ek input sequence process karta hai aur ek output sequence produce karta hai (jaise machine translation), potentially alag input/output lengths ke saath.
Har naye sequence ki shuruwat mein initialize kyun karte hain?
Training examples independent hote hain. Pehle sequence ka hidden state carry over karne se spurious correlations banti hain, jisse network un patterns ko seekhne ki koshish karta hai jo alag sequences ke across actually exist nahi karte.
Truncated BPTT kya hai aur isko kyun use karte hain?
Truncated Backpropagation Through Time gradient computation ko full sequence ki jagah time steps tak limit karta hai. Ye long sequences ke liye memory usage aur computation reduce karta hai, jabki pure sequence ke through forward propagation ab bhi allow karta hai.
RNN hidden state se output kaise produce karte hain?
Ek linear projection apply karo: , phir task-specific activation apply karo (classification ke liye softmax, regression ke liye kuch nahi). Isse hidden dimension output dimension se alag ho sakta hai.
Sequence length ke liye RNN forward pass ki computational complexity kya hai?
jahan hidden dimension hai. Ye har time step par matrix multiplications ki wajah se hota hai. Feedforward networks ke ulat, RNN operations sequential hain aur time ke across parallelize nahi ho sakte.
RNN hidden state mein sigmoid ki jagah activation kyun use karte hain?
sigmoid ke ki jagah range mein output deta hai (zero-centered), jo backpropagation ke dauran healthier gradients maintain karne mein madad karta hai aur hidden state mein bias shift issues rokta hai.

Concept Map

processed by

maintains

acts as

feeds back via

updates

via W_xh

tanh activation

via W_hy

reuses

gives

enables

solves

Sequential data

RNN cell

Hidden state h_t

Memory

Recurrent weights W_hh

Input x_t

Non-linearity

Output y_t

Weight sharing

Parameter efficiency

Generalization across time

No-memory problem of feedforward nets