Deep Learning · Sequence Models
See why one sequence may need to become another sequence, how an encoder and decoder cooperate, and how attention replaces one fixed source summary with a different weighted context for every output step.
The Problem: Transform One Sequence Into Another
A normal classifier converts an input into one label. A sequence-to-sequence (seq2seq) task needs an ordered output whose length may differ from the input. Translation is the clearest example: “the machine stopped” has three source tokens, while a valid French translation can have four target tokens.
An encoder reads the source and creates representations of its meaning. A decoder produces the target, using the source representation and target tokens already known. Special start and end tokens tell the decoder when generation begins and ends.
Sequence-to-sequence learning maps one ordered series to another
The input and output do not need the same number of tokens.
source sequence
Encoder
↓
representations
Decoder
↓
one token at a time
target sequence
Why One Fixed Context Vector Is a Bottleneck
Early recurrent encoder–decoder systems often compressed the complete source into one final hidden vector. The decoder had to recover every needed detail from that one container. As sentences grew, names, relationships and word order could be difficult to preserve.
Attention removes that single-vector restriction. The encoder keeps a representation for every source position. At each output step, the decoder scores those representations, converts the scores to weights, and forms a new weighted context. Predicting “voiture” can focus on “car”; predicting “rouge” can focus on “red.”
Attention creates a new source summary for each output step
While predicting the French word “voiture,” this illustrative decoder focuses most on “car.”
weighted context
ct = Σ αt,ivi
used to predict
voiture
Attention Is a Weighted Lookup
Modern dot-product attention gives every item three learned views. If one token representation is x, the model calculates q=xWQ, k=xWK and v=xWV. The token representation comes from the previous layer; the three W matrices are initialized and then learned during training; Q, K and V are recalculated from the current input on every forward pass.
A query states what the current position needs. A key advertises what a candidate position contains. A value carries the information to retrieve. Query–key similarity determines how much of each value enters the result.
Scaled dot-product attention
Attention(Q,K,V) = softmax(QKᵀ / √dk)V
1. Multiply QKᵀ
Dot products measure each query–key match.
2. Divide by √dk
Scaling controls score size as vector width grows.
3. Apply softmax
Exponentiate and divide by the row sum to get positive weights totalling 1.
4. Multiply by V
Scale each value by its weight and add the results.
One query chooses between two source values
For a calculation we can see completely, manually choose q=[1,0], k₁=[1,0], k₂=[0,1], v₁=[10,2] and v₂=[2,8]. Their two components make dk=2; it is calculated from vector width, not chosen separately.
1. Scores
q·k₁=1×1+0×0=1
q·k₂=1×0+0×1=0
scaled=[1/√2,0]=[0.707,0]
2. Softmax
e⁰·⁷⁰⁷≈2.028, e⁰=1
sum=3.028
weights≈[0.67,0.33]
3. Retrieve
[6.70,1.34]+[0.66,2.64]
≈ [7.36,3.98]
Interpretation: the query matches key 1 more strongly, so the output resembles v₁ while still mixing in v₂. Real q, k and v vectors are model-produced; the values—not the keys—are what get averaged.
A Short Bridge to Transformer Attention
Self-attention lets positions within one sequence exchange information: queries, keys and values come from that same sequence. Cross-attention connects two sequences: decoder queries retrieve keys and values created by the encoder. Attention removes the fixed-context bottleneck; the next lesson shows what happens when attention becomes the main architecture.
What to Carry Into the Next Lesson
- Seq2seq learning transforms one ordered sequence into another, possibly with a different length.
- A recurrent encoder summarizes source information and a decoder predicts target tokens from source context and target history.
- One final encoder state can become a bottleneck for long or detailed inputs.
- Attention forms a new weighted source summary for each decoder step.
- Self-attention and cross-attention extend the same weighted-lookup idea; the next lesson shows how Transformers organize them into a full architecture.