Skip to content
AI360Xpert
Beta
Core ML

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.

Three English words become four French words with no slot to slot map.
Three English words become four French words with no slot to slot map.

Standard feedforward neural networks and synchronous recurrent models assume that input and output sequences have identical dimensions (Tx=TyT_x = T_y) 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" (Tx=3T_x = 3) into French requires four words: "le chat est assis" (Ty=4T_y = 4). 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 folds the cat sat into one running summary.
The encoder folds the cat sat into one running summary.

The Encoder component of a Seq2Seq network ingests the input sequence (x1,x2,…,xTx)(x_1, x_2, \dots, x_{T_x}) sequentially. At each time step tt, the recurrent cell updates its internal hidden state vector hth_t by combining the current input embedding with the previous state:

ht=tanh⁡(Wxhxt+Whhht−1+bh)h_t = \tanh\left(W_{xh} x_t + W_{hh} h_{t-1} + b_h\right)

Starting from an initial zero state h0=[0,0]h_0 = [0, 0]:

  1. Step 1: Reading "the" (x1x_1) updates state to h1=[0.20,0.45]h_1 = [0.20, 0.45], encoding initial determiner syntax.
  2. Step 2: Reading "cat" (x2x_2) combines with h1h_1 to produce h2=[0.55,0.30]h_2 = [0.55, 0.30], incorporating the subject noun.
  3. Step 3: Reading "sat" (x3x_3) combines with h2h_2 to reach the terminal state h3=[0.80,0.30]h_3 = [0.80, 0.30].

Each step compresses fresh token information into the running hidden state. Because the recurrent weights (Wxh,Whh)(W_{xh}, W_{hh}) are shared across all positions, the encoder can process sequences of arbitrary length.

Two Numbers Hold All

Three tokens compress into vector 0.8, 0.3 that seeds the decoder.
Three tokens compress into vector 0.8, 0.3 that seeds the decoder.

When the encoder reads the final input token at step TxT_x, its terminal hidden state is emitted as the context vector cc:

c=hTx=[0.80,0.30]c = h_{T_x} = [0.80, 0.30]

This context vector serves as the sole bridge between the encoder and the decoder. In classical Seq2Seq models (Sutskever et al., Cho et al.), cc is a fixed-dimensional vector (e.g. d=512d = 512 or 10241024 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, cc directly initializes the decoder's starting hidden state:

s0=cs_0 = c

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 emits 4 tokens one by one and feeds each back in.
The decoder emits 4 tokens one by one and feeds each back in.

The Decoder operates as an autoregressive language model conditioned on the context vector cc. It generates the output sequence (y1,y2,…,yTy)(y_1, y_2, \dots, y_{T_y}) one token at a time:

P(Y∣X)=∏t=1TyP(yt∣y<t,c)P(Y \mid X) = \prod_{t=1}^{T_y} P\left(y_t \mid y_{<t}, c\right)

The generation loop unrolls step by step:

  1. Step 1: Initialized with s0=cs_0 = c, the decoder receives a special start-of-sequence token ⟨SOS⟩\langle\text{SOS}\rangle. It computes state s1s_1 and projects through a softmax vocabulary head to emit y1="le"y_1 = \text{"le"}.
  2. Step 2: The emitted token "le" is fed back as the input for step 2. Combined with state s1s_1, the cell updates to s2s_2 and emits y2="chat"y_2 = \text{"chat"}.
  3. Step 3: "chat" feeds back to produce s3s_3, emitting y3="est"y_3 = \text{"est"}.
  4. Step 4: "est" feeds back to produce s4s_4, emitting y4="assis"y_4 = \text{"assis"}.
  5. Step 5: "assis" feeds back into step 5, where the softmax head emits the special end-of-sequence token ⟨EOS⟩\langle\text{EOS}\rangle, 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

Attention keeps word 1 alive while fixed context drops it.
Attention keeps word 1 alive while fixed context drops it.

While the fixed context vector works adequately for short sentences (<10< 10 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 (0.256≈0.00020.25^6 \approx 0.0002). In practice, only 22 of 66 early concepts survive into the final vector cc.

To eliminate this bottleneck, Bahdanau et al. introduced the Cross-Attention Mechanism:

  • Instead of discarding intermediate encoder hidden states (h1,h2,…,hTx)(h_1, h_2, \dots, h_{T_x}), the decoder retains direct access to all of them.
  • At each decoding step tt, the decoder compares its current query state sts_t against each encoder state hih_i to compute dynamic alignment weights αt,i\alpha_{t,i}.
  • When translating the subject noun, the decoder sends a focused attention beam directly to h1h_1, retrieving its representation with 85%85\% signal fidelity.

With attention, memory capacity scales dynamically with input length, rescuing 55 of 66 concepts and removing the fixed-vector constraint.

Guide, Then Let Go

Train with true tokens then switch to your own.
Train with true tokens then switch to your own.

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 tt, the input provided to the decoder is the true ground-truth target token yt−1∗y_{t-1}^*, 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 y^t−1\hat{y}_{t-1} 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

One fixed vector forgets early words on long inputs.
One fixed vector forgets early words on long inputs.

The fundamental theoretical limit of vanilla Seq2Seq is the Fixed-Capacity Collapse Cliff:

As input sentence length increases from 1010 to 5050 tokens:

  • Attention-Augmented Seq2Seq: Translation quality (measured by BLEU score) remains steady and robust (34.0→31.534.0 \to 31.5), because the dynamic attention matrix scales with sentence length O(Tx⋅Ty)\mathcal{O}(T_x \cdot T_y).
  • Plain Fixed-Vector Seq2Seq: BLEU score falls off a cliff—dropping from 32.532.5 at length 10 down to 19.519.5 at length 30, and collapsing to 7.07.0 at length 50.

Because a fixed-size vector c∈Rdc \in \mathbb{R}^d 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 (Tx≠TyT_x \ne T_y).
  • The recurrent encoder sequentially folds input tokens into a running hidden state: ht=tanh⁡(Wxt+Uht−1)h_t = \tanh(W x_t + U h_{t-1}).
  • A single fixed context vector c=hTxc = h_{T_x} serves as the information bottleneck bridge seeding the decoder (s0=cs_0 = c).
  • The decoder unrolls autoregressively, feeding each generated token back in to predict the next word until ⟨EOS⟩\langle\text{EOS}\rangle.
  • 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.