Sequence-to-Sequence Models
A sequence-to-sequence (seq2seq) task maps an input sequence to an output sequence . The two lengths can differ. Output position need not correspond to input position . Translation is the standard example. An encoder-decoder model solves it with two RNNs that have separate weights:
- Encoder: reads the input one token at a time with the update . Its final state is the summary of the whole input.
- Decoder: starts from the state and the input token
<start>. At step it computes and outputs a probability for every token in the vocabulary. It stops after it emits<stop>.Training minimizes the cross-entropy of the true output tokens, summed over the output steps:
Backpropagation through time carries the gradient back through the decoder, into and through the encoder.The decoder’s input at step is the previous output token. Training and inference fill it differently:
- Teacher forcing: during training, the decoder receives the true token . Every step then sees a correct history.
- Autoregressive generation: at inference no true tokens exist. The decoder receives its own previous output .
Each step produces a distribution, but the next step needs a single token. A decoding rule picks it:
- Greedy: take the most probable token, .
- Sampling: draw at random from the distribution.
- Beam search: keep the most probable partial outputs and extend each of them by one token per step.
The design has one weak point. The decoder sees the input only through the fixed-size vector . This bottleneck is the problem that attention solves.
Intuition
Take the sentence “Give me the book” and its translation into another language. The encoder reads the four English words and ends in the state . The decoder then writes the translation one word at a time. At each step it outputs probabilities over the target vocabulary. Three candidate first words might get , and . Training compares these probabilities with the true translation. Generation turns them into one word, feeds that word back in, and repeats.
The lecture draws each RNN block as a state update followed by a small MLP, with residual connections. The encoder and the decoder can unroll for different numbers of steps. The input sentence sets , and the output sets . The encoder’s last state is the initial condition of the decoder.
The weak point is the size of . A 40-word sentence must fit into the same number of state entries as a 3-word sentence. The first input word also has to survive state updates before the decoder sees it. A U-Net faces a similar problem in images and solves it with skip connections. Pixel of the output reads pixel of the input. In translation that fixed wiring fails, because word order differs between languages. The decoder cannot know in advance which input position holds the word it needs. It needs a lookup that it can learn, and that lookup is attention.
Properties
(No Cheating Under Teacher Forcing) The true token enters the decoder only at step , after the loss for step .
Under teacher forcing the decoder reads the true output sequence, so it might seem able to copy the answer. It cannot, because the wiring only runs forward in time. The prediction for depends on and the encoder state. The token itself arrives one step later as an input. The same rule holds if the model predicts <stop> too early. Training still compares that prediction with the true token. The next step still receives the true token.
(Exposure Mismatch) A model trained only with teacher forcing never sees its own mistakes as inputs. At inference it must continue from them.
At inference the decoder conditions on , and one wrong token changes every later input. Feeding the model’s own predictions during training avoids this mismatch. It also makes the gradients noisier, because later steps then train on wrong histories, so training is slower. The Spring 2023 lecture 14 notes describe the usual compromise. During training, the decoder receives its own prediction at randomly chosen steps and the true token at the others. These noisy inputs act as a regularizer, and the decoder learns to recover from its own errors.
(The Loss Trains the Encoder) Every decoder loss sends gradient into , and from there into all encoder steps through the shared encoder weights.
The encoder has no loss of its own. Its only training signal is the gradient of with respect to . That signal asks the encoder to put into whatever the decoder needs. One consequence comes from the Spring 2023 notes. Data for English to Spanish can help an English to French model, since both train an English encoder. For outputs that are real numbers, such as a robot trajectory, a squared loss replaces the cross-entropy. Nothing else changes.
(Greedy Decoding Can Miss the Best Sequence) The sequence made of the most probable token at each step need not be the most probable sequence.
The probability of an output is the product . A first token with high probability can lead to continuations that are all unlikely. Beam search with width keeps the best partial outputs, ranked by this product. At each step it extends each of them by every token. It then keeps the best of the candidates, where is the vocabulary size. Width is greedy decoding. Beam search still does not guarantee the most probable sequence, but it finds better ones as grows. The Medium example below shows a case where greedy decoding fails and succeeds.
(Reshaping the Distribution Before Sampling) Temperature, top- and nucleus sampling change the distribution before a token is drawn.
Not Taught in Lecture
The lecture covers greedy decoding and plain sampling, and the Spring 2023 notes add beam search. Top- sampling follows Prince §12.7.3. Temperature and nucleus sampling follow the notes to Prince chapter 12 and Holtzman et al. (2020).
Let be the score of token before the softmax. Sampling at temperature uses . Small sharpens the distribution toward greedy decoding. Large flattens it toward a uniform draw. Top- sampling keeps only the most probable tokens, renormalizes, and samples. Nucleus sampling keeps the smallest set of top tokens whose total probability reaches a threshold , such as . The long tail of unlikely tokens has a large total probability. These rules stop the decoder from drawing a token from that tail.
(The Bottleneck) The decoder reads the input only through , so long inputs lose detail and early inputs fade.
In a linear encoder , the first input reaches through the factor . For a stable that factor shrinks geometrically with , as in 5.3.2. Backpropagation Through Time. The gradient from the decoder back to shrinks by the same factor. A larger state helps only partly, since every input must still pass through the same updates. Attention removes the bottleneck. Each decoder step can read every encoder state directly, with weights that depend on the content.
Examples
(Easy) Teacher-Forced Loss
A decoder is trained on the target “le chat
<stop>”. With teacher forcing it assigns the true tokens the probabilities , and at steps , and .Compute the training loss and the average loss per token. The average per token is . The second step contributes most, because the model gave the true token only . Under teacher forcing, step still receives the true token "chat", even though the model was unsure of it.
The loss adds the negative log-probabilities of the true tokens:
(Medium) Greedy Decoding Versus Beam Search
A decoder has the vocabulary . At step it gives , and . After , step gives , and for , and
<stop>. After , step gives , and .Find the output of greedy decoding and of beam search with , and their probabilities. Greedy decoding picks at step . At step it picks
<stop>with . The output "<stop>" has probability .Beam search keeps () and () after step . Step scores all six extensions:
The best two are ”<stop>” with and ”<stop>” with . Both are finished, so beam search returns ”<stop>”. Its probability is , almost twice the greedy result. Greedy decoding failed because was slightly less likely at step but led to a confident ending.
(Hard) How Fast the First Word Fades
A scalar linear encoder computes with . The decoder starts from .
Find the weight of in for , and . Compare it with the weight of , and explain what attention changes. Unrolling gives . The last input has weight . The first input has weight . This is for , for and for . In a 40-word sentence, the first word counts about times less than the last one.
The derivative is the same number. Any loss on the decoder therefore sends a gradient to the first word that is times weaker. A weight closer to helps the first word. Then old inputs never fade, and the state mixes everything.
With attention, a decoder step reads all states and forms with learned weights . The state holds with weight . A step that needs the first word can put most of its weight on . The gradient to is then scaled by that weight instead of by .
See Also
- 5.3.1. RNNs, the recurrent blocks inside the encoder and the decoder.
- 5.3.2. Backpropagation Through Time, which trains the model and explains why early inputs fade.
- 7.1. Attention, which replaces the single state with a lookup over all encoder states.
- 7.5. Encoder and Decoder Models, the transformer version of the encoder-decoder.
- 6.4. U-Net, the image model whose skip connections inspired attention.
- 2.2.4. Log Loss, the cross-entropy loss at each output step.