Search

Search papers and lessons

Effective Approaches to Attention-based Neural Machine Translation

Minh-Thang Luong, Hieu Pham, Christopher D. Manning · 2015

Luong, Pham and Manning compare a global and a local attention for neural translation; their ensemble reached 25.9 BLEU on WMT'15 English-to-German.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Architectures
  • Probability
  • ReadReady to readNot yet

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 xx into a representation ss and then uses ss 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 tt. The decoder has already produced the target words before it, and it has a set of source hidden states hˉ1,…,hˉn\bar{h}_1, \ldots, \bar{h}_n from the encoder. Here is the global model, step by step.

  1. Run the decoder one step. The top layer of the stacked LSTM gives the target hidden state hth_t. It summarises the target words so far.
  2. Score every source state. For each source position ss, compute a number score(ht,hˉs)\text{score}(h_t, \bar{h}_s) 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).
  3. Turn scores into weights. A softmax over the scores gives weights at(s)a_t(s) that are positive and add up to 1. A large score means a large weight.
  4. Average the source states. The context vector ctc_t 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.
  5. Combine context and decoder state. Concatenate ctc_t and hth_t, multiply by a matrix WcW_c, and apply tanh⁡\tanh. The result is the attentional hidden state h~t\tilde{h}_t.
  6. Predict the word. Multiply h~t\tilde{h}_t by WsW_s 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 hˉ1=(1,0)\bar{h}_1 = (1, 0), hˉ2=(0,1)\bar{h}_2 = (0, 1) and hˉ3=(1,1)\bar{h}_3 = (1, 1), and the decoder state is ht=(2,0)h_t = (2, 0).

  • Step 2, dot scores: ht⋅hˉ1=2h_t \cdot \bar{h}_1 = 2, ht⋅hˉ2=0h_t \cdot \bar{h}_2 = 0, ht⋅hˉ3=2h_t \cdot \bar{h}_3 = 2. 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 e2,e0,e2=7.389,1,7.389e^2, e^0, e^2 = 7.389, 1, 7.389. Their sum is 15.778, so the weights are 0.46830.4683, 0.06340.0634 and 0.46830.4683. Check: they add to 1.0000.
  • Step 4, context: ct=0.4683⋅(1,0)+0.0634⋅(0,1)+0.4683⋅(1,1)=(0.9366,0.5317)c_t = 0.4683 \cdot (1, 0) + 0.0634 \cdot (0, 1) + 0.4683 \cdot (1, 1) = (0.9366, 0.5317), which rounds to (0.937,0.532)(0.937, 0.532). Check: the first coordinate is 0.4683+0.4683=0.93660.4683 + 0.4683 = 0.9366.

The local model changes steps 2 to 4. It does not score every position. It first predicts one aligned position ptp_t in the source sentence, then looks only at the window from pt−Dp_t - D to pt+Dp_t + D. Inside that window it weights states with the same score as before, multiplied by a Gaussian bump centred on ptp_t, so that positions near ptp_t count more. The paper also has a monotonic variant (local-m) that sets pt=tp_t = t, and a predictive variant (local-p) that learns ptp_t.

A last change, input feeding, alters step 1. The attentional vector h~t−1\tilde{h}_{t-1} from the previous step is concatenated to the input of the decoder at step tt. 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 yy given a source sentence xx as a product of word probabilities. Here y<jy_{<j} means the words before position jj, and D\mathbb{D} is the parallel training corpus:

log⁡p(y∣x)=∑j=1mlog⁡p(yj∣y<j,s),J=∑(x,y)∈D−log⁡p(y∣x)\log p(y|x)=\sum_{j=1}^{m}\log p\left(y_{j}|y_{<j},s\right), \qquad J=\sum_{(x,y)\in\mathbb{D}}-\log p(y|x)

Training minimises JJ, the total negative log probability of the reference translations. The source is summarised in ss, and with attention ss means the whole set of source states hˉs\bar{h}_s.

Step 1 and 2 in symbols

The decoder hidden state is ht=f(ht−1,⋅)h_t = f(h_{t-1}, \cdot), where ff is an LSTM unit. The score function is one of three:

