Self-Attention in Transformers
The Bahdanau attention mechanism lets the decoder selectively access different parts of the input sequence at each decoding step.
A Translator Who Can Look Back
Imagine translating a sentence one word at a time while covering the original with your hand. Each time you need to choose the next word, you have only your memory of what you have read so far. Now imagine being allowed to lift your hand and glance back at any part of the original sentence whenever you need it. Attention mechanisms give neural networks that ability. This article introduces the idea, starting with the Bahdanau attention mechanism and ending with the link to the transformer.
As you read, keep one question in mind: when the network produces one output token, which input tokens does it actually use, and how much does each one matter? Answering that question is the thread running through every section below.
Why Relevance Depends on the Word
Read a sentence and you constantly decide which other words matter for understanding a given word. A pronoun such as 'it' is meaningless until you connect it to something earlier in the text. A word near the end of a long passage may depend on a phrase introduced many words before it. Language is full of these links, and they do not always sit next to each other.
The important consequence is that importance is not a fixed property of a word. Some neighbours carry most of the meaning, some carry little, and some far-away tokens carry a crucial clue. How much a word matters depends on which word is being interpreted at that moment.
What do you think happens?
In the sentence 'The cat sat on the mat because it was tired', which earlier word should 'it' be connected to in order to understand it?
Reveal answer
The cat
The pronoun 'it' is meaningless on its own. Understanding it means connecting it to the earlier noun it refers to, here 'cat', because it is the cat that was tired. The connection spans several words, which is exactly the kind of relationship the attention idea is meant to express.
What Bahdanau Attention Changes
An encoder-decoder RNN reads an input sequence, and the decoder then generates an output sequence. The Bahdanau mechanism keeps that overall structure but changes what the decoder is allowed to look at. The key word is selectively. At each decoding step, the decoder can access different parts of the input, and it does not have to rely on the same part every time. The mechanism was developed for RNNs in 2014 and is named after the first author of the paper that introduced it.
Without this change, the decoder is limited to what the encoder has carried forward from the input. With Bahdanau attention, the input tokens stay available for every output token. What changes from step to step is how strongly the decoder connects to each of them. The decoder therefore gets a fresh weighting of the input at every step instead of one unchanging picture of the whole sentence.
Reading the Attention Weights
The strength of each connection is controlled by attention weights. These are the weights that determine the importance of input tokens for generating a given output token. The source defers the details of how they are computed, so the important point for now is what they mean. A larger weight means an input token matters more for the output token currently being produced. A smaller weight means it matters less.
Weights belong to a pair of positions, not to a single input token. The word 'du' may matter a great deal when generating one output token and very little when generating another. That is why the decoder must make its choice again at each step. Reading the weights for one output token gives a row of values, one per input position, and the row changes when the output token changes.
Reading one row of attention weights
The input is 'This is an example' and the decoder is producing the English-side word that corresponds to 'Beispiel'. Which input positions should carry more weight for this output token, and how do you read the row?
- 1Fix the output token
Attention weights are always defined relative to one output token. Here the token being produced is the one for 'Beispiel'.
- 2List the input positions
Write the input tokens in order with their positions: This (0), is (1), an (2), example (3). Each position gets one weight for this output token.
- 3Compare the weights
A larger weight means that input position matters more for this output token. In the illustrative row, 'example' has the largest weight because it is the input word the output word corresponds to.
- 4Repeat for the next step
When the decoder produces a different output token, the same input positions get a new row of weights. The input did not change; the connection strengths did.
A row of weights, one per input position, shows how much each input token contributes to the current output token. The row is recomputed at every decoding step.
How Self-Attention Weighs Tokens Against Each Other
Transformers and large language models (LLMs) are built around a component called the self-attention mechanism, which is designed to handle the problem of linking words that depend on one another. Its job is to let the model weigh the importance of different words or tokens in a sequence relative to each other. Because the weighing happens between the words of the input itself, the mechanism is described as self-attention.
Self-attention is what allows a model to relate every position in an input to other positions, rather than reading each word in isolation. Where Bahdanau attention connects decoder positions to encoder positions, self-attention connects positions within one sequence. Each token is judged against the other tokens in the same input, and the judgement determines how much each neighbour should influence that token's representation.
Notice what this picture does and does not claim. It shows the kind of judgement self-attention is meant to make: deciding which parts of the input bear on which other parts. It does not show how the model computes those judgements. The source introduces the mechanism at this conceptual level and says nothing more specific about the arithmetic inside it.
Splitting the Work Between Encoder and Decoder
A transformer is a deep learning model architecture for language translation. In the simplified depiction the source gives, it is made of two parts: an encoder and a decoder. Self-attention appears within this larger structure. Understanding the jobs of the two parts makes it easier to see where the attention-based weighing of words does its work.
- The encoder receives the complete input text, here 'This is an example'.
- The encoder produces an embedding representation of that input.
- The decoder uses that representation to generate output one word at a time.
- When only the last word remains, the decoder generates 'Beispiel' to complete 'Das ist ein'.
- Self-attention works inside this structure, relating positions to one another while the words are being represented.
Why Self-Attention Reaches Long-Range Links
The source identifies two closely linked benefits of self-attention. First, it enables the model to capture long-range dependencies, meaning relationships between words that sit far apart. Second, it captures contextual relationships within the input, so that a token's role is shaped by its surroundings. Because every position can be related to other positions, a distant word is not out of reach simply because other words lie between it and the current token.
The source connects these capabilities to output quality. By capturing these relationships, the mechanism enhances the model's ability to generate output that is coherent and contextually relevant. The reason self-attention matters is not only that it sees many words at once, but that it uses those words to judge what each one means in its setting.
Bahdanau and Self-Attention Compared
The two mechanisms share an idea: a weighting that expresses how much one part of the data should influence the processing of another. They differ in where that weighting happens and in what architecture it lives inside. The source presents the transformer's self-attention mechanism as inspired by Bahdanau attention, and notes that it does not require RNN architectures.
| Feature | Bahdanau attention | Transformer self-attention |
|---|---|---|
| Setting | Encoder-decoder RNN | Within the transformer architecture |
| Which positions are weighed | Decoder positions against input tokens | Tokens against other tokens in the same sequence |
| Typical question answered | Which input tokens matter for this output token? | Which tokens in the input bear on which other tokens? |
| Architectural requirement | Used with RNNs | Does not require RNN architectures |
| Relationship | The original idea | Inspired by Bahdanau attention |
Both mechanisms weigh input positions by relevance, but they apply that weighing in different places.
Mistakes Beginners Make
Common mistake Treating attention weights as a fixed property of each input word
What it looks likeConcluding that 'du' is always important because it has a large weight once.
Why it failsThe source states that weights are not a property of the input token alone. The same token can matter a great deal for one output token and very little for another.
Do this insteadRead weights as a relationship between one output token and each input position, and expect the row to change at each step.
Common mistake Assuming Bahdanau attention and self-attention are the same mechanism
What it looks likeSaying that the transformer simply uses the encoder-decoder attention from the RNN model unchanged.
Why it failsBahdanau attention is attention between an encoder and a decoder. Self-attention is inspired by it, but it weighs tokens within the same sequence and sits inside the transformer architecture.
Do this insteadDescribe the relationship as inspiration and difference, not identity.
Common mistake Believing self-attention only helps when words are next to each other
What it looks likeThinking a pronoun can only be linked to the word directly before it.
Why it failsThe source explicitly describes long-range dependencies, where relationships sit far apart in the input.
Do this insteadExpect self-attention to relate tokens regardless of their distance within the input.
Common mistake Believing the diagram shows the exact implementation of Bahdanau attention. The source treats the token diagram as a general intuition, not an exact implementation.
What This Introduction Defers
This article establishes the conceptual foundation only. The source defers the detailed mechanics of self-attention to a later chapter. It also defers the computation of attention weights, so you should not yet expect to calculate them from the material here.
Practice: Tracing Dependencies
Take the sentence 'The dog chased the ball until it stopped rolling.' Identify one pair of words where the meaning of one word depends on the other, even though they are not adjacent. Then explain, without using the word 'weight', why a model that weighs words against each other would be more likely to link them than a model that reads each word in isolation.
Hints
- Start with the pronoun 'it' and ask what it refers to.
- Notice that the word it depends on sits several positions earlier in the sentence.
- Think about the difference between being able to see a word and judging what that word means in its setting.
For the output token 'Beispiel' in the example 'This is an example', write down the input positions in order and predict which one should have the largest attention weight. Then state what would have to change for the row of weights to look different when the decoder produces a different output word.
Hints
- Remember that weights are defined for one output token at a time.
- Ask which input word corresponds to the output word you are producing.
- Consider what happens to the row when the output token changes, even if the input stays the same.
Key Takeaways
- The Bahdanau attention mechanism keeps the encoder-decoder structure of an RNN but lets the decoder selectively access different parts of the input at each decoding step.
- Attention weights express how important each input token is for a specific output token; a larger weight means more importance, and the weights change from step to step.
- Self-attention lets a transformer weigh the importance of different words or tokens in a sequence relative to each other, and it was inspired by Bahdanau attention without requiring RNN architectures.
- In the transformer, the encoder processes the full input into an embedding representation and the decoder uses it to generate output one word at a time. Self-attention helps capture long-range dependencies and context.
- This introduction covers the concepts only; the detailed mechanics of self-attention and the computation of weights are deferred to a later chapter.