Search

Search papers and lessons

Learning Phrase Representations using RNN Encoder-Decoder for SMT

Kyunghyun Cho, Bart van Merrienboer, Caglar Gulcehre et al. · 2014

Cho and colleagues (2014) train two recurrent networks together, one reads a phrase into a fixed-length vector and the other writes a phrase out, and use its scores to improve a machine translation system.

On this page

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

TL;DR

This paper introduces the RNN Encoder-Decoder: two recurrent neural networks trained together. The first, the encoder, reads a sequence of words and squeezes it into one fixed-length vector. The second, the decoder, unfolds that vector back into a new sequence. The authors also propose a simpler gated hidden unit, which later became known as the GRU. They use the model to score phrase pairs inside an English-to-French statistical translation system, and the scores raise the translation quality measured by BLEU.

Builds on: Encoder-Decoder Architecture, Word Embedding, Stochastic Gradient Descent · Leads to: Sequence to Sequence Learning with Neural Networks, Neural Machine Translation by Jointly Learning to Align and Translate

Why it mattered

The gated unit from this paper, the Gated Recurrent Unit, is still a standard building block for sequence models. The idea of encoding a whole input into one vector and then decoding an output from it became the template for neural machine translation. The 2014 system was not yet a full neural translator: the network only rescored phrases that an older translation system had already proposed. The next papers, Sequence to Sequence Learning with Neural Networks and Neural Machine Translation by Jointly Learning to Align and Translate, removed that dependence and let the network generate whole translations.

The paper also showed that a network trained on phrase pairs learns a space in which phrases with similar meaning sit close together. That result made the vector representation of a phrase something other people wanted to reuse.

The problem

In 2014 a common way to build a translator was phrase-based statistical machine translation. The system splits a sentence into phrases, looks up each possible source phrase and its target phrases in a phrase table, and combines scores for every candidate translation. Each entry in the table carries a probability estimated by counting how often the two phrases appeared together in a parallel corpus. Counting works well for frequent phrases and badly for rare ones.

Neural networks had already been used in this pipeline, but usually as small feedforward models with a fixed input size. A feedforward model that takes at most seven words cannot handle a phrase of any length. The authors wanted a model that accepts a sequence of any length and returns a score or a new sequence, so that long phrases are handled too.

A recurrent neural network (RNN) can do that. It reads one symbol at a time and keeps a hidden state. The difficulty is that a plain RNN with a simple Activation Function learns poorly over long gaps. The paper's hidden unit is an answer to that problem.

How it works

Here is the whole model traced on one tiny instance. Suppose the source phrase is the two words "thank you" and the target phrase is the two words "merci beaucoup". The model is trained so that the probability of the target given the source is high.

  1. Encode the input, one word at a time. The encoder starts with a hidden state of zeros. It reads "thank", computes a new hidden state from the old one and the word, then reads "you" and updates again. Check: after two updates the state depends on both words, and the order of the words matters.
  2. Take the final state as the summary. After the end-of-sequence marker, the last hidden state is a vector c. In the paper's model c has 1000 numbers. Check: the same vector length is produced for a two-word phrase and for a twenty-word phrase.
  3. Start the decoder from the summary. The decoder is a second RNN. Its first hidden state is computed from c, so the decoder knows the meaning of the source before it writes anything.
  4. Predict the first target word. At each step the decoder turns its hidden state into a probability for every word in its vocabulary, using a Softmax. It assigns high probability to "merci". Check: the probabilities over the vocabulary add up to 1.
  5. Feed the chosen word back in. The decoder takes the word it just produced as its input for the next step, along with its hidden state and the summary c. It then predicts "beaucoup". Check: the second prediction sees both c and "merci", so it can be conditioned on what was already said.
  6. Stop at the end marker. The decoder keeps going until it produces the end-of-sequence symbol. The score of the pair is the product of the probabilities of the target words, which is the conditional probability the model was trained to maximise.

The phrase-table use is the same machinery with the roles fixed. The authors train the model on phrase pairs taken from the phrase table, and then add its score for each pair as one more feature in the existing translation system.

flowchart LR
  A["source words: thank, you"] --> B["encoder RNN"]
  B --> C["summary vector c"]
  C --> D["decoder RNN"]
  D --> E["merci, then beaucoup"]
  E -. "previous word fed back" .-> D

The hidden unit

The paper's own hidden unit decides at each step how much of the old state to keep. Its response is the logistic Sigmoid Function, which outputs a number between 0 and 1. Here is the picture for a single unit. A gate near 0 closes, and a gate near 1 opens.

−6−4−2024600.20.40.60.81gate inputgate value
  • sigmoid, the gate squashing function
The gates are sigmoid outputs. Near 0 they close and near 1 they open. Each gate value is a number between 0 and 1.

The math

We write hth_t for the hidden state at step tt, and hj(t)h_j^{(t)} for element jj of that state.

Definition: RNN update

Given a hidden state ht−1h_{t-1} and an input xtx_t, a recurrent network computes ht=f(ht−1,xt)h_t = f(h_{t-1}, x_t), where ff is a non-linear function. The paper allows ff to be a sigmoid, an LSTM unit, or the gated unit it proposes.

