3.5.8 · HinglishSequence Models

Encoder-decoder architecture

2,337 words11 min readRead in English

3.5.8 · AI-ML › Sequence Models

Encoder-decoder architecture kya hai?

Yeh kaam kaise karta hai? Step-by-step derivation

Step 1: Encoding phase

KYA: Input sequence ko ek context representation mein transform karo.

Encoder input tokens ko sequentially process karta hai:

jahan:

  • time par input hai
  • encoder hidden state hai
  • recurrent function hai (LSTM/GRU cell)

YEH RECURSION KYUN? Har hidden state saare pichle tokens ki information accumulate karta hai. tak, final hidden state ne poori sequence "dekh" li hoti hai.

Context vector typically hota hai:

ya kabhi kabhi saare hidden states ka ek function:

LAST HIDDEN STATE KYUN USE KARO? Theory mein, RNNs ki recurrent nature ki wajah se final state poori sequence ki ek compressed representation contain karta hai.

Step 2: Decoding phase

KYA: Output tokens ek-ek karke generate karo, context par conditioned hokar.

Decoder context se initialize hota hai:

Phir tokens autoregressively generate karta hai:

AUTOREGRESSIVE KYUN? Time par, humein pata hona chahiye ki humne pehle kya generate kiya hai () taaki agla token produce kar sakein. Decoder ka hidden state yeh history carry karta hai.

SOFTMAX KYUN? Yeh raw scores ko vocabulary par ek probability distribution mein convert karta hai: .

Step 3: Training with teacher forcing

Loss function ki derivation:

Hum correct output sequence ki likelihood maximize karna chahte hain:

jahan position par ground-truth token hai.

YEH FORM KYUN? Hum joint probability ko chain rule use karke decompose karte hain:

Log lete hain (numerical stability ke liye aur product ko sum mein convert karne ke liye):

Teacher forcing: Training ke dauran, hum har step par decoder ko ground-truth previous token as input dete hain, chahe model ne par kuch aur hi predict kiya ho.

KYUN? Faster convergence. Iske bina, early mistakes compound hoti hain, aur model training mein mostly apni khud ki galtiyan dekhta hai.

Worked examples

Common pitfalls aur misconceptions

Limitations aur kab yeh fail karta hai

  1. Long sequences: Context vector bottleneck (attention se solve hota hai)
  2. Rare words: Model unseen vocabulary generate nahi kar sakta (subword tokenization, copy mechanism se solve hota hai)
  3. Exposure bias: Training-test mismatch (scheduled sampling se partially address hota hai)
  4. Slow inference: Sequentially decode karna padta hai, koi parallelization nahi (Transformer ka decoder training ke dauran parallelize kar sakta hai)

Applications

  • Machine translation (original use case: Sutskever et al., 2014)
  • Text summarization
  • Conversational AI / chatbots
  • Image captioning (CNN encoder + RNN decoder)
  • Speech recognition (audio encoder + text decoder)
  • Code generation (natural language → code)

Connections

Recall Feynman explanation (ek 12-saal ke bacche ko explain karo)

Socho tum ek robot dost ke saath "telephone game" khel rahe ho, lekin robot ek baar mein sirf ek sentence yaad rakh sakta hai.

Encoder jaisa robot teri poori baat sun raha hota hai: "I want to go to the park play basketball." Robot har ek word yaad nahi rakh sakta, toh ek short note likhta hai: "Kid wants park basketball." Yahi context hai — ek super-short summary.

Decoder wahi robot hai jo ab teri baat kisi aur ko sunaa raha hai. Woh apna note padhta hai ("Kid wants park basketball"), phir kisi nayi language mein, jaise Spanish mein, word-by-word message bolne ki koshish karta hai: "Quiero... ir... al... parque..." Har word jo woh bolta hai, usse agla word figure out karne mein help karta hai, jaise LEGO blocks ek-ek karke lagana. Tricky part kya hai? Note bahut chhota ho sakta hai! Agar teri message bahut lambi thi ("I want to go to the park near my house after lunch to play basketball with my friends and maybe get ice cream after"), toh robot ka tiny note sab capture nahi kar sakta. Woh ice cream waala part bhool sakta hai! Isliye scientists ne "attention" invent kiya (agla topic) — taaki robot teri original baaton ko dobara dekh sake jab woh bol raha ho, sirf apne chhote note par nahi.

#flashcards/ai-ml

Encoder-decoder architecture ke do main components kya hain? :: Encoder (input sequence ko ek fixed-size context vector mein process karta hai) aur decoder (context par conditioned hokar output sequence generate karta hai).

Encoder-decoder architecture mein context vector kya hota hai?
Poori input sequence ki ek fixed-size vector representation, typically encoder ka final hidden state: .
Decoding process ko "autoregressive" kyun kaha jaata hai?
Kyunki decoder har output token saare pehle generate kiye tokens par conditioned hokar generate karta hai: .
Seq2seq training mein teacher forcing kya hota hai?
Training ke dauran har step par decoder ko model ki apni prediction ki jagah ground-truth previous token as input dena, convergence speed up karne ke liye.
Basic encoder-decoder architecture ki main limitation kya hai?
Information bottleneck: ek fixed-size context vector lambi input sequences ko adequately represent nahi kar sakta, jisse performance degrade hoti hai.
Encoder-decoder models mein exposure bias kya hota hai?
Training (jahan model teacher forcing se ground-truth previous tokens dekhta hai) aur inference (jahan woh apni predictions dekhta hai) ke beech ka mismatch, jisse test time par errors compound hoti hain.
Greedy decoding sequence generation ke liye suboptimal kyun hota hai?
Kyunki locally optimal choices (har step par highest probability) globally optimal sequences guarantee nahi karte; ek lower-probability token abhi baad mein higher-probability continuations enable kar sakta hai.
Beam search kya hai aur ise kyun use karte hain?
Ek decoding algorithm jo har step par sirf ek ki jagah candidate sequences (beams) maintain karta hai, greedy decoding se zyada higher-probability complete sequences dhundhne ke liye multiple hypotheses explore karta hai.

Encoder-decoder training ke liye loss function likho :: (negative log-likelihood).

Decoder ka hidden state har step par kaise update hota hai?
jahan previous output token hai aur previous hidden state hai.
Encoder-decoder architecture mein decoder ko kya initialize karta hai?
Encoder ka context vector: .
Output token probabilities ke liye softmax kyun use karte hain?
Raw logits ko ek valid probability distribution mein convert karne ke liye: ensure karta hai ki saari probabilities positive hain aur sum to 1 hain, aur exponentiation ke through most likely tokens ko emphasize karta hai.

Concept Map

processed by

produces

compressed into

initializes

autoregressive gen

samples token

feeds back y_t-1

feeds ground truth

scored by

maximize likelihood

Input sequence x

Encoder RNN/LSTM/GRU

Encoder hidden states h_enc

Context vector c

Decoder RNN

Softmax over vocabulary

Output sequence y

Teacher forcing

Log-likelihood loss