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.
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
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:
The states are 1, and . 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 bracketed one-coordinate vector is copied across the model boundary: context becomes decoder initial state . 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
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:
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
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 , and . 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
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
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
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
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 . Keeping the lower-ranked first token allows un chat, with probability . 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
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:
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.