- Introduced by
- Bahdanau, D.; Luong, M.T.
- Year
- 2015
- Type
- Neural attention layer (encoder-decoder)
What does it do?
The attention mechanism, introduced by Bahdanau, Cho and Bengio in 2015 and refined by Luong, Pham and Manning the same year, lets a sequence decoder dynamically learn which of the encoder's outputs to focus on at each step.
Before the Transformer, it substantially improved machine-translation quality by freeing models from compressing an entire input into a single fixed vector.
The intuition behind the method
A plain encoder-decoder squeezes a whole input sequence into one fixed context vector, which becomes a bottleneck for long sequences. Attention removes that bottleneck: at every output step the decoder looks back over all encoder states and computes a weighted blend, leaning hard on the few positions that matter for the word it is about to produce. It is like a translator glancing back at the most relevant source words rather than trying to remember the entire sentence at once.
History
Attention was introduced by Bahdanau, Cho and Bengio in 2015 for neural machine translation, learning to align and translate jointly so the model no longer had to compress the whole source sentence into one vector. The same year, Luong, Pham and Manning compared attention architectures, including global and local variants, and reported gains over non-attentional translation systems. Bahdanau et al. emphasised a bidirectional recurrent encoder, whereas Luong et al. used a unidirectional one. Later architectures, including the Transformer covered in a related page, built on this idea.
When to use it
Choose attention when the problem is sequence-to-sequence, such as machine translation, where an encoder reads an input sequence, a decoder produces an output sequence, and the decoder should draw on the most relevant encoder states at each step. It needs an encoder-decoder backbone that yields a set of encoder outputs to attend over, since the score, weight and context steps have nothing to act on without them. Do not use it for fixed-length feature tables, where there is no sequence of encoder states to weight and a non-sequential model is the more natural fit. Check that you have enough aligned input-output pairs before committing: with few examples the scoring parameters may be learned poorly and the model may overfit.
Limitations and considerations
- Requires an existing encoder-decoder backbone that produces encoder outputs; the mechanism operates on those states and cannot be added to a model that lacks them.
- Each decoding step scores every encoder output, so the scoring cost grows with the length of the input sequence.
- The scoring parameters are learned from the training pairs; when data are limited they may not be estimated reliably, and the model can overfit.
- Global attention always attends to all source positions, as the Luong et al. abstract describes, which adds computation compared with attending to a subset of positions.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Replaces a single fixed-length context vector with a context recomputed at every decoding step, which removes the bottleneck that limits plain encoder-decoder models on long inputs.
- The attention weights form an alignment between input and output positions that can be inspected to see which source positions influenced each output.
- Additive (Bahdanau-style) scoring learns the relevance function with a small feed-forward network, while multiplicative (Luong-style) scoring uses a dot-product, giving a choice between a more flexible and a cheaper scoring function.
- In the Luong et al. experiments on the WMT English-German translation tasks in both directions, local attention achieved a significant gain of 5.0 BLEU points over non-attentional systems that already incorporated techniques such as dropout.
Applications
- Neural machine translation between language pairs, the setting in which the original papers evaluated it.
- Other sequence-to-sequence tasks with an encoder-decoder design; the original evaluations of this method were carried out on translation.
- Producing interpretable input-output alignments to analyse which input positions influenced each output.
How it works
How it works
At each decoding step the current decoder state is compared against every encoder output to produce a relevance score. Bahdanau-style (additive) attention learns this score with a small feed-forward network, while Luong-style (multiplicative) attention uses a dot-product between query and keys, which is cheaper to compute.
Formula
e_{ij} = \text{score}(s_{i}, h_{j})The raw scores are passed through a softmax so they become a set of attention weights that are positive and sum to one across the input positions. These weights express how much focus each encoder output should receive for the current step, and can be inspected as an interpretable alignment between input and output.
Formula
\alpha_{ij} = \dfrac{\exp(e_{ij})}{\sum_{k} \exp(e_{ik})}The encoder outputs are combined into a single context vector as their weighted sum, using the attention weights. This dynamic context — different at every step — is fed to the decoder so it can generate the next element while attending to the most relevant parts of the input.
Formula
c_{i} = \sum_{j} \alpha_{ij}\, h_{j}Common pitfalls
- Applying attention to fixed-length tabular rows: a single feature vector gives no sequence of encoder states to weight, so the method has nothing to attend over.
- Reading attention weights as a complete explanation of the model; they show where the decoder weighted the input, not the full account of its reasoning.
- Expecting attention to compensate for a small or noisy training set; the weights are learned from the same data and can overfit.
- Using the dot-product (multiplicative) score without matching the dimensions of decoder states and encoder outputs, since the dot-product is only defined when they agree.
Frequently asked questions
What problem does attention solve?
It removes the fixed-vector bottleneck of plain encoder-decoder models. Instead of compressing the whole input into one context vector, the decoder builds a fresh weighted blend of all encoder states at each step, focusing on the positions that matter most.
What is the difference between Bahdanau and Luong attention?
Bahdanau (additive) attention learns the relevance score with a small feed-forward network, while Luong (multiplicative) attention uses a dot-product between query and keys, which is computationally cheaper.
How much data do I need?
There is no reliable fixed threshold given here. Attention needs enough paired input-output examples to learn its scoring parameters; with small datasets the model can overfit, so check whether your sample is adequate for your task before relying on it, and compare against a simpler baseline.
Can I read attention weights as an explanation?
The weights form an interpretable alignment that shows where the model focused, which is genuinely useful. Treat it as a helpful cue rather than a complete or definitive explanation of the model's reasoning.
Related methods
Sources
Bahdanau, D., Cho, K. & Bengio, Y. (2015). Neural Machine Translation by Jointly Learning to Align and Translate. ICLR.
Open publication referenceLuong, M.T., Pham, H. & Manning, C.D. (2015). Effective Approaches to Attention-based Neural Machine Translation. EMNLP, 1412–1421.
Open DOI reference2018_Book_NeuralNetworksAndDeepLearning, p. 429