Sequence-to-sequence models
What Problem Does Seq2Seq Solve?
The challenge: Standard neural networks expect fixed input/output sizes. But in machine translation, summarization, chatbots, the input "Bonjour" → output "Hello" has 1 token → 1 token, while "Comment ça va?" → "How are you doing?" has 3 → 4 tokens.
The breakthrough: Decouple input processing from output generation.
- Encoder: Reads the entire input sequence and produces a context vector (the "thought").
- Decoder: Generates output sequence conditioned on , one token at a time.
Why this works: The context vector is a learned, compressed representation. The decoder learns to "unfold" this representation into the target language/format.
Architecture Derivation from First Principles
Step 1: The Encoder
We need to process a variable-length input. What structure handles sequences? RNNs, LSTMs, or GRUs.
Let the encoder be an LSTM. At each time step :
After processing all tokens, the final hidden state contains information about the entire input. We set:
Why the final state? In an LSTM, information flows forward. The last hidden state has "seen" all previous tokens (though it might forget early ones—this motivates attention later).
Step 2: The Decoder
The decoder generates the output sequence. At each decoding step :
Wait, what's ? The previous output token. During training, we use teacher forcing: feed the ground-truth previous token. During inference, we feed the model's own prediction.
The decoder's hidden state is passed through a softmax layer to predict the next token:
Why softmax? We're choosing one token from a vocabulary , so we need a probability distribution over classes.
The full sequence probability (assuming conditional independence given ):
Step 3: Training with Cross-Entropy
We have ground-truth pairs . The loss for one example:
where is the true target token at position .
Why negative log-likelihood? We want to maximize the probability of the correct sequence. Maximizing is equivalent to minimizing .
Backpropagation: Gradients flow backward through the decoder, into , then through the encoder. This is backpropagation through time (BPTT) across two RNNs.
Training vs. Inference: The Critical Difference
Training (Teacher Forcing)
Input: "I love AI"
Target: "<SOS> Me encanta IA <EOS>"
Decoder sees at t=1: <SOS> (from target)
Decoder predicts: "Me" (loss if wrong)
Decoder sees at t=2: "Me" (ground truth, not prediction!)
Decoder predicts: "encanta"
... and so on
Why teacher forcing? Speeds up training. The model gets the correct context at each step, rather than compounding errors.
Inference (Autoregressive Generation)
Input: "I love AI"
Decoder starts with <SOS>
t=1: Decoder predicts "Me" (from <SOS>)
t=2: Decoder uses "Me" to predict "encanta"
t=3: Decoder uses "encanta" to predict "IA"
t=4: Decoder predicts <EOS>, stop.
The exposure bias problem: At training, the decoder never sees its own mistakes. At inference, one wrong prediction can cascade. This is a known weakness of teacher forcing.
Worked Example: English → Spanish Translation
Input: "I am happy"
Target: "Estoy feliz"
Encoding Phase
Vocabulary: {I, am, happy} → {0, 1, 2}
Embed:
Encoder LSTM (simplified, ):
h_0 = [0,0,0]
h_1 = LSTM(x_1, h_0) → [0.2, -0.1, 0.5, 0.3]
h_2 = LSTM(x_2, h_1) → [0.4, 0.2, 0.1, -0.2]
h_3 = LSTM(x_3, h_2) → [0.1, 0.6, -0.3, 0.4]
Context vector c = h_3 = [0.1, 0.6, -0.3, 0.4]
Decoding Phase
Target vocab: {
Initialize decoder with :
Input: y_0 = <SOS> (token 0)
h_1^(dec) = LSTM(c, E[0], h_0^(dec))
Output: P(y_1|c) = softmax(W_o h_1^(dec))
= [0.01, 0.92, 0.05, 0.02] # Predicts "Estoy" (token 1)
Input: y_1 = "Estoy" (token 1, teacher forcing)
h_2^(dec) = LSTM(c, E[1], h_1^(dec))
Output: P(y_2|y_1,c) = [0.02, 0.03, 0.93, 0.02] # Predicts "feliz" (token 2)
Input: y_2 = "feliz" (token 2)
h_3^(dec) = LSTM(c, E[2], h_2^(dec))
Output: P(y_3|y_1,y_2,c) = [0.01, 0.02, 0.03, 0.94] # Predicts <EOS> (token 3)
Loss calculation (assuming these are the correct predictions):
Why this step? We sum negative log-probs of the correct tokens. Lower loss means higher confidence in correct sequence.
Common Decoding Strategies
1. Greedy Decoding
Pick the highest-probability token at each step:
Fast but suboptimal: Doesn't consider future consequences. Picking the best word now might lead to a dead-end later.
2. Beam Search
Maintain top- candidate sequences at each step. Expand, keep top- again.
Example with beam width :
Step 1: <SOS> → {"Estoy" (0.9), "Yo" (0.7)}
Step 2 (expand both):
"Estoy" → {"Estoy feliz" (0.9×0.8=0.72), "Estoy contento" (0.9×0.6=0.54)}
"Yo" → {"Yo soy" (0.7×0.5=0.35), "Yo estoy" (0.7×0.4=0.28)}
Keep top 2: {"Estoy feliz" (0.72), "Estoy contento" (0.54)}
Why beam search? Balances exploration and efficiency. Considers multiple paths without exhaustive search.
Special Tokens and the Mechanism
Problem: How does the decoder know when to stop?
Solution: Add a special end-of-sequence (EOS) token to the vocabulary. Train the model to predict <EOS> when the output is complete.
Training data:
Input: "Hello"
Target: "<SOS> Hola <EOS>"
At inference, we stop generating when the model outputs <EOS> or we hit a max length (safety).
Why <SOS>? The decoder needs an initial input. <SOS> (start-of-sequence) signals the beginning.
The Information Bottleneck Problem
Why it feels right: The LSTM's hidden state is designed to carry forward information.
The reality: A fixed-size vector (e.g., 512 dimensions) must encode everything about a potentially long input (e.g., 50 tokens, each with rich meaning). For long sequences, early information gets forgotten—the LSTM's memory is limited.
The fix: Attention mechanisms (next topic). Instead of one fixed , the decoder computes a different context vector at each decoding step by "looking back" at all encoder hidden states.
Steel-man the mistake: For short sequences (5-10 tokens), the bottleneck isn't severe. The original seq2seq paper (Sutskever et al., 2014) got good results on moderate-length sentences. The problem becomes critical for documents, paragraphs, or nuanced context.
Bidirectional Encoder Improvement
Limitation: A unidirectional encoder at token has only seen , not future tokens.
Enhancement: Use a bidirectional RNN/LSTM as encoder:
Concatenate:
The context vector becomes:
Why this helps: The encoder now has information about the entire sequence at every position. The word "bank" can be understood differently if followed by "river" vs. "money".
Input: "The cat sat on the mat. It was very comfortable."
Target: "Cat on mat."
Encoder
Tokenize input: ["The", "cat", "sat", "on", "the", "mat", ".", "It", "was", "very", "comfortable", "."]
Process through bidirectional LSTM →
Decoder
t=1: Input <SOS>, output "Cat" (prob 0.85)
t=2: Input "Cat", output "on" (prob 0.78)
t=3: Input "on", output "mat" (prob 0.82)
t=4: Input "mat", output "." (prob 0.91)
t=5: Input ".", output <EOS> (prob 0.95)
Loss:
Why each step?
- t=1: Decoder learns to pick the most salient subject.
- t=2-3: Learns to compress "sat on the" → "on".
- t=4: Punctuation is part of well-formed output.
- t=5: Termination signal.
When to Use Seq2Seq
| Use Case | Why Seq2Seq Fits |
|---|---|
| Machine Translation | Variable-length input/output, meaning transfer |
| Text Summarization | Long input → short output, content selection |
| Chatbots | Input query → response, dialogue context |
| Speech Recognition | Audio frames → text tokens |
| Video Captioning | Frame sequence → sentence |
Not ideal for: Tasks where input/output alignment is known (e.g., POS tagging—here, each input word gets one tag; simpler models suffice).
Recall Explain to a 12-Year-Old
Imagine you're playing a game of "telephone" but in two languages. You hear a sentence in English, and you need to whisper it in Spanish to the next person. But here's the trick: you can't just translate word-by-word because the sentence structure is different!
So you do it in two steps:
- Listen carefully to the whole English sentence and remember the idea in your head (that's the encoder making the context vector).
- Say it in Spanish, one word at a time, using what you remembered (that's the decoder).
The "context vector" is like a mental note you made: "They're talking about a happy cat on a mat." Then you speak Spanish words that match that mental note, not the exact English words. That's why the lengths can be different—you're translating ideas, not just words!
Or simply: "Understand, Remember, Speak" (Encoder understands, Context remembers, Decoder speaks).
Connections
- 3.5.05-LSTM-networks – Encoder/decoder typically use LSTMs to handle long sequences
- 3.5.06-GRU-networks – Alternative to LSTM in seq2seq, faster training
- 3.5.08-Attention-mechanism – Solves the context bottleneck by letting decoder "attend" to all encoder states
- 3.5.09-Transformers – Replace RNNs with self-attention, modern seq2seq backbone
- 3.4.03-Backpropagation-through-time – How gradients flow through seq2seq RNNs
- 4.2.01-Beam-search – Decoding strategy for seq2seq
- 2.3.02-Cross-entropy-loss – Loss function for training seq2seq models
- 3.5.01-Recurrent-neural-networks – Foundation of encoder/decoder architecture
#flashcards/ai-ml
What are the two main components of a seq2seq model? :: Encoder (processes input into context vector) and Decoder (generates output from context)
Why is teacher forcing used during training?
What is the context vector in seq2seq?
How does the decoder generate output tokens?
What is the information bottleneck problem?
What is beam search and why use it?
Why use bidirectional encoder?
What is the loss function for seq2seq training?
What signals the decoder to stop generating?
What's the difference between training and inference in seq2seq?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, seq2seq models ka concept bahut simple hai lekin powerful. Jab tumhe ek language se dosri language mein translate karna ho, ya phir ek lambi text ko summarize karna ho, toh input aur output ki lengths different ho sakti hain. Ek normal neural network fixed size expect karta hai, lekin translation mein "Namaste" ek word hai aur "Hello" bhi ek word, par "Aap kaise hain?" teen words hain aur "How are you?" bhi teen hain, lekin structure alag hai.
Seq2seq ne is problem ko solve kiya do parts mein – encoder aur decoder. Encoder pora input sentence padhta hai aur ek chota sa "meaning vector" (context vector) bana leta hai, jisme pori input ki information compressed form mein hoti hai. Sochlo jaise tumne ek paragraph padha aur ek sentence mein summary yad rakh li. Phir decoder us summary ko use karke output generate karta hai,ek token (word) at a time. Training mein hum "teacher forcing" use karte hain – matlab decoder ko actual correct previous word dikhate hain, lekin inference (real use) mein decoder apne hi predictions use karta hai.
Ismek badi problem hai jo "information bottleneck" kehte hain – agar input bahut lamba ho (50-100 words), toh ek fixed-size context vector mein sab kuch fit nahi ho pata, aur starting ki information bhool jati hai. Isliye bad mein "attention mechanism" aya, jo is problem ko solve karta hai. Lekin basic seq2seq ne machine translation aur chatbots ka foundation rakha, aur aj bhi simple tasks ke liye use hota hai. Ye architecture samajhna zaroori hai kyunki modern models (Transformers) bhi isi idea ko extend karte hain – encode karo, compress karo, decode karo. Ye technique sirf translation ke liye nahi, speech recognition, summarization, aur dialogue systems mein bhi kaam ati hai, jahan variable-length input/output natural hai.