Search

Search papers and lessons

Sequence to Sequence Learning with Neural Networks

Ilya Sutskever, Oriol Vinyals, Quoc V. Le · 2014

Sutskever, Vinyals and Le (2014) translated sentences with two LSTMs, one that compresses the input into a vector and one that writes the output from it, and reversing the source sentence markedly improved it.

On this page

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

TL;DR

Sutskever, Vinyals and Le (2014) translated English sentences into French with two LSTMs. The first reads the whole input and compresses it into one fixed-length vector. The second starts from that vector and writes the output one word at a time. No word alignment or phrase table is built by hand.

The trick that helped most was reversing the order of the source sentence before training. An ensemble of five such models reached a BLEU score of 34.81 on WMT'14 English to French, which beat a strong phrase-based statistical baseline (33.30) but not the best published system (37.0).

Where you'll see it: machine translation, summarization and any system that maps one variable-length sequence to another, such as speech recognition output or chat replies.

The problem

A neural network needs its inputs and outputs to have a fixed size. An image has a fixed number of pixels, but a sentence does not have a fixed number of words, and neither does its translation. Translation, speech recognition and question answering all map a sequence of unknown length to another sequence of unknown length, and the two lengths need not match.

Before this paper, the strong translation systems were statistical. They split the sentence into phrases, translated each phrase with a table, and reordered the results with separate models. Neural networks had been used only to rescore lists of candidates or to add features to that pipeline. The question was whether one neural network could do the whole job end to end, and whether a recurrent network could keep track of meaning across a long sentence.

Key idea

Use one LSTM as an encoder and a second LSTM as a decoder. The encoder reads the source words in order and its final hidden state is a summary of the sentence. The decoder is a language model that starts from that summary and predicts the target words, so each output word is conditioned on the input.

flowchart LR
  A[A] --> B[B] --> C[C] --> E[EOS]
  E --> V((vector v))
  V --> W[W] --> X[X] --> Y[Y] --> Z[Z] --> F[EOS]

The second idea is smaller but decisive. The source sentence is fed in reversed. If the input is "a b c" and the target is "alpha beta gamma", the encoder reads "c b a". The first source word is then next to the first target word in the sequence the LSTM sees, which shortens the distance backpropagation has to cover.

The method

The model

A recurrent network computes a hidden state hth_t from the current input and the previous state, and produces outputs from it. The paper's goal is a conditional probability of the whole output sequence given the whole input sequence. Let x1,…,xTx_1, \dots, x_T be the input tokens and y1,…,yT′y_1, \dots, y_{T'} the output tokens. The encoder LSTM's last hidden state gives a fixed vector vv. The decoder LSTM starts from vv and factorizes the probability one word at a time:

p(y1,…,yT′∣x1,…,xT)=∏t=1T′p(yt∣v,y1,…,yt−1)p(y_1, \dots, y_{T'} \mid x_1, \dots, x_T) = \prod_{t=1}^{T'} p(y_t \mid v, y_1, \dots, y_{t-1})

Each factor p(yt∣v,y1,…,yt−1)p(y_t \mid v, y_1, \dots, y_{t-1}) is a Softmax over the whole output vocabulary. Every target sentence ends with a special end-of-sentence symbol, EOS. That symbol lets the model put probability on sequences of every possible length, because generation stops when EOS is produced.

The encoder and decoder are two separate LSTMs with separate parameters. This adds parameters at almost no compute cost, and it lets the same architecture be trained on several language pairs. The encoder-decoder pattern is the general shape, and the LSTM is the cell that keeps information alive over long spans.

Training objective

Training maximizes the average log probability of the correct translation TT given the source sentence SS over the training set S\mathcal{S}:

1∣S∣∑(T,S)∈Slog⁡p(T∣S)\frac{1}{|\mathcal{S}|} \sum_{(T, S) \in \mathcal{S}} \log p(T \mid S)

This is the same quantity as the Cross-Entropy Loss for next-word prediction, negated and averaged over sentence pairs. Optimization uses Stochastic Gradient Descent with a fixed learning rate and no momentum.

Decoding

At test time the model needs the most likely translation, T^=arg⁡max⁡Tp(T∣S)\hat{T} = \arg\max_T p(T \mid S). Finding it exactly is too expensive, so the paper uses a left-to-right Beam Search. The decoder keeps the BB best partial translations at each step, extends each one with every vocabulary word, and keeps the best BB again. A hypothesis is moved to the finished set when it emits EOS. The authors found that even a beam of 1 works reasonably, and a beam of 2 gives most of the gain.

Reversing the source

The authors first trained on the source sentence in normal order and found that test perplexity was 5.8 with a BLEU of 25.9. After reversing the source sentences, perplexity dropped to 4.7 and BLEU rose to 30.6. These are the figures in the paper's text (Section 3.3). The results table gives slightly different numbers for the same comparison: 26.17 for the forward model and 30.59 for the reversed one, both with beam 12. The paper does not say why they differ. The table values are labelled with their setting (single LSTM, beam 12), so this lesson uses them, and the text values are unlabelled. Either way the gain is about 4.4 to 4.7 BLEU. The argument is about the minimal time lag. In normal order, each source word is far from its translation in the sequence. Reversal keeps the average distance the same, but it puts the first source words next to the first target words, so the earliest target words can be predicted from information that has just passed through the network. The authors say they lack a complete explanation, but the effect was large.

Architecture and training

The models use four LSTM layers with 1000 cells each and 1000-dimensional word embeddings. The source vocabulary has 160,000 words and the target vocabulary has 80,000, with everything else mapped to an unknown token. Each model has 384 million parameters, of which 64 million are recurrent connections (32 million in the encoder and 32 million in the decoder). The whole sentence is represented by 8000 real numbers: 4 layers of 1000 cells, each cell holding both a hidden value and a memory-cell value.

Training details:

  • Weights start uniform between -0.08 and 0.08.
  • SGD with no momentum, learning rate 0.7. After 5 epochs the rate is halved every half epoch. Training stops at 7.5 epochs.
  • Minibatches of 128 sentence pairs.
  • Gradient norm clipping: if the norm of the averaged gradient exceeds 5, it is rescaled to norm 5. This guards against exploding gradients, which LSTMs can still suffer from. See Gradient Norm Clipping.
  • Sentences in a minibatch are grouped by similar length, which gave a 2x speedup because short sentences would otherwise waste most of the batch.

The training set is a 12-million-sentence subset of WMT'14 with about 348 million French words and 304 million English words. A single GPU ran at about 1,700 words per second, too slow, so each of the four layers ran on its own GPU and the remaining four GPUs computed the softmax in parallel. That reached about 6,300 words per second, and training took roughly ten days.

Results

Translation quality

The paper reports BLEU on the WMT'14 English to French test set, measured with the tokenized multi-bleu script. BLEU compares n-gram overlap with human references.

MethodTest BLEU
Phrase-based SMT baseline33.30
Single forward LSTM, beam 1226.17
Single reversed LSTM, beam 1230.59
Ensemble of 5 reversed LSTMs, beam 133.00
Ensemble of 5 reversed LSTMs, beam 234.50
Ensemble of 5 reversed LSTMs, beam 1234.81

The best direct translation reaches 34.81 BLEU, which is 1.5 points above the baseline. The score is penalized whenever the reference contains a word outside the 80,000-word output vocabulary, so it understates what the model could do with a larger one. The authors note that the best published WMT'14 system at the time scored 37.0 under the same evaluation, so the neural system did not beat the best system, only the phrase-based baseline. The gap between the single reversed model and the single forward model is more than 4 BLEU points, which is the largest effect in the paper.

246810123233343536beam sizetest BLEU
  • 5-LSTM ensemble, reversed source
  • SMT baseline
Beam size matters most at the first step. Going from beam 1 to beam 2 recovers most of the gain, and beam 12 adds only a little more.

Rescoring

The LSTM also rescored the 1000-best lists from the baseline system. Averaging its score with the baseline score gives a BLEU of 36.5 for the ensemble, a gain of 3.2 points over the baseline. A single reversed LSTM reaches 35.85. The paper notes that an oracle that picks the best hypothesis from the same lists would reach about 45, so the lists contain much better translations than any reranker found.

Long sentences

The authors expected long sentences to fail, since earlier models had degraded on them. The paper's sentence-length figure shows no loss on sentences shorter than 35 words and only a small loss on the longest ones. Table 3 in the paper shows long outputs that are largely faithful to the reference, though they can contain unknown-word placeholders and small omissions.

Learned representations

Projecting the final encoder states with PCA shows that sentences with the same meaning cluster together, and that word order changes the representation. Sentences that differ only by active or passive voice stay close.

Why it mattered

The paper showed that a plain LSTM encoder-decoder, trained end to end, could compete with carefully engineered statistical systems on a large translation task, which made sequence-to-sequence learning a default approach. The encoder-decoder pattern carried over to speech, captioning and summarization. The paper also left a clear bottleneck. All the information in a sentence has to pass through one fixed vector, which limits how well very long inputs can be handled. The attention mechanism of Bahdanau et al. (2014) was designed to remove that bottleneck, and the Transformer later replaced recurrence entirely. The reversal trick is a reminder that data ordering and representation can matter as much as the architecture.

Reading guide

Read the paper with these lessons in hand, in reading order:

Check yourself

QuestionWhy does the decoder need an EOS symbol, and what would break without it?Show answer
The EOS token is the end marker that every target sentence ends with. The model learns to produce it at the end of a sentence, so generation can stop. Without it the decoder has no way to choose a length, and beam search would have no point at which a hypothesis is finished.
QuestionThe paper reports a single reversed LSTM with beam 12 at 30.59 BLEU and the forward one at 26.17. What is the difference, and why does reversing help?Show answer
The difference is 4.42 BLEU points. Reversing the source puts the first source words close to the first target words in the sequence the LSTM sees, which reduces the minimal time lag and makes backpropagation easier. The average distance between corresponding words is unchanged, but the early ones become close.
QuestionCompute the parameter-count arithmetic: the paper's model has 4 layers of 1000 cells, 160,000 source words and 80,000 target words with 1000-dimensional embeddings. Roughly how many parameters are in the embedding tables alone?Show answer
The embedding tables have 160,000 times 1000 for the source, which is 160 million, and 80,000 times 1000 for the target, which is 80 million. Together that is about 240 million parameters. The remaining roughly 144 million are the 64 million recurrent connections plus the other LSTM input weights and the 80,000 by 1000 softmax output layer (about 80 million).
QuestionYou want to translate sentences of 60 words or more with a model of this design. Based on the paper, what should you expect, and what would you change?Show answer
The paper reports little degradation on long sentences, so the basic design can work. But the whole sentence must fit in one fixed vector, which is a known bottleneck for long inputs. A reasonable change is to add an attention mechanism over the encoder states, as in Bahdanau et al., so the decoder can look back at the source words directly.

Sources

Read the full paper

Ilya Sutskever, Oriol Vinyals, Quoc V. Le, 2014. arXiv:1409.3215