Search

Search papers and lessons

Neural Machine Translation by Jointly Learning to Align and Translate

Dzmitry Bahdanau, Kyunghyun Cho, Yoshua Bengio · 2014

Bahdanau, Cho and Bengio (2014) let a translation decoder look back at every source word with learned weights, which removed the fixed-length bottleneck and made long sentences translate well.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Architectures
  • Neural network basics
  • Probability
  • Discrete math
  • Sign in to see your progress

TL;DR

Before this paper, neural machine translation squeezed a whole source sentence into one fixed-length vector and decoded the translation from it. That vector became a bottleneck, and quality fell sharply on long sentences. Bahdanau, Cho and Bengio let the decoder compute a fresh weighted mix of all the encoder's per-word states for every output word, with the weights learned from data. The model matched a strong phrase-based system on English-to-French translation for sentences made of known words, and it degraded much less on long inputs.

The problem: a sentence of 50 words had to fit into the same number of numbers as a sentence of 5. The fix: keep every word's representation and learn where to look.

The problem

By 2014 the simplest neural translation model was an encoder-decoder. A recurrent network read the source sentence and produced one vector cc. A second RNN, the decoder, generated the target words from cc alone, one at a time. This worked, and it was trained end to end on parallel text. It also had a hard limit: all the information about the source had to pass through cc, a vector of fixed size.

The authors measured what that limit cost. On English-to-French test sentences, the translation quality of the basic model collapsed as sentence length grew. Their conjecture was that a fixed-size vector cannot hold the meaning of a long sentence in a form the decoder can use.

Phrase-based statistical translation, the dominant method, did not have this problem, because each phrase was translated locally from a table. The gap to close was therefore between the easy-to-train neural model and the local, length-robust statistical one.

Key idea

Let the translator choose what to read at each step. The encoder keeps one representation per source word, each summarising the words around it. When the decoder is about to produce a target word, it scores every source position against its current state, turns the scores into weights that sum to one, and takes the weighted average of the source representations as its context. The weights are soft: the model never picks one word. It blends many.

flowchart LR
  S[Source words x1 to xT] --> E[Bidirectional RNN encoder]
  E --> H[Annotations h1 to hT]
  D[Decoder state s i minus 1] --> A[Alignment scores e i j]
  H --> A
  A --> W[Softmax weights alpha i j]
  H --> C[Context c i = weighted sum]
  W --> C
  C --> Y[Next target word y i]
  D --> Y

The decoder therefore has a different context vector for every output word. The information about the source is no longer squeezed into one place. It stays spread across the annotations, and the decoder retrieves what it needs when it needs it.

The method

Encoder: a bidirectional RNN

Read the source sentence x=(x1,…,xTx)\mathbf{x} = (x_1, \dots, x_{T_x}) in both directions. A forward RNN produces h→j\overrightarrow{h}_j from left to right. A backward RNN produces h←j\overleftarrow{h}_j from right to left. The annotation of word jj is their concatenation,

hj=[h→j⊤; h←j⊤]⊤.h_j = \big[\overrightarrow{h}_j^{\top};\, \overleftarrow{h}_j^{\top}\big]^{\top}.

Each annotation therefore summarises the words before and after position jj, with a bias toward the nearby ones because RNNs remember recent inputs best. This is the bidirectional RNN construction. The paper's models use 1000 hidden units per direction.

Decoder: a state and a context per output word

The decoder is an RNN with state sis_i. Its update and its output distribution are

si=f(si−1,yi−1,ci),p(yi∣y1,…,yi−1,x)=g(yi−1,si,ci).s_i = f(s_{i-1}, y_{i-1}, c_i), \qquad p(y_i \mid y_1, \dots, y_{i-1}, \mathbf{x}) = g(y_{i-1}, s_i, c_i).

The key change from the basic model is the subscript on cic_i. The context is recomputed for every output position ii rather than fixed once for the whole sentence.

Attention weights

The context is a weighted sum of annotations,

ci=∑j=1Txαij hj,αij=exp⁡(eij)∑k=1Txexp⁡(eik).c_i = \sum_{j=1}^{T_x} \alpha_{ij}\, h_j, \qquad \alpha_{ij} = \frac{\exp(e_{ij})}{\sum_{k=1}^{T_x} \exp(e_{ik})}.

The weights αij\alpha_{ij} form a probability distribution over source positions (the expected annotation view in the paper): they are non-negative and sum to one. The score eij=a(si−1,hj)e_{ij} = a(s_{i-1}, h_j) measures how well source position jj matches the decoder's state just before emitting yiy_i. The paper parameterises the alignment model as a one-hidden-layer network,

a(si−1,hj)=va⊤tanh⁡ ⁣(Wasi−1+Uahj),a(s_{i-1}, h_j) = v_a^{\top} \tanh\!\big(W_a s_{i-1} + U_a h_j\big),

with trainable WaW_a, UaU_a and vav_a. It is trained jointly with the rest of the system. Since UahjU_a h_j does not depend on ii, it can be computed once per sentence.

The paper calls the weights αij\alpha_{ij} soft alignments. They are not a latent variable sampled from a discrete set. Because they are differentiable, the translation loss flows back through the weights into the alignment model, and the model learns where to look from the translation objective alone.

Training and decoding

Training maximises the log-probability of the reference translations over a parallel corpus, using minibatch stochastic gradient descent with a batch of 80 sentences and the Adadelta adaptive step rule. Decoding is not greedy. The paper uses beam search to find a high-probability translation.

Results

The evaluation uses English-to-French on WMT 2014, with 348 million words of selected parallel data and the news-test-2014 set of 3003 sentences. Quality is measured by BLEU.