Step 1 in symbols: the encoder reads the source

ht=f(ht−1,xt),t=1,…,Th_t = f(h_{t-1}, x_t), \qquad t = 1, \dots, T

After the last source symbol the summary is c=hTc = h_T. Appendix A adds a tanh of a linear map of this state, which does not change the idea.

Step 2 in symbols: the decoder starts from the summary

The decoder hidden state also depends on the previous target word yt−1y_{t-1} and on cc:

ht=f(ht−1,yt−1,c)h_t = f(h_{t-1}, y_{t-1}, c)

and it outputs a distribution over the next word,

P(yt∣yt−1,…,y1,c)=g(ht,yt−1,c)P(y_t \mid y_{t-1}, \dots, y_1, c) = g(h_t, y_{t-1}, c)

where gg must return probabilities, normally with a softmax. The probability of a whole target sequence yy given a source xx is the product of these next-word probabilities.

Step 3 in symbols: the gated hidden unit

For unit jj the paper computes a reset gate rjr_j and an update gate zjz_j. Each is a sigmoid over the current input and the old state:

rj=σ([Wrx]j+[Urht−1]j),zj=σ([Wzx]j+[Uzht−1]j)r_j = \sigma\left([\mathbf{W}_r x]_j + [\mathbf{U}_r h_{t-1}]_j\right), \qquad z_j = \sigma\left([\mathbf{W}_z x]_j + [\mathbf{U}_z h_{t-1}]_j\right)

Here σ\sigma is the logistic sigmoid, W\mathbf{W} and U\mathbf{U} are learned weight matrices, and [⋅]j[\cdot]_j picks element jj of a vector. A candidate state uses the reset gate to decide how much of the old state to read:

h~j=tanh⁡([Wx]j+[U(r⊙ht−1)]j)\tilde{h}_j = \tanh\left([\mathbf{W} x]_j + [\mathbf{U}(r \odot h_{t-1})]_j\right)

where ⊙\odot is element-wise multiplication. The new state mixes the old state and the candidate:

hj(t)=zj hj(t−1)+(1−zj) h~jh_j^{(t)} = z_j\, h_j^{(t-1)} + (1 - z_j)\, \tilde{h}_j

When zjz_j is near 1 the unit copies its old value, so information survives many steps. When rjr_j is near 0 the candidate ignores the past.

Step 4 in symbols: the training objective

Training maximises the average conditional log-likelihood over NN phrase pairs (xn,yn)(x_n, y_n):

max⁡θ1N∑n=1Nlog⁡pθ(yn∣xn)\max_{\theta} \frac{1}{N} \sum_{n=1}^{N} \log p_\theta(y_n \mid x_n)

Here θ\theta is the full set of weights. The log turns the product over words into a sum, which is easier to optimise with gradient methods.

Example
Tiny: one gated update by hand

Take a single hidden unit with z=0.5z = 0.5, previous state ht−1=1h_{t-1} = 1, and candidate h~=0\tilde{h} = 0.

  1. Write the mixing rule: ht=z ht−1+(1−z) h~h_t = z\, h_{t-1} + (1 - z)\, \tilde{h}.
  2. Put in the numbers for the old-state part: 0.5×1=0.50.5 \times 1 = 0.5.
  3. Put in the numbers for the candidate part: (1−0.5)×0=0(1 - 0.5) \times 0 = 0.
  4. Add: ht=0.5+0=0.5h_t = 0.5 + 0 = 0.5.

Interpretation: the gate kept half of the old value and dropped the candidate entirely. If zz had been 1, the output would have been exactly 1.

Example
Standard: a three-step encoder

Let the source be the three symbols x1,x2,x3x_1, x_2, x_3 and suppose the summary after the last step is c=h3c = h_3. The encoder applies the update three times, first from h0=0h_0 = 0.

  1. Step 1 reads x1x_1 and produces h1h_1. Take h1=0.2h_1 = 0.2 for this illustration.
  2. Step 2 reads x2x_2 and produces h2=0.5×0.2+0.5×0.6=0.1+0.3=0.4h_2 = 0.5 \times 0.2 + 0.5 \times 0.6 = 0.1 + 0.3 = 0.4, where 0.60.6 is the candidate.
  3. Step 3 reads x3x_3 and produces h3=0.5×0.4+0.5×0.8=0.2+0.4=0.6h_3 = 0.5 \times 0.4 + 0.5 \times 0.8 = 0.2 + 0.4 = 0.6, where 0.80.8 is the candidate.
  4. The summary is c=0.6c = 0.6.

Interpretation: each step keeps half of the running summary and mixes in half of the new candidate, so the final value reflects all three symbols. Reversing the order would change the candidates and so change cc.

Example
Realistic: the phrase score used in translation

