TL;DR
Neural machine translation (NMT) used to squeeze a whole source sentence into one vector before translating it. This paper tests two simple ways to let the decoder look back at every source word while it writes each target word. The global model weighs all source words; the local model first picks one position and looks only at a small window around it. The best single English-to-German system gained 5.0 BLEU over a non-attentional baseline that already used dropout, and an ensemble of eight models scored 25.9 BLEU on WMT'15 English-to-German.
Builds on: Attention Mechanism, Encoder-Decoder Architecture, Long Short-Term Memory · Leads to: Attention Is All You Need
Why it mattered
Attention-based NMT was the usual design in the years before the Transformer. The paper showed, with numbers, that a decoder which reads the source states directly translates names and long sentences better than one that only sees a fixed summary. It also compared scoring functions: the plain dot product between the decoder state and each encoder state worked well for global attention, and a learned bilinear form worked best for local attention. The dot-product scoring idea is the same family that the Transformer paper (Attention Is All You Need) later uses inside its attention layers.
The paper also made the cost of attention concrete. Looking at every source word for every target word is expensive when the input is a paragraph, and the local model was built to score only a small window, which the paper calls computationally less expensive than the global model.
The problem
A basic encoder-decoder NMT system (Encoder-Decoder Architecture) encodes the source sentence into a representation and then uses only once, to initialise the decoder. Every target word has to be produced from that one summary. Two things go wrong. A long sentence has to fit into a fixed-size state, so information is lost. And proper names and rare words, which a decoder cannot produce from a summary alone, are hard to get right.
Attention had already been used for NMT by Bahdanau, Cho and Bengio (see Neural Machine Translation by Jointly Learning to Align and Translate). That model used a bidirectional encoder, and its decoder state passes through a deep-output and a maxout layer before the prediction. The authors of this paper asked whether a simpler architecture could do as well, and which choices of scoring function matter.
The global model is a weighted sum over all source states for every target word. The local model is cheaper than that, and unlike hard attention it is differentiable almost everywhere, which makes it easier to train.
How it works
Take one target word at decoding step . The decoder has already produced the target words before it, and it has a set of source hidden states from the encoder. Here is the global model, step by step.
- Run the decoder one step. The top layer of the stacked LSTM gives the target hidden state . It summarises the target words so far.
- Score every source state. For each source position , compute a number that says how relevant that source word is to the current step. The paper tries dot, general, concat and location scores (in The math below).
- Turn scores into weights. A softmax over the scores gives weights that are positive and add up to 1. A large score means a large weight.
- Average the source states. The context vector is the weighted average of the source states, using the weights from step 3. It is a single vector that says what the source looks like for this step.
- Combine context and decoder state. Concatenate and , multiply by a matrix , and apply . The result is the attentional hidden state .
- Predict the word. Multiply by and apply a softmax over the vocabulary to get the probability of each possible next word.
A tiny instance with round numbers. Suppose the source has three positions with states , and , and the decoder state is .
- Step 2, dot scores: , , . Positions 1 and 3 score high because their first coordinate matches the decoder state's first coordinate.
- Step 3, softmax: the weights are proportional to . Their sum is 15.778, so the weights are , and . Check: they add to 1.0000.
- Step 4, context: , which rounds to . Check: the first coordinate is .
The local model changes steps 2 to 4. It does not score every position. It first predicts one aligned position in the source sentence, then looks only at the window from to . Inside that window it weights states with the same score as before, multiplied by a Gaussian bump centred on , so that positions near count more. The paper also has a monotonic variant (local-m) that sets , and a predictive variant (local-p) that learns .
A last change, input feeding, alters step 1. The attentional vector from the previous step is concatenated to the input of the decoder at step . The model then knows where it attended before. Standard machine translation keeps a coverage set of which source words are done, and the authors argue that independent alignment decisions can translate a word twice or skip one, so this input lets the model use past alignments.
flowchart LR
A["Source states h-bar 1 to n"] --> S["Score each source state against h_t"]
H["Decoder state h_t"] --> S
S --> W["Softmax gives weights a_t"]
W --> C["Weighted average gives context c_t"]
C --> T["Combine c_t and h_t with tanh gives h-tilde_t"]
H --> T
T --> P["Softmax over vocabulary gives next word"]
The math
The paper models the probability of a whole target sentence given a source sentence as a product of word probabilities. Here means the words before position , and is the parallel training corpus:
Training minimises , the total negative log probability of the reference translations. The source is summarised in , and with attention means the whole set of source states .
Step 1 and 2 in symbols
The decoder hidden state is , where is an LSTM unit. The score function is one of three:
Here is the transpose, so is the dot product. is a learned matrix and a learned vector. The semicolon in means the two vectors are stacked into one longer vector.
The paper also tried a location score, which ignores the source states and sets the weights from the target state alone: . It did not learn good alignments, and the best global result in Table 4 uses the dot score.
Step 3 and 4 in symbols
The weights and the context vector are
The sum over runs over all source positions, so the weights add to 1. The context is a weighted average of the source states. Check with the tiny instance: the weights multiply the three states and sum to , as in the walkthrough.
Step 5 and 6 in symbols
The softmax of a vector with entries gives divided by the sum of over all entries, so the output is a probability for each vocabulary word.
The local model in symbols
The local-p variant predicts an aligned position in , where is the source length:
The weights then get a Gaussian factor centred on with standard deviation , over the window , which holds positions:
Here is the same score-and-softmax as the global model. A position away from keeps a factor of , and a position away keeps .
Worked check with , so , and . The Gaussian factors for positions 2 to 6 are , , , and . Position 4 gets the largest factor, so the context stays close to the state at position 4.
Results
The paper's main numbers, copied from its own tables. All BLEU scores are tokenised BLEU on the stated test set unless marked otherwise.
- English-to-German, WMT'14, newstest2014 (Table 1). The non-attentional dropout model scores 14.0 BLEU. Adding global attention with location scores gives 16.8, a gain of 2.8. Adding input feeding gives 18.1, a gain of 1.3. Switching to local-p attention with general scores gives 19.0, a gain of 0.9. The total gain over the dropout baseline is BLEU, the figure in the abstract. Unknown word replacement adds 1.9 more for the single local-p model (20.9 BLEU). An ensemble of 8 models with unknown word replacement reaches 23.0 BLEU, a gain of 2.1 over the single model, and the paper reports it as a new state of the art: 1.4 BLEU above the previous best end-to-end system (RNNsearch ensemble of 8, 21.6 BLEU).
- English-to-German, WMT'15, newstest2015 (Table 2). The ensemble with unknown word replacement reaches 25.9 BLEU. The previous best entry, an NMT system with a 5-gram reranker, reached 24.9 BLEU. The models were trained on WMT'14 data with slightly less data than the competing system, and tested on newstest2015 to check that they generalise.
- German-to-English, WMT'15 (Table 3). The paper's best system reaches 24.9 BLEU with unknown word replacement. The phrase-based Edinburgh system scores 29.2 BLEU and an NMT system with a 5-gram reranker scores 27.6 BLEU, so this direction did not reach the state of the art. The paper says so directly.
- Scoring functions (Table 4). Global attention with dot scores gets 18.6 BLEU before and 20.5 after unknown word replacement. Local-p with general scores gets 19.0 before and 20.9 after, the best row in that table.
- Alignment quality (Table 6). On 508 hand-aligned English-German Europarl sentences, the local-m (general) model gets alignment error rate 0.34 and local-p (general) gets 0.36. Global (location) gets 0.39 and the Berkeley aligner gets 0.32. The ensemble's alignment error (0.34) is not better than local-m's, and the paper cites the known observation that alignment error and translation score are not well correlated.
Limits. The results cover one language pair in two directions, with BLEU as the only translation metric. Each model was trained with plain SGD for 10 to 12 epochs on 4.5 million sentence pairs with a 50,000-word vocabulary, which took 7 to 10 days on a single Tesla K40 GPU. The authors could not run every combination of architecture and score function "due to limited resources". The location-based score did not learn good alignments (Section 5.3). Unknown words are handled by a replacement trick, not by the attention model itself. Global attention scores every source position, so its cost grows with the input, which is why the local model matters for long inputs.
Reading guide
Read the paper in this order. Each section names the lessons it needs.
- Abstract and Section 1 (Introduction). Needs Attention Mechanism and Encoder-Decoder Architecture. The introduction compares the approach to Bahdanau et al. and to soft and hard attention in image captioning.
- Section 2 (Neural Machine Translation). Needs Encoder-Decoder Architecture, Recurrent Neural Network, Long Short-Term Memory and Language Model. The training objective is a sum of negative log probabilities; see Stochastic Gradient Descent for how such an objective is minimised.
- Section 3 (Attention-based Models). Needs Attention Mechanism, Softmax, Dot Product and Weighted Average. Section 3.1 is the global model, 3.2 the local model (with Gaussian (Normal) Distribution for the window), and 3.3 input feeding. Read 3.1 twice before 3.2.
- Section 4 (Experiments). Needs BLEU Score for the metric and Perplexity for the second number in the tables. Dropout is in Dropout, and gradient norm clipping is in Gradient Norm Clipping.
- Section 5 (Analysis). Uses the Results above. Section 5.4 uses alignment error rate, where lower is better.
- Section 6 and the appendix. The conclusion summarises the results. Appendix A shows alignment images; read them alongside Section 3.2 to see how sharp the local weights are.
Check yourself
QuestionIn one sentence, what is the difference between global and local attention in this paper?Show answer
Global attention scores every source state and averages all of them with softmax weights. Local attention predicts one aligned position, then averages only the source states in a window of positions around it, weighted by the score and a Gaussian centred on that position.
QuestionTwo source states are and , and the decoder state is . Using dot scores and softmax weights, what is the context vector ? Give both coordinates to three decimals.Show answer
Dot scores: and . Exponentials: and , sum . Weights: and . Context: .
QuestionWhy does the local model multiply the scores by a Gaussian centred on instead of just cutting the window to a hard edge?Show answer
The Gaussian makes the weights smooth and differentiable everywhere, so gradients can flow to the parameters and that predict . A hard cut on the window would make the choice of non-differentiable. The Gaussian also favours positions close to , which matches the idea that the aligned word is most likely near the predicted point.
QuestionA translation system must handle a 2000-word document. Which attention variant from this paper suits it better, and why?Show answer
The local variant. Global attention scores all 2000 source states for every target word, which is expensive and grows with the input length. The local variant scores only the positions in its window, which is fixed in size.
Sources
- Luong, M.-T., Pham, H., and Manning, C. D. (2015). Effective Approaches to Attention-based Neural Machine Translation. EMNLP 2015. arXiv: 1508.04025. DOI: 10.18653/v1/D15-1166.