ModelBLEU, all sentencesBLEU, no unknown words
RNNencdec-3013.9324.19
RNNsearch-3021.5031.44
RNNencdec-5017.8226.71
RNNsearch-5026.7534.16
RNNsearch-50 (trained longer)28.4536.15
Moses (phrase-based)33.3035.63

Three findings matter. First, the attention model beats the basic encoder-decoder at both training lengths. Second, the gain grows with sentence length. The basic model's score falls steeply as sentences get longer, while RNNsearch-50 shows no deterioration even on sentences of 50 words or more. Third, RNNsearch-30, trained only on sentences up to 30 words, still scores above RNNencdec-50 overall. On sentences without unknown words, the longer-trained RNNsearch-50 scores 36.15 against Moses' 35.63. On all sentences, though, Moses is still well ahead (33.30 against 28.45), and the authors note that Moses also used an extra 418 million words of monolingual data that the neural models did not.

The authors also inspected the weights. For a typical sentence, the matrix of αij\alpha_{ij} is close to diagonal, because English and French are largely word-ordered alike. Where word order differs, as in the adjective-noun phrase "European Economic Area" translated to "zone économique européen", the weights jump over the intervening words. The paper also shows that soft weights handle "the man" to "l'homme": the decoder looks at both source words before choosing an article.

Why it mattered

Attention became the standard mechanism for sequence-to-sequence models within a few years. Luong, Pham and Manning (2015) simplified the scoring function. Later systems used attention inside recurrent networks for speech and captioning. The Transformer (Vaswani et al., 2017) removed the recurrence altogether and built the whole model out of attention, and nearly all current large language models are descendants of that design.

The paper also set a template that persists: a differentiable, learned alignment trained jointly with the translation objective, rather than a separate alignment pipeline. The bottleneck argument, that a fixed-size summary limits what a decoder can recover, is the reason the mechanism exists.

The paper is also clear about what it did not solve. The authors name rare and unknown words as the main open problem: the 30,000-word shortlist maps everything else to an unknown token, and the scores on the full test set are lower than on the subset without unknown words for that reason. The model also still runs a recurrent decoder one word at a time, which limits how much of the computation can run in parallel. Both limits shaped the work that followed.

Reading guide

Read the paper in this order, using these prerequisite lessons:

  1. Section 1, Introduction. Needs encoder-decoder architecture and the idea of a fixed-length bottleneck.
  2. Section 2, Background. Needs Conditional Probability to read p(y∣x)p(\mathbf{y} \mid \mathbf{x}) and the factorisation into ordered conditionals. Needs RNN for ht=f(xt,ht−1)h_t = f(x_t, h_{t-1}).
  3. Section 2.1, RNN Encoder-Decoder. Needs the encoder-decoder and RNN ideas above. The gated hidden units the paper uses are defined later, in Appendix A.1.1, so read the gated recurrent unit lesson before that appendix.
  4. Section 3.1, Decoder. Needs attention for the context vector and the softmax weights. Read the paragraph on expected annotations slowly: it is the central idea.
  5. Section 3.2, Encoder. Needs bidirectional RNN.
  6. Section 4.2, Models. Needs feedforward networks for the maxout output layer and SGD for training.
  7. Sections 4.2 (decoding) and 5, Results. Needs beam search and enough expectation to read BLEU averages.
  8. Appendix A.1.2, Alignment model. Needs the tanh-network form of the score in the method section above.

For background on the surrounding family, the sequence-to-sequence paper is the predecessor to read first.

Check yourself

QuestionWhy does the basic encoder-decoder degrade on long sentences, according to the paper?Show answer

All information about the source passes through one fixed-length vector cc. As a sentence grows, that vector has to carry more content in the same number of dimensions, and the decoder cannot recover the details it needs. The attention model avoids this because each output word gets its own context, a weighted mix of per-word annotations, so it does not depend on one summary of the whole sentence.

QuestionThree source annotations get scores e=(1.0,2.0,0.5)e = (1.0, 2.0, 0.5). What are the attention weights, and what is the context for scalar annotations h=(1,3,5)h = (1, 3, 5)?Show answer

Exponentiate the scores: e1.0=2.718e^{1.0} = 2.718, e2.0=7.389e^{2.0} = 7.389, e0.5=1.649e^{0.5} = 1.649. Their sum is 11.75611.756. The weights are α=(0.231,0.629,0.140)\alpha = (0.231, 0.629, 0.140), which sum to about 1. The context is c=0.231×1+0.629×3+0.140×5=0.231+1.887+0.700=2.818c = 0.231 \times 1 + 0.629 \times 3 + 0.140 \times 5 = 0.231 + 1.887 + 0.700 = 2.818, close to the middle annotation because it has the highest score.

QuestionWhy does the paper use soft weights instead of choosing one source word per output word?Show answer

A hard choice is not differentiable, so the loss cannot train the alignment model through it. Soft weights are a probability distribution over positions, so the gradient flows back into the scoring network and the whole system is trained together. Soft weights also handle cases like "the man" to "l'homme", where the right article depends on the word after it.

QuestionRNNsearch-50 scores 26.75 BLEU and RNNencdec-50 scores 17.82 on all sentences. What is the relative improvement?Show answer

The absolute gain is 26.75−17.82=8.9326.75 - 17.82 = 8.93 BLEU. The relative gain is 8.93/17.82≈0.5018.93 / 17.82 \approx 0.501, so about 50 percent more. Relative gains are the more useful summary here because the basic model is so much weaker on long sentences that the absolute number understates how much the architecture changes.

Sources

Read the full paper

Dzmitry Bahdanau, Kyunghyun Cho, Yoshua Bengio, 2014. arXiv:1409.0473