score(ht,hˉs)={ht⊤hˉsdotht⊤Wahˉsgeneralva⊤tanh⁡(Wa[ht;hˉs])concat\text{score}(h_t, \bar{h}_s) = \begin{cases} h_t^{\top} \bar{h}_s & \text{dot} \\ h_t^{\top} W_a \bar{h}_s & \text{general} \\ v_a^{\top} \tanh\left(W_a [h_t; \bar{h}_s]\right) & \text{concat} \end{cases}

Here ht⊤h_t^{\top} is the transpose, so ht⊤hˉsh_t^{\top} \bar{h}_s is the dot product. WaW_a is a learned matrix and vav_a a learned vector. The semicolon in [ht;hˉs][h_t; \bar{h}_s] 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: at=softmax⁡(Waht)a_t = \operatorname{softmax}(W_a h_t). 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

at(s)=exp⁡(score(ht,hˉs))∑s′exp⁡(score(ht,hˉs′)),ct=∑sat(s) hˉsa_t(s) = \frac{\exp\left(\text{score}(h_t, \bar{h}_s)\right)}{\sum_{s'} \exp\left(\text{score}(h_t, \bar{h}_{s'})\right)}, \qquad c_t = \sum_{s} a_t(s)\, \bar{h}_s

The sum over s′s' runs over all source positions, so the weights add to 1. The context ctc_t is a weighted average of the source states. Check with the tiny instance: the weights (0.468,0.063,0.468)(0.468, 0.063, 0.468) multiply the three states and sum to (0.937,0.532)(0.937, 0.532), as in the walkthrough.

Step 5 and 6 in symbols

h~t=tanh⁡(Wc[ct;ht]),p(yt∣y<t,x)=softmax⁡(Wsh~t)\tilde{h}_t = \tanh\left(W_c [c_t; h_t]\right), \qquad p(y_t|y_{<t},x) = \operatorname{softmax}\left(W_s \tilde{h}_t\right)

The softmax of a vector zz with entries zkz_k gives ezke^{z_k} divided by the sum of ezk′e^{z_{k'}} 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 [0,S][0, S], where SS is the source length:

pt=S⋅sigmoid⁡(vp⊤tanh⁡(Wpht))p_t = S \cdot \operatorname{sigmoid}\left(v_p^{\top} \tanh\left(W_p h_t\right)\right)

The weights then get a Gaussian factor centred on ptp_t with standard deviation σ=D/2\sigma = D/2, over the window [pt−D,pt+D][p_t - D, p_t + D], which holds 2D+12D + 1 positions:

at(s)=align⁡(ht,hˉs)exp⁡(−(s−pt)22σ2)a_t(s) = \operatorname{align}(h_t, \bar{h}_s) \exp\left(-\frac{(s - p_t)^2}{2\sigma^2}\right)

Here align⁡\operatorname{align} is the same score-and-softmax as the global model. A position σ\sigma away from ptp_t keeps a factor of e−1/2≈0.61e^{-1/2} \approx 0.61, and a position 2σ2\sigma away keeps e−2≈0.14e^{-2} \approx 0.14.

Worked check with D=2D = 2, so σ=1\sigma = 1, and pt=4p_t = 4. The Gaussian factors for positions 2 to 6 are e−2=0.135e^{-2} = 0.135, e−0.5=0.607e^{-0.5} = 0.607, e0=1e^{0} = 1, e−0.5=0.607e^{-0.5} = 0.607 and e−2=0.135e^{-2} = 0.135. 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 19.0−14.0=5.019.0 - 14.0 = 5.0 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.

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 2D+12D + 1 positions around it, weighted by the score and a Gaussian centred on that position.

QuestionTwo source states are hˉ1=(1,0)\bar{h}_1 = (1, 0) and hˉ2=(0,1)\bar{h}_2 = (0, 1), and the decoder state is ht=(0,2)h_t = (0, 2). Using dot scores and softmax weights, what is the context vector ctc_t? Give both coordinates to three decimals.Show answer

Dot scores: ht⋅hˉ1=0h_t \cdot \bar{h}_1 = 0 and ht⋅hˉ2=2h_t \cdot \bar{h}_2 = 2. Exponentials: e0=1e^0 = 1 and e2=7.389e^2 = 7.389, sum 8.3898.389. Weights: 1/8.389=0.1191 / 8.389 = 0.119 and 7.389/8.389=0.8817.389 / 8.389 = 0.881. Context: ct=0.119⋅(1,0)+0.881⋅(0,1)=(0.119,0.881)c_t = 0.119 \cdot (1, 0) + 0.881 \cdot (0, 1) = (0.119, 0.881).

QuestionWhy does the local model multiply the scores by a Gaussian centred on ptp_t 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 WpW_p and vpv_p that predict ptp_t. A hard cut on the window would make the choice of ptp_t non-differentiable. The Gaussian also favours positions close to ptp_t, 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 2D+12D + 1 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.

Read the full paper

Minh-Thang Luong, Hieu Pham, Christopher D. Manning, 2015. arXiv:1508.04025