What is a sequence-to-sequence (seq2seq) model?
A sequence-to-sequence (seq2seq) model is a neural network that maps an input sequence to an output sequence of a possibly different length. An encoder reads the input and compresses it into a context vector, and a decoder generates the output one token at a time from that vector. In the original 2014 form, both halves are recurrent networks.
Seq2seq models made neural machine translation practical and replaced phrase-based systems at Google Translate and elsewhere within a few years. The same encoder-decoder layout handles summarization, speech recognition, question answering, and code generation. Its main weakness, squeezing a whole input into one vector, led directly to attention and then to the Transformer, which keeps the encoder-decoder design in models such as T5 and BART.
Key ideas
- An encoder turns the input sequence into a context vector; a decoder turns that vector into the output sequence.
- Input and output lengths are independent, which suits translation and summarization.
- The decoder is autoregressive: each output token is fed back as the next input.
- Teacher forcing trains the decoder on correct previous tokens instead of its own predictions.
- Beam search keeps the k most probable partial outputs instead of only the single best one.
- One fixed-size context vector limits quality on long inputs, which attention removes.
Common questions
What is the difference between seq2seq and a Transformer?
Seq2seq names the task setup, mapping one sequence to another with an encoder and a decoder. The original seq2seq models built both halves from RNNs. The Transformer is an architecture that fills the same two roles with attention layers, so it is itself a seq2seq model, just not a recurrent one.
What are <SOS> and <EOS> tokens?
<SOS> (start of sequence) is the first input to the decoder, giving it something to condition on before any word has been produced. <EOS> (end of sequence) is a token the decoder learns to emit when the output is complete, which is how the model decides its own output length.
How are seq2seq models evaluated?
Translation is usually scored with BLEU, which counts how many n-grams of the output also appear in reference translations. Summarization often uses ROUGE, which focuses on recall of reference n-grams. Both are rough proxies, so human ratings and learned metrics such as COMET are used alongside them.
Is GPT a seq2seq model?
Not in the strict sense. GPT is decoder-only: the input and output share one sequence, and the model continues it token by token. It can still do seq2seq tasks such as translation by reading the source text as a prompt and generating the target after it.
Related explainers
- Recurrent Neural Networks — How RNNs read and understand sequences
- LSTMs — Gates and memory cells for long sequences
- How Self-Attention Works — Query, key, value — the core of self-attention
- Cross-Attention — Attending across sequences and modalities
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
- Language Modeling — Predict the next token — the objective that powers GPT, BERT, and modern NLP
References
- Sutskever, Vinyals & Le (2014), "Sequence to Sequence Learning with Neural Networks", NeurIPS
- Cho et al. (2014), "Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation", EMNLP
- Cho et al. (2014), "On the Properties of Neural Machine Translation: Encoder-Decoder Approaches"
- Bahdanau, Cho & Bengio (2014), "Neural Machine Translation by Jointly Learning to Align and Translate"
- Wu et al. (2016), "Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation"
Last updated