Skip to content
AI360XpertBeta
Core ML

Visual explainer

Sequence-to-Sequence

Follow recurrent encoding, token feedback, teacher forcing and sequence search, then see an exact collision in a toy fixed context.

Input and output sequences can have different lengths.
Input and output sequences can have different lengths.

Sequence-to-sequence models separate reading an input from generating an output, so their lengths and alignments can differ. The example has three English words and four French words; we count whitespace-delimited words, not subword tokens. The brackets count each row without inventing a one-to-one alignment. An encoder-decoder is a general structure; this page follows its recurrent, fixed-context form and then adds attention.

Update The Encoder State

The encoder updates one running state for each input.
The encoder updates one running state for each input.

Inputs enter from below; bars above the common zero line encode the running state at 40 drawing units per state unit. For checkable arithmetic, assign the words the, cat and sat synthetic scalar features 1, 2 and 3. Use a deliberately linear toy recurrence:

ht=0.5ht−1+xt,h0=0.h_t=0.5h_{t-1}+x_t,\qquad h_0=0.

The states are 1, 0.5×1+2=2.50.5\times1+2=2.5 and 0.5×2.5+3=4.250.5\times2.5+3=4.25. This illustrates recurrent compression; it is not a trained translation model, a semantic coordinate or an LSTM equation. Real encoders use learned vector representations and richer state updates.

Pass The Final Context

The final encoder state initializes a separate decoder.
The final encoder state initializes a separate decoder.

The bracketed one-coordinate vector is copied across the model boundary: context c=h3=[4.25]c=h_3=[4.25] becomes decoder initial state s0=cs_0=c. Encoder and decoder are separate parameterized models. Their state dimensions must match for a direct copy; an implementation can learn a projection instead. An LSTM implementation must also handle its cell state, omitted by this one-state toy.

The original sequence-to-sequence paper describes a recurrent encoder representation conditioning a recurrent decoder. Our scalar calculation illustrates the handoff, not that paper's trained system. The target words in the following trace are a stipulated translation example, not outputs numerically derived from the toy scalar.

Feed Tokens Back

Generated tokens become inputs for later decoding steps.
Generated tokens become inputs for later decoding steps.

SOS is a start symbol. The first decoder step produces le; the mint route feeds that chosen token into the next step, while the separate upper edge carries hidden state. The second step produces chat, followed by est and assis in the illustrative trace. An embedding maps each discrete previous token into the decoder's input representation.

Autoregressive generation factors the target probability into ordered conditionals:

p(y1,…,yT∣x)=∏t=1Tp(yt∣y<t,c).p(y_1,\ldots,y_T\mid x)=\prod_{t=1}^{T}p(y_t\mid y_{<t},c).

Each distribution depends on context and the preceding target prefix. Feeding back a token permits conditioning on earlier choices; it does not guarantee grammatical or correct output.

Form A Fresh Context

Attention forms a fresh weighted context from encoder states.
Attention forms a fresh weighted context from encoder states.

Now retain all three encoder states instead of exposing only the last. The stack's segment heights encode weighted contributions, at 60 drawing units per contribution unit. With chosen attention weights (0.1, 0.8, 0.1), contributions are 0.1×1=0.10.1\times1=0.1, 0.8×2.5=20.8\times2.5=2 and 0.1×4.25=0.4250.1\times4.25=0.425. They add to context 2.525. The middle contribution dominates and is outlined in mint. Weights sum to one; this is a synthetic alignment, not a measurement of retrieval fidelity.

Bahdanau attention computes a weighted sum of encoder annotations for each decoding step. Its learned alignment uses the previous decoder state to score source positions. Our scalar states isolate the weighted-sum arithmetic; the paper uses richer bidirectional annotations. Keeping source states adds storage and scoring work, and does not guarantee accurate alignment. See attention for the score-to-weight mechanism.

Train With The True Prefix

