Seq2Seq Attention
You’ll learn how an encoder-decoder model translates one sequence into another, and why squeezing a whole sentence into one vector creates a bottleneck. You’ll also see how cross-attention lets the decoder look back at every encoder state and reveal word-level alignment patterns.
You already know self-attention: every token attends to every other token in the same sequence. But where did this idea come from? It started with a simpler problem: how do you translate a long sentence without losing information?
How Encoder-Decoder Translation Works
Translation needs to read an entire input sentence, then produce an output sentence in a different language. The earliest neural approach split this into two parts: an encoder that reads the input word by word, and a decoder that generates the output word by word.
Step through the animation to see the full pipeline:
What to look for: the encoder processes words left to right, accumulating information into a hidden state. At the end, that final state becomes the context vector, the only thing passed to the decoder. The decoder then generates output words one at a time, consulting only this context vector. Step 4 reveals the problem: what happens when the sentence gets longer?
The Bottleneck
Toggle between a short and long sentence to see the problem more clearly:
Seq2Seq Bottleneck
What to look for: toggle to the long sentence and watch the per-word capacity bar shrink. The context vector doesn’t grow with the input; the same fixed-size vector must encode everything, so each word gets less space.
The context vector is always the same size. With 3 words, there’s plenty of room. With 8 words, each word gets a fraction of the capacity, and early words fade as the encoder overwrites them with newer information. For long sentences, details are inevitably lost.
The Fix: Look Back at Everything
Instead of one context vector for the entire output, compute a different context for each decoder step by looking back at all encoder states. At each step, the model assigns an attention weight to every encoder state (“how relevant is this input word right now?”), then blends all the encoder states together using those weights.
Click through decoder steps below. Watch how the attention weights shift as each French word is generated:
Seq2Seq Cross-Attention Alignment
English → French TranslationWhat to look for:
- When generating a target word, attention focuses on the most relevant source word(s)
- The attention pattern isn’t always left-to-right: it can reorder words across languages
- Each decoder step creates a different blend of source information
The result is a weighted sum, a custom-built context vector tailored to exactly what the decoder needs at this step. For translating “world” to “monde”, attention focuses mostly on the encoder state where “world” was encoded, not on all states equally.
Worked Example: Computing Attention Weights
Suppose we’re translating “I love cats” → “J’aime les chats” and the decoder has just output “J’aime”. Now it needs to generate the second word.
Recall from the encoder-decoder pipeline: the encoder processed each input word and produced a hidden state at each position (h1, h2, h3). The decoder RNN also has its own hidden state, s, which summarizes what it has generated so far. This decoder state acts as the query: “given what I’ve produced so far, what source information do I need next?”
Step through the four operations that turn these vectors into a prediction:
What to look for: in Step 1, the dot products reveal which encoder states match the decoder’s query. In Step 2, softmax turns those raw scores into weights that sum to 1. In Step 3, the weighted sum blends encoder states into a context vector dominated by the highest-weighted word. Step 4 closes the loop: the context vector feeds into the final prediction.
Cross-Attention vs. Self-Attention
Seq2seq attention lets the decoder attend to the encoder: one sequence looking at another. This is called cross-attention. You’ve already seen the other variant, self-attention, where every token attends to tokens in the same sequence.
| Cross-Attention (this page) | Self-Attention | |
|---|---|---|
| What attends to what | Decoder looks at encoder (two sequences) | Each token looks at all tokens (one sequence) |
| Question it answers | ”What input word is relevant to my output?" | "What other words in my sentence are relevant to me?” |
| Where it’s used | Translation, summarization | Every transformer layer |
The core mechanism is identical: query-key matching followed by a weighted sum of values. The only difference is where Q, K, and V come from. In self-attention, all three come from the same sequence. In cross-attention, Q comes from one sequence and K, V from another.
Quick Summary
| Concept | What Happens | Intuition |
|---|---|---|
| Vanilla seq2seq | Encoder compresses entire input into one fixed vector; decoder reads only that | Like summarizing a novel in a single tweet: something has to give |
| Bottleneck problem | Longer inputs lose more information in the fixed-size vector | 100 words sharing 512 floats means ~5 floats per word |
| Attention | Decoder computes a fresh context vector at each step by weighting all encoder states | Like re-reading the relevant parts of the source for each word you translate |
| Cross vs. self-attention | Cross: decoder attends to encoder. Self: every token attends to its own sequence | Same mechanism, different source of Q, K, V |
- I can explain the bottleneck problem in vanilla seq2seq and why it gets worse with longer inputs
- I understand how attention computes a different context vector for each decoder step
- I can trace through a worked example: dot products, softmax, weighted sum
- I can interpret an attention alignment matrix and explain what diagonal vs. non-diagonal patterns mean
- I understand how cross-attention relates to self-attention (same mechanism, different Q/K/V sources)
Next: The Full Transformer
You’ve now seen the full attention toolkit: self-attention within a sequence, multiple heads for parallel perspectives, positional encoding for word order, and cross-attention between sequences. Time to see how all the pieces fit together. Architecture Overview →