The translation system scores a phrase pair (x,y)(x, y) with log⁡pθ(y∣x)\log p_\theta(y \mid x). Suppose a phrase pair has two target words with conditional probabilities p(merci∣x)=0.5p(\text{merci} \mid x) = 0.5 and p(beaucoup∣x,merci)=0.4p(\text{beaucoup} \mid x, \text{merci}) = 0.4.

  1. The model's score for the pair is the product 0.5×0.4=0.20.5 \times 0.4 = 0.2.
  2. Take the logarithm: log⁡0.2≈−1.609\log 0.2 \approx -1.609 (natural log).
  3. This log-probability enters the log-linear translation model as one feature among several, weighted by a number the system tunes.

Interpretation: a higher log-probability makes the pair more likely to be chosen. The decoder never sees the phrase table counts; it only sees the phrase pair.

Try it: the decoder gives the first word probability 0.25 and the second word probability 0.8. What is the pair's probability? Answer: 0.25×0.8=0.20.25 \times 0.8 = 0.2.

Results

The paper evaluates on English-to-French translation from the WMT'14 shared task, using newstest2014 as the test set. The baseline is a phrase-based system built with Moses at default settings. BLEU scores from Table 1, written dev / test:

Systemdev BLEUtest BLEU
Baseline30.6433.30
Baseline + RNN31.2033.87
Baseline + CSLM + RNN31.4834.64
Baseline + CSLM + RNN + word penalty31.5034.54

The RNN feature alone adds 0.57 BLEU points on the test set (33.87 against 33.30). Adding a neural target language model (CSLM) as well gives the best test score, 34.64. The word-penalty variant was better on the development set only, and the authors report that it did not improve the test set.

What the numbers mean: the gain is real but modest, and it comes from rescoring phrases the old system already proposed. The model is not a full translator in this paper. The authors also say the two neural components are not too correlated, so improving each one separately should help the combined system.

Limits: one language pair, one baseline system, test sets of more than 70 thousand words each with a single reference translation. The model used a vocabulary of the most frequent 15,000 words per language, and everything else became an unknown token. Training took about three days.

The qualitative part is in Tables 2 and 3. The model's top phrase choices are often closer to literal translations than the phrase table's choices, and it prefers shorter phrases. Samples drawn from the model are well-formed French phrases, though they do not all match the phrase table.

Reading guide

Read the paper in this order, with the lessons that prepare each part.

  1. Abstract and Section 1, Introduction. Needs Word Embedding and the idea of a neural language model.
  2. Section 2.1, Recurrent Neural Networks. Needs Recurrent Neural Network and Softmax. The equations in this section are the basis of everything after.
  3. Section 2.2, RNN Encoder-Decoder. Needs Encoder-Decoder Architecture and Stochastic Gradient Descent for the training objective at the end of the section.
  4. Section 2.3, Hidden unit. Needs Sigmoid Function, Long Short-Term Memory (for the comparison) and Vanishing Gradient Problem (the reason for the gates). The new unit is what Gated Recurrent Unit refers to today.
  5. Section 3, Statistical machine translation. Needs Language Model and BLEU Score. The log-linear model in Eq. (9) is the baseline the new scores join.
  6. Section 4, Experiments. Needs BLEU Score for reading Table 1. Appendix A gives the full architecture; read it after the main text.
  7. Section 5, Conclusion. Short. It names the next step, which Sequence to Sequence Learning with Neural Networks took.

Check yourself

QuestionIn one sentence, what does the encoder produce, and what does the decoder do with it?Show answer

The encoder reads the whole source sequence and produces one fixed-length vector c that summarises it. The decoder starts from c and generates the target sequence one word at a time, conditioning each word on c and on the words it has already produced.

QuestionA gated unit has z = 0.25, previous state h_{t-1} = 2 and candidate h~ = 4. What is the new state h_t?Show answer

Use ht=z ht−1+(1−z) h~h_t = z\,h_{t-1} + (1 - z)\,\tilde{h}. First term: 0.25×2=0.50.25 \times 2 = 0.5. Second term: 0.75×4=30.75 \times 4 = 3. Sum: ht=3.5h_t = 3.5.

QuestionWhy does the paper train on phrase pairs without their corpus frequencies?Show answer

The phrase table already stores translation probabilities that come from counting, so frequencies would add little. Leaving them out also avoids costly sampling by frequency and stops the model from simply learning how often each pair occurred. The authors want its capacity spent on linguistic regularities, so that its scores add information the table lacks.

QuestionThe baseline test BLEU is 33.30 and the RNN feature alone gives 33.87. What is the gain, and what does it tell you about the RNN feature?Show answer

The gain is 33.87−33.30=0.5733.87 - 33.30 = 0.57 BLEU points. It is a real but small improvement from one added feature, so the RNN is a useful addition rather than a replacement for the existing system.

QuestionWhy can this model score phrases of any length, while the feedforward model of Schwenk (2012) cannot?Show answer

The model reads its input one symbol at a time and keeps a hidden state of fixed size, so it accepts sequences of any length. The feedforward model takes a fixed number of words (7 in Schwenk's case), padding shorter phrases with zeros, so it cannot take longer ones.

Sources

Read the full paper

Kyunghyun Cho, Bart van Merrienboer, Caglar Gulcehre et al., 2014. arXiv:1406.1078