Teacher forcing supplies the true previous target during training.
Teacher forcing supplies the true previous target during training.

Suppose the first prediction is un but the reference begins le. Under teacher forcing, the predicted token is blocked from the next input; the gold token le goes into step two. At inference the reference is unavailable, so the model's un goes forward. The scene isolates token selection; recurrent state dependencies still exist.

Training on reference prefixes and generating from model prefixes creates a distribution mismatch. Errors may compound, but this does not force an inevitable failure. Scheduled sampling is one proposed modification, not a required or universally successful remedy. No mitigation is assumed in this example.

Recognize Restricted Access

A decoder with only final context cannot query earlier encoder states.
A decoder with only final context cannot query earlier encoder states.

The lower route is the decoder's available context input. The upper attempted query terminates at a cross: this fixed-context decoder has no interface to the earlier states h₁ and h₂. Dashed outlines mean not exposed to the decoder, not proven erased from the encoder's summary. This structural restriction can make preserving needed distinctions harder; it does not establish a universal sentence-length threshold or a BLEU decline. The original paper even reports successful long-sentence results under its setup.

Stop On EOS

An end token stops variable-length generation.
An end token stops variable-length generation.

After the fourth ordinary word, assis becomes the input to decoder decision five. Choosing EOS, the end-of-sequence symbol, stops generation. It is a model decision rather than a fixed four-slot output. In practice generation also needs a maximum-length guard because a model may fail to emit EOS. The terminal bar has no continuation to another generated word.

Compare Whole Sequences

The best next token need not begin the best sequence.
The best next token need not begin the best sequence.

Switch to a separate, synthetic two-step decoding fixture. First probabilities are p(le) = 0.6 and p(un) = 0.4. After le, p(chat) = p(chien) = 0.5. After un, p(chat) = 0.9 and p(chien) = 0.1. Short diagonal stubs denote the omitted chien branches, not zero probability.

Greedy decoding picks le first and can choose chat under the stated tie rule, giving joint probability 0.6×0.5=0.300.6\times0.5=0.30. Keeping the lower-ranked first token allows un chat, with probability 0.4×0.9=0.360.4\times0.9=0.36. The right bars encode these products at 300 units per probability unit. Assume identical EOS factors afterward, so they do not change the ranking. A beam retaining both first-step candidates finds the better sequence in this tiny tree; a finite beam does not guarantee global optimality generally. Probable output and correct translation are separate criteria.

Where It Breaks

Distinct inputs can collapse to the same fixed context.
Distinct inputs can collapse to the same fixed context.

Return to the toy encoder, now with two input streams and different required next labels. Stream (1, 0) should lead to A; stream (0, 0.5) should lead to B. Their calculations are:

cA=0.5(1)+0=0.5,cB=0.5(0)+0.5=0.5.c_A=0.5(1)+0=0.5,\qquad c_B=0.5(0)+0.5=0.5.

Both routes merge into exactly the same context. If the decoder has the same starting prefix and only that context, its next-token distribution must be identical for both cases. The crossed split marks the desired distinction it cannot make. Sampling could produce different tokens by chance, but it cannot reliably condition that choice on the lost source distinction.

This is an exact failure of this specified encoder, not proof that every fixed vector loses the first word or that arbitrary real vectors have a stated finite Shannon capacity. A learned encoder may preserve the relevant distinction; exposing more encoder states through attention can provide additional access. Neither architecture guarantees faithful output for every input.

The Quick Version

  • Input and output lengths can differ.
  • An encoder repeatedly updates its running state.
  • Final context can initialize a separate decoder.
  • Generated tokens feed later autoregressive steps.
  • Attention builds a context from accessible encoder states.
  • Teacher forcing supplies reference tokens during training.
  • Fixed context alone provides no earlier-state query interface.
  • EOS stops variable-length generation.
  • Greedy next-token choices can miss better whole sequences.
  • An encoder collision removes distinctions the decoder may need.