Sequence-to-Sequence Model

Machine Learning
Introduced by
Sutskever, I.; Cho, K.
Year
2014
Type
Encoder-decoder neural network (deep learning)

What does it do?

The sequence-to-sequence (Seq2Seq) model, introduced in 2014 by Sutskever, Vinyals and Le and by Cho and colleagues, is an encoder-decoder neural network that maps an input sequence to an output sequence whose length can differ from the input.

It is used for machine translation, text summarization, dialogue systems and code generation. Its central design choice is to separate reading the input from producing the output.

The intuition behind the method

Many tasks convert one sequence into another whose length is not fixed in advance: a sentence in English may become a sentence of a different length in French. A classifier with a fixed output cannot express this. Seq2Seq divides the work between two networks. The encoder reads the whole input and condenses it into a context representation, and the decoder then generates the output one token at a time, with each token conditioned on that context and on the tokens already produced.

History

The encoder-decoder framework appeared in 2014 in two near-simultaneous papers: Sutskever, Vinyals and Le's 'Sequence to Sequence Learning with Neural Networks' and Cho and colleagues' RNN encoder-decoder for statistical machine translation. The early formulations compressed the whole source into one fixed context vector. Adding attention, which lets the decoder access all encoder states, addressed this bottleneck and improved output quality, and later architectures such as the Transformer, which relies on attention, followed.

When to use it

Choose Seq2Seq when the task maps one variable-length token sequence to another, such as translation, summarization, dialogue or code generation, and when you have paired source-target examples that represent the mapping you need. If the output is a fixed label or a single number, a standard classifier or regressor is the better choice. Do not rely on this model when paired data are scarce: with few pairs the decoder receives too little signal to learn the mapping and is prone to overfitting. In that case, consider pretrained sequence models or fine-tuning, simpler rule-based or retrieval baselines, or collecting more paired data. Tree-based models such as Random Forest or XGBoost are suited to ordinary tabular prediction. They do not map token sequences to sequences, so they are not a substitute for this model on text-to-text tasks.

Limitations and considerations

  • Requires a substantial corpus of paired source-target examples; the amount needed depends on the task and vocabulary.
  • Training is computationally demanding, and GPU hardware is generally recommended.
  • In the basic form, the entire source is compressed into one context vector c, so information from long inputs can be lost without attention.
  • The decoder generates output from a learned conditional distribution, so its output can be fluent while still departing from the source; fluency alone does not verify correctness.
  • Performance depends on how closely the training data match the deployment data, including domain and vocabulary coverage; inputs that differ substantially from the training data can degrade output quality.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Input and output lengths are independent, so the same architecture handles tasks such as translation, where the output length differs from the input and is not fixed in advance.
  • One framework covers several generation tasks, including translation, summarization, dialogue and code generation, with the task largely determined by the training pairs.
  • The mapping is learned end to end from paired examples, without hand-designed alignment rules between source and target tokens.
  • Adding attention lets the decoder draw on all encoder states, which addresses the single-vector bottleneck of the basic model.

Applications

  • Machine translation between language pairs.
  • Abstractive text summarization.
  • Dialogue and conversational response generation.
  • Code generation from natural-language descriptions.

How it works

How it works

1

The encoder reads the input one token at a time and updates its internal state. In the basic formulation, the final state serves as a single context vector c that summarises the source sequence. Because the whole input is read before decoding starts, inputs of different lengths are handled by the same network.

Formula
c = \text{Encoder}(x_1, \dots, x_T)
2

At each step the decoder conditions on c and on all previously generated tokens, and produces a probability distribution over the next token. The next token is selected by a decoding procedure: greedy decoding picks the most probable token at each step, while beam search keeps several candidate sequences and chooses among them by overall score. The choice affects both output quality and generation speed, and the best option depends on the task, so it should be checked on held-out data. Generation ends when a stop token is emitted, so the output length is determined during generation rather than fixed beforehand.

Formula
p(y_t \mid y_{<t}, c)
3

The encoder and decoder are trained jointly by maximising the log-likelihood of each target sequence given its source, using paired source-target examples as supervision. A common extension is attention, which lets the decoder consult all encoder states rather than only the single vector c; this addresses the difficulty of compressing a long input into one vector. Training is computationally demanding, so GPU hardware is generally recommended.

Formula
\max_{\theta} \sum \log p(y \mid x; \theta)

Common pitfalls

  • Training on too few source-target pairs, where the decoder overfits rather than learning the mapping.
  • Omitting attention on long inputs, where the single context vector discards distant information.
  • Training with the correct previous tokens at every step, which is the usual practice, means the decoder never sees its own errors during training, so mistakes can compound at generation time.
  • Stopping training early because CPU-only runs are slow, before the model has converged.
  • Using Seq2Seq for a fixed-output task that a classifier or regressor handles more simply.

Frequently asked questions

How much data does Seq2Seq need?

It needs paired source-target examples. No validated minimum size exists, because the amount required depends on the task, the vocabulary and the model size. Judge whether more pairs are needed with held-out validation: if the model overfits or fails to generalise beyond the training pairs, more data or a different approach is needed. With very little paired data, consider pretrained sequence models or fine-tuning, simpler rule-based or retrieval baselines, or collecting more pairs.

Do I need a GPU?

A GPU is generally recommended. Training an encoder-decoder over many sequence pairs is computationally demanding, and CPU-only training is often slow.

What does adding attention do?

Attention lets the decoder look back at all encoder states at each step instead of relying on one fixed context vector. This reduces the information loss on long inputs and often improves output quality.

How is Seq2Seq different from a classifier?

A classifier produces a fixed output such as a single label. Seq2Seq produces a variable-length output sequence whose length is decided during generation, which is what makes it suitable for translation, summarization and similar tasks.

Related methods

Sources

  1. Sutskever, I., Vinyals, O. & Le, Q. V. (2014). Sequence to Sequence Learning with Neural Networks. NeurIPS.

    Open publication reference
  2. Cho, K., van Merriënboer, B., Gulcehre, C., Bahdanau, D., Bougares, F., Schwenk, H. & Bengio, Y. (2014). Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation. EMNLP, 1724–1734.

    Open DOI reference