WHAT: Transform the input sequence into a context representation.
The encoder processes input tokens sequentially:
htenc=fenc(xt,ht−1enc)
where:
xt is the input at time t
htenc is the encoder hidden state
fenc is the recurrent function (LSTM/GRU cell)
WHY this recursion? Each hidden state accumulates information from all previous tokens. By t=Tx, the final hidden state has "seen" the entire sequence.
The context vector is typically:
c=hTxenc
or sometimes a function of all hidden states:
c=q({h1enc,h2enc,…,hTxenc})
WHY use the last hidden state? In theory, the final state contains a compressed representation of the entire sequence due to the recurrent nature of RNNs.
WHAT: Generate output tokens one at a time, conditioned on the context.
The decoder initializes with the context:
h0dec=c
Then generates tokens autoregressively:
htdec=fdec(yt−1,ht−1dec)
P(yt∣y<t,x)=softmax(Wohtdec+bo)
WHY autoregressive? At time t, we need to know what we've already generated (y<t) to produce the next token. The decoder's hidden state carries this history.
WHY softmax? It converts raw scores into a probability distribution over the vocabulary: ∑v∈VP(yt=v)=1.
We want to maximize the likelihood of the correct output sequence:
L=∑t=1TylogP(yt∗∣y<t∗,x)
where yt∗ is the ground-truth token at position t.
WHY this form? We decompose the joint probability using the chain rule:
P(y∗∣x)=∏t=1TyP(yt∗∣y<t∗,x)
Taking the log (for numerical stability and converting product to sum):
logP(y∗∣x)=∑t=1TylogP(yt∗∣y<t∗,x)
Teacher forcing: During training, we feed the ground-truth previous token yt−1∗ as input to the decoder at each step, even if the model predicted something else at t−1.
WHY? Faster convergence. Without it, early mistakes compound, and the model sees mostly its own errors during training.
Recall Feynman explanation (explain to a 12-year-old)
Imagine you're playing the "telephone game" with a robot friend, but the robot can only remember one sentence at a time.
The Encoder is like the robot listening to your entire message: "I want to go to the park play basketball." The robot can't remember every single word, so it writes down a short note: "Kid wants park basketball." That's the context — a super-short summary.
The Decoder is the same robot now telling your message to someone else. It reads its note ("Kid wants park basketball"), then tries to say the message word-by-word in a new language, like Spanish: "Quiero... ir... al... parque..." Each word it says helps it figure out the next word, like building with LEGO blocks one piece at a time.
The tricky part? The note might be too short! If your message was really long ("I want to go to the park near my house after lunch to play basketball with my friends and maybe get ice cream after"), the robot's tiny note can't capture everything. It might forget about the ice cream part! That's why scientists invented "attention" (the next topic) — so the robot can look back at your original words while it's speaking, not just its short note.
#flashcards/ai-ml
What are the two main components of an encoder-decoder architecture? :: The encoder (processes input sequence into a fixed-size context vector) and the decoder (generates output sequence conditioned on the context).
What is the context vector in encoder-decoder architecture?
A fixed-size vector representation of the entire input sequence, typically the final hidden state of the encoder: c=hTxenc.
Why is the decoding process called "autoregressive"?
Because the decoder generates each output token conditioned on all previously generated tokens: P(yt∣y<t,x).
What is teacher forcing in seq2seq training?
Feeding the ground-truth previous token as input to the decoder at each step during training, rather than the model's own prediction, to speed up convergence.
What is the main limitation of basic encoder-decoder architecture?
The information bottleneck: a fixed-size context vector cannot adequately represent long input sequences, causing performance degradation.
What is exposure bias in encoder-decoder models?
The mismatch between training (where the model sees ground-truth previous tokens via teacher forcing) and inference (where it sees its own predictions), causing errors to compound at test time.
Why is gredy decoding suboptimal for sequence generation?
Because locally optimal choices (highest probability at each step) don't guarantee globally optimal sequences; a lower-probability token now might enable higher-probability continuations later.
What is beam search and why use it?
A decoding algorithm that maintains k candidate sequences (beams) at each step instead of just one, exploring multiple hypotheses to find higher-probability complete sequences than greedy decoding.
Write the loss function for encoder-decoder training :: L(θ)=−N1∑i=1N∑t=1Ty(i)logPθ(yt(i)∣y<t(i),x(i)) (negative log-likelihood).
How does the decoder hidden state update at each step?
htdec=fdec(yt−1,ht−1dec) where yt−1 is the previous output token and ht−1dec is the previous hidden state.
What initializes the decoder in encoder-decoder architecture?
The context vector from the encoder: h0dec=c=hTxenc.
Why use softmax for output token probabilities?
To convert raw logits into a valid probability distribution: ensures all probabilities are positive and sum to 1, and emphasizes the most likely tokens through exponentiation.
Encoder-decoder architecture ek powerful technique hai jo variable-length input ko variable-length output mein convert karta hai. Socho tumhe ek English sentence ko Hindi mein translate karna hai - pehle tum pori English sentence ko READ karoge aur samajhoge (yeh ENCODER kaam hai), phir uska meaning yad rakhte hue Hindi mein word-by-word output doge (yeh DECODER ka kaam hai).
Encoder ek RNN/LSTM hota hai jo input sequence ke har token ko process karta hai aur final mein ek CONTEXT VECTOR banata hai - yeh basically pori input ki compressed summary hai, jaise WhatsApp pe long message ko "seen" karke uska gist yad rakhna. Decoder phir iss context vector se start karke, apne previous outputs ko dekhte hue next token predict karta hai, bilkul jaise tum kisi story ko retell karte ho step-by-step.
Iska sabse bada use machine translation mein hai (Google Translate type systems), lekin chatbots, text summarization, aur image captioning mein bhi kaam ata hai. Main challenge yeh hai ki agar input bahut lamba ho (50+ words), toh ek fixed-size context vector sab kuch capture nahi kar pata - information loss ho jata hai. Isi problem ko solve karne ke liye ATTENTION mechanism develophua, jo next topic hai. Training mein hum "teacher forcing" use karte hain jisme model ko correct previous word dete hain (not its own prediction), taki learning fast ho.