Visual explainer
Sequence-to-Sequence
Compress variable-length input sequences into a fixed context vector with a recurrent encoder, then unroll it autoregressively with a decoder.
Standard feedforward neural networks and synchronous recurrent models assume that input and output sequences have identical dimensions () and monotonic point-to-point alignments. However, in foundational natural language applications such as machine translation, text summarization, and dialogue generation, sequence lengths inherently diverge.
Translating the three-word English sentence "the cat sat" () into French requires four words: "le chat est assis" (). Rigid slot-to-slot classification collapses because:
- Length Asymmetry: The target sequence contains an extra token that possesses no synchronous corresponding slot in the input stream.
- Syntactic Reordering: Word orders vary across languages (e.g. adjectives following nouns in Romance languages or verbs moving to clause boundaries in German).
To handle variable-length, non-monotonic mapping, the Sequence-to-Sequence (Seq2Seq) framework decouples input ingestion from output generation through an intermediate memory buffer.
Fold Words Into State
The Encoder component of a Seq2Seq network ingests the input sequence sequentially. At each time step , the recurrent cell updates its internal hidden state vector by combining the current input embedding with the previous state:
Starting from an initial zero state :
- Step 1: Reading "the" () updates state to , encoding initial determiner syntax.
- Step 2: Reading "cat" () combines with to produce , incorporating the subject noun.
- Step 3: Reading "sat" () combines with to reach the terminal state .
Each step compresses fresh token information into the running hidden state. Because the recurrent weights are shared across all positions, the encoder can process sequences of arbitrary length.
Two Numbers Hold All
When the encoder reads the final input token at step , its terminal hidden state is emitted as the context vector :
This context vector serves as the sole bridge between the encoder and the decoder. In classical Seq2Seq models (Sutskever et al., Cho et al.), is a fixed-dimensional vector (e.g. or numbers).
The context vector represents an extreme informational bottleneck: it must encapsulate the semantic meaning, grammatical mood, tense, and relational dependencies of the entire source sentence. Once emitted, directly initializes the decoder's starting hidden state:
The decoder cannot query the original tokens again; whatever was lost during the encoder's recurrent compression is gone permanently.
Write One, Reread One
The Decoder operates as an autoregressive language model conditioned on the context vector . It generates the output sequence one token at a time:
The generation loop unrolls step by step:
- Step 1: Initialized with , the decoder receives a special start-of-sequence token . It computes state and projects through a softmax vocabulary head to emit .
- Step 2: The emitted token "le" is fed back as the input for step 2. Combined with state , the cell updates to and emits .
- Step 3: "chat" feeds back to produce , emitting .
- Step 4: "est" feeds back to produce , emitting .
- Step 5: "assis" feeds back into step 5, where the softmax head emits the special end-of-sequence token , signaling the model to halt generation.
By feeding its own previous prediction back into the input slot, the decoder maintains grammatical consistency across arbitrary output lengths.
Eyes Beat Bottlenecks
While the fixed context vector works adequately for short sentences ( words), it deteriorates rapidly on longer sequences.
In a recurrent encoder, each successive step partially overwrites earlier activations. By the time an RNN reaches word 6 or 20, the gradient signal and informational trace of the initial tokens decay exponentially through repeated Jacobian products (). In practice, only of early concepts survive into the final vector .
To eliminate this bottleneck, Bahdanau et al. introduced the Cross-Attention Mechanism:
- Instead of discarding intermediate encoder hidden states , the decoder retains direct access to all of them.
- At each decoding step , the decoder compares its current query state against each encoder state to compute dynamic alignment weights .
- When translating the subject noun, the decoder sends a focused attention beam directly to , retrieving its representation with signal fidelity.
With attention, memory capacity scales dynamically with input length, rescuing of concepts and removing the fixed-vector constraint.
Guide, Then Let Go
Training a Seq2Seq model introduces a subtle operational dilemma between training stability and deployment realism:
1. Training: Teacher Forcing
During training, the decoder is guided using teacher forcing: at step , the input provided to the decoder is the true ground-truth target token , regardless of what token the model actually predicted. Teacher forcing stabilizes training gradients and prevents an early erroneous prediction from derailing the entire sequence during backpropagation.
2. Inference: Free-Running (Flying Solo)
At test time in production, ground-truth answers are unavailable. The decoder must feed its own generated predictions back into itself.
This discrepancy causes exposure bias: the model was trained exclusively on pristine ground-truth prefixes, making it fragile when exposed to its own mistakes. If the model makes an early error at step 1 (emitting "un" instead of "le"), it encounters a state trajectory unseen during training, leading to compounding hallucination cascades. Engineering pipelines mitigate exposure bias through scheduled sampling—gradually replacing ground-truth tokens with model predictions during late training epochs.
Where It Breaks
The fundamental theoretical limit of vanilla Seq2Seq is the Fixed-Capacity Collapse Cliff:
As input sentence length increases from to tokens:
- Attention-Augmented Seq2Seq: Translation quality (measured by BLEU score) remains steady and robust (), because the dynamic attention matrix scales with sentence length .
- Plain Fixed-Vector Seq2Seq: BLEU score falls off a cliff—dropping from at length 10 down to at length 30, and collapsing to at length 50.
Because a fixed-size vector possesses finite informational Shannon capacity, attempting to compress an entire paragraph into a single static array forces the network to overwrite initial tokens with the sentence tail. In real-world production translation and summarization, vanilla fixed-vector Seq2Seq has been entirely superseded by attention-based architectures and the Transformer.
The Quick Version
- Sequence-to-sequence translation decouples input ingestion from output generation to handle length mismatches ().
- The recurrent encoder sequentially folds input tokens into a running hidden state: .
- A single fixed context vector serves as the information bottleneck bridge seeding the decoder ().
- The decoder unrolls autoregressively, feeding each generated token back in to predict the next word until .
- Fixed vectors suffer exponential recurrent decay, dropping early tokens on sequences longer than 10 words.
- Dynamic cross-attention solves the bottleneck by providing direct query-key memory access to all encoder states.
- Teacher forcing stabilizes training, but scheduled sampling is required to prevent inference exposure bias.