Encoder-Decoder Models for Sequence Generation
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 of the translation, 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 follows one idea through a neural sequence model. The model is an encoder-decoder RNN: an encoder reads an input sequence, and a decoder then generates an output sequence. The Bahdanau attention mechanism, introduced for RNNs in 2014 and named after the first author of the paper that proposed it, keeps this overall structure but changes what the decoder is allowed to look at. 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?
Following One Decoding Step
Before looking at the mechanism itself, trace what happens when the decoder produces a single output token. The source uses a text-generating decoder that translates a short German phrase into English. The input tokens are the German words, the output tokens are the English words, and a row of hidden states sits between them. The source's diagram focuses on the generation of the second output token.
Read the diagram as a picture of access rather than a fixed pipeline. When the decoder generates this one output token, every input token is available to it. Each input token is connected to the output token being generated, and the connections differ in strength. The diagram below shows the flow for one step: encoder states are compared with the decoder's current state, the resulting attention weights decide how much each input contributes, and the combined result informs the output token.
What the Bahdanau Mechanism Changes
The Bahdanau mechanism is attention between an encoder and a decoder. It keeps the encoder-decoder RNN structure: the encoder reads the input, the decoder generates the output. What changes is the decoder's access. At each decoding step the decoder can reach different parts of the input, and it does not have to rely on the same part every time. The key word in the definition is selectively: the decoder chooses which input parts matter for the token it is producing now.
Attention Weights as Relative Importance
The strength of each connection is controlled by attention weights. The source defines them as weights that determine the importance of input tokens for generating a given output token. The weights are computed by the attention mechanism, and the source defers the details of that computation. For now, the important point is what the weights mean: a larger weight means an input token matters more for the output token currently being produced, and a smaller weight means it matters less.
Reading one row of attention weights
Four input tokens are in the sentence. For the output token currently being generated, the attention weights are 0.05, 0.70, 0.20, and 0.05. Which input token matters most, and what does that say about the output token?
- 1Identify the weight row
All weights in the row belong to the same output token. They describe how much each input token matters for this one output token, not for the sentence as a whole.
- 2Compare the weights
The second input token has weight 0.70, larger than the others. A larger weight means that input token matters more for the output token currently being produced.
- 3Check the small weights
The first and fourth input tokens have weight 0.05 each, so they contribute little to this output token. They are still available to the decoder; the weights only express how much each one matters.
For this output token, the second input token carries the most importance. The weights are relative: they show how the input tokens compare for this particular output token.
The weight values in the worked example are illustrative numbers chosen for this lesson, not values from the source. The source's point is about meaning: a weight is a measure of importance relative to one output token, and the same input token can receive very different weights for different outputs.
Weights Change From Step to Step
The weights are not a property of the input token alone. The source uses the German word du as an example: it may matter a great deal when the decoder generates one output token and very little when it generates another. This is why the decoder must make its choice again at each step. As the decoder advances through the output sequence, the attention distribution is recomputed, so the input parts it relies on can shift.
Consider a generated two-step trace. At step one, the decoder is generating its first English word, and the weights concentrate on input token 2. At step two, the decoder generates its second word, and the weights concentrate on input token 4 instead. The input sequence is the same at both steps; only the decoder's position in the output has changed. Attention is therefore a per-step selection, not a single fixed choice made once for the whole sentence.
Common Mistakes With Attention Weights
Common mistake Treating an input token's weight as a fixed property of that token
What it looks likeDeciding that a word is always important because it had a large weight at one step.
Why it failsThe source notes that the weights depend on the output token being produced. The same input token can matter a great deal for one output and very little for another.
Do this insteadRead each weight as relative to the current output token, and expect the distribution to be recomputed at each decoding step.
Common mistake Believing the decoder reads only the highest-weighted input token
What it looks likeAssuming that a weight of 0.05 means the input token is ignored entirely.
Why it failsWeights express relative importance, and a smaller weight means the token matters less, not that it is unavailable. The source says all input tokens remain accessible.
Do this insteadThink of the weights as a graded pattern of connection strengths, where every input token has some connection to the current output.
Common mistake Assuming attention changes the encoder-decoder structure
What it looks likeThinking attention removes the encoder or makes the decoder read the input directly without any hidden states.
Why it failsThe Bahdanau mechanism keeps the overall encoder-decoder RNN structure and changes only what the decoder can access.
Do this insteadKeep the encoder reading the input and the decoder generating the output, and add the per-step access on top of that.
Common mistake Confusing the Bahdanau mechanism with the transformer's self-attention, as if they were the same component
From Bahdanau Attention to Self-Attention
Only three years after the Bahdanau mechanism, researchers found that RNN architectures are not required for building deep neural networks for natural language processing. They proposed the original transformer architecture, which includes a self-attention mechanism inspired by the Bahdanau attention mechanism. The conceptual step is this: Bahdanau attention operates between an encoder and a decoder, while the transformer's self-attention is inspired by it and is used within the transformer architecture.
| Aspect | Bahdanau attention | Transformer self-attention |
|---|---|---|
| Where it operates | Between an encoder and a decoder | Within the transformer architecture |
| Architecture it belongs to | Encoder-decoder RNN | Transformer, which does not require RNN architectures |
| Core idea | The decoder selectively accesses different input parts at each step | Inspired by the Bahdanau mechanism, as stated in the source |
| Source coverage | Explained in this article through attention weights | Only the relationship is established here; its internal workings are not described in this section |
How the two mechanisms relate, as established by the source
Practice Tracing Attention
A decoder with three input tokens is generating an output token. Its attention weights for this output token are 0.60, 0.30, and 0.10. Which input token matters most for this output token, and what would you expect to change at the next decoding step if the decoder's focus shifts to the third input token?
Hints
- Compare the three weights and identify the largest one.
- Remember that weights are recomputed at each decoding step, so the weight pattern for the next output token can be different.
- A smaller weight means the token matters less for this output token, not that it is unavailable.
Key Takeaways
- The Bahdanau attention mechanism keeps the encoder-decoder RNN structure 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 the input token matters more for the output currently being produced.
- The weights are not fixed properties of the input tokens, so they are recomputed at each decoding step as the output sequence advances.
- The transformer's self-attention mechanism was inspired by Bahdanau attention and is used within the transformer architecture, which does not require RNN architectures.