Sequence to Sequence Learning
Encoder-decoder architecture for mapping sequences to sequences
Updated
Contents
- Why Students Should Care
- The Problem
- The Solution: Encoder-Decoder
- Interactive Demo
- The Architecture
- Encoder (LSTM)
- Decoder (LSTM)
- Key Innovations
- 1. Reversing the Input Sequence
- 2. Deep LSTMs
- 3. Beam Search Decoding
- Training
- The Bottleneck Problem
- From Seq2Seq to Attention
- Applications
- Evolution
- Why It Mattered
- Code Example
- Common Confusion
- Where To Go Next
- Key Papers
Sequence-to-Sequence (Seq2Seq) is a neural architecture that turns one sequence into another: read the input with an encoder, then write the output with a decoder. Introduced by Sutskever et al. at Google in 2014, it enabled neural machine translation and became the foundation for modern language models.
This page assumes basic familiarity with recurrent networks — read Understanding LSTMs first if needed. Seq2Seq’s main weakness led directly to Bahdanau Attention and then the Transformer.
Why Students Should Care
- The encoder-decoder split introduced here is still the mental model behind translation, summarization, and chat systems.
- Seq2Seq is the first step in the chain that leads to attention and Transformers — you cannot appreciate what attention fixed without knowing this design.
- Practical ideas born here — teacher forcing, beam search — are still used to train and run today’s language models.
The Problem
Traditional neural networks require fixed-size inputs and outputs. But many tasks have variable lengths on both sides:
- Translation: “Hello” (1 word) → “Bonjour” (1 word)
- Translation: “How are you?” (3 words) → “Comment allez-vous?” (2 words)
There is no fixed slot count you can design a plain feed-forward network around. Seq2Seq needed a way to map any-length input to any-length output.
The Solution: Encoder-Decoder
Split the problem into two parts:
- Encoder: read the entire input sequence and compress it into a fixed-size vector (the “thought vector” or “context”)
- Decoder: generate the output sequence, one token at a time, starting from this context vector
Think of it as: read the whole sentence, form a thought, then say the thought in the other language.
Interactive Demo
Watch how Seq2Seq encodes a sentence and decodes its translation:
Seq2Seq: Encoder-Decoder
The Architecture
Encoder (LSTM)
The encoder is an LSTM that processes the input sequence one token at a time, updating its hidden state:
The final hidden state becomes the context vector — a summary of everything it read.
Decoder (LSTM)
The decoder is another LSTM that generates the output sequence , feeding each generated word back in as input for the next step:
The decoder is initialized with — the “thought” is its starting point.
Key Innovations
1. Reversing the Input Sequence
Reversing the source sentence improved translation significantly:
Original: "A B C" → "X Y Z"
Reversed: "C B A" → "X Y Z"
Why it helps: the first source word ends up right next to the first target word, so the RNN has a shorter path between corresponding words at the start of the sentence.
2. Deep LSTMs
Using 4-layer LSTMs significantly outperformed shallow networks:
| Depth | BLEU Score |
|---|---|
| 1 layer | 25.9 |
| 2 layers | 29.6 |
| 4 layers | 34.8 |
3. Beam Search Decoding
Greedy decoding (taking the single most likely word at each step) can lock in an early mistake. Beam search instead keeps the top-k candidate sequences at every step:
Beam size = 3:
Step 1: [The, A, This]
Step 2: [The cat, The dog, A cat, ...]
...select top 3 sequences by total probability
Training
Training uses teacher forcing: at each step, feed the decoder the ground-truth previous token rather than the model’s own (possibly wrong) prediction:
The takeaway: during training the model always sees the correct history, which keeps learning stable; at inference it must rely on its own outputs.
The Bottleneck Problem
The fixed-size context vector must encode the entire input, and that is the design’s Achilles’ heel:
- Works well for short sequences
- Degrades for long sequences (information gets compressed away)
- Led directly to the invention of attention mechanisms
From Seq2Seq to Attention
The limitation of a single context vector motivated Bahdanau attention (2014):
Instead of one fixed summary, each decoder step gets its own weighted mix of all encoder states — the decoder can look back at whichever input words it needs. This solved the bottleneck.
Applications
Seq2Seq enabled a huge range of “sequence in, sequence out” tasks:
| Task | Input | Output |
|---|---|---|
| Translation | ”Hello world" | "Bonjour le monde” |
| Summarization | Long article | Short summary |
| Dialogue | User message | Response |
| Code generation | Description | Code |
| Speech recognition | Audio features | Text |
Evolution
| Year | Model | Innovation |
|---|---|---|
| 2014 | Seq2Seq | Encoder-decoder RNNs |
| 2014 | Bahdanau | Attention mechanism |
| 2015 | Luong | Simplified attention variants |
| 2017 | Transformer | Self-attention, no recurrence |
| 2018+ | BERT, GPT | Pre-trained transformers |
Why It Mattered
Seq2Seq established:
- End-to-end learning: no hand-crafted features or alignment
- Encoder-decoder paradigm: used by transformers today
- Variable-length I/O: fundamental for language tasks
- Transfer learning path: pre-trained encoders/decoders
Code Example
A minimal Seq2Seq loop with teacher forcing:
class Seq2Seq(nn.Module):
def __init__(self, encoder, decoder):
self.encoder = encoder
self.decoder = decoder
def forward(self, src, trg):
# Encode: get context from final hidden state
_, (hidden, cell) = self.encoder(src)
# Decode: generate output sequence
outputs = []
input = trg[0] # <SOS> token
for t in range(1, len(trg)):
output, (hidden, cell) = self.decoder(input, hidden, cell)
outputs.append(output)
input = trg[t] # Teacher forcing
return torch.stack(outputs)
Common Confusion
- Seq2Seq is an architecture pattern, not one specific model. The 2014 paper used LSTMs, but “encoder-decoder” describes Transformers too.
- The context vector is not attention. Vanilla Seq2Seq has exactly one fixed summary vector; attention (a later addition) replaces it with a per-step weighted mix.
- Teacher forcing happens only during training. At inference time the decoder must consume its own predictions.
Where To Go Next
- Read Understanding LSTMs for the recurrent cells inside encoder and decoder
- Read Bahdanau Attention for the fix to the bottleneck problem
- Read Transformer for the architecture that replaced recurrence entirely
- Read GPT for modern decoder-only sequence generation
Key Papers
- Sequence to Sequence Learning with Neural Networks – Sutskever et al., 2014
https://arxiv.org/abs/1409.3215 - Learning Phrase Representations using RNN Encoder-Decoder – Cho et al., 2014
https://arxiv.org/abs/1406.1078 - Neural Machine Translation by Jointly Learning to Align and Translate – Bahdanau et al., 2014
https://arxiv.org/abs/1409.0473 - Effective Approaches to Attention-based Neural Machine Translation – Luong et al., 2015
https://arxiv.org/abs/1508.04025