Attention Intuition › A Step-by-Step Worked Example
Language AI HandbookAll chapters Attention Intuition › A Step-by-Step Worked ExampleOne section · Sections in this chapter6 / 20 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To cement the mechanics, let's walk through attention computation for a complete sentence, step by step, with concrete numbers.
We will use the translation of "The cat sat" to French, simplified to focus on the attention computation. The encoder has processed the English sentence and produced three hidden states:
These are 2-dimensional vectors to keep the arithmetic readable. In practice they would be hundreds of dimensions.
Step t=1: Generating "Le" (The)
The decoder starts with an initial state (encoding "start of translation, producing an article").
Alignment scores using dot product:
Applying softmax to get attention weights (using of each score divided by the sum):
Context vector (weighted sum of encoder states):
The attention is fairly distributed here, which makes sense for generating an article: "Le" is mostly determined by the subject noun ("cat"), but generating it requires knowing the overall sentence structure.
Step t=2: Generating "chat" (cat)
After generating "Le," the decoder advances to state (now oriented toward producing the noun). The attention query shifts dramatically toward :
The score for position 2 ("cat") is now much higher. After softmax:
Now over half the attention weight falls on "cat." The context vector will be dominated by . This provides the decoder with a strong signal about the noun to generate.
Step t=3: Generating the verb
For the verb phrase, the decoder state would shift again to attend primarily to ("sat"). This illustrates how attention tracks the linguistic content being generated at each step, automatically shifting focus as the translation proceeds through different parts of the source sentence.
This step-by-step trace reveals something important: the attention weights are not predetermined. They emerge from the interaction between the decoder's current state and the encoder states at each step. As the decoder evolves through the translation, its internal state changes, and those changes shift which encoder positions receive high attention. The mechanism is fully dynamic, computed fresh at every generation step.