Sub topic 2
How an Encoder-Decoder Model Works
Assume you know basic Python, neural-network training, PyTorch tensors, and the high-level idea of Transformer self-attention. This chapter focuses on the working model: how a source sequence becomes a target sequence in an encoder-decoder Transformer.
The sequence-to-sequence view
An encoder-decoder model maps one sequence of tokens to another:
- Source sequence: the input, such as an English sentence or a long article.
- Target sequence: the output, such as a German translation or a short summary.
- Encoder: the network that reads and represents the complete source sequence.
- Decoder: the network that generates the target sequence one token at a time.
The source and target can have different lengths. A translation might turn ten English tokens into twelve German tokens. A summary might turn hundreds of tokens into twenty.
The model first processes the source. It then predicts a special beginning-of-sequence token and continues predicting the next token until it produces an end-of-sequence token.
What the encoder computes
The tokenizer converts the source text into token IDs. An embedding layer maps each ID to a vector. Positional information is added so that the model can distinguish “dog bites man” from “man bites dog.”
Each encoder layer usually contains:
- Self-attention, which lets every source position use information from other source positions.
- A feed-forward network, which transforms each position independently.
- Residual connections and layer normalization, which help the stack train reliably.
The encoder can attend to the entire source at once. Its output is a sequence of contextual vectors, often called the encoder hidden states. A vector for the word “bank,” for example, can reflect nearby words that indicate whether the text discusses finance or a river.
What the decoder computes
The decoder also contains Transformer layers, but it has two attention operations with different jobs.
Masked self-attention lets the decoder examine tokens that have already been generated. A causal mask prevents it from looking at future target tokens. When predicting token 5, it may use tokens 1 through 4, but not token 6.
Cross-attention connects the decoder to the encoder. The decoder creates a query from its current state. The encoder hidden states provide keys and values. This lets the decoder assign different attention weights to source positions. While generating a translated noun, it may focus on the corresponding source noun. While generating a summary phrase, it may focus on several important parts of the article.
The decoder then passes its state through a linear layer and a softmax function. The resulting probability distribution covers the target vocabulary.
| Component | Can read | Main purpose |
|---|---|---|
| Encoder self-attention | All source tokens | Build contextual source representations |
| Decoder masked self-attention | Earlier target tokens | Model target-side order and context |
| Decoder cross-attention | Encoder representations | Select relevant source information |
| Output softmax | Decoder state | Predict the next target token |
Autoregressive generation
At inference time, generation is autoregressive. The model generates one token, adds it to the target prefix, and uses the longer prefix to generate the next token.
For example:
- Input the source and the start token.
- Predict
Das. - Input the source and
Das; predictWetter. - Input the source and
Das Wetter; predictist. - Continue until the end token or a length limit.
The decoder does not need to recompute the encoder for every step. Implementations also cache previous decoder key and value tensors, which makes later generation more efficient.
The most likely next token is not always the best complete sequence. Generation algorithms such as greedy search and beam search compare possible continuations. Their settings affect length, repetition, and translation choices.
Training versus generation
During training, the model commonly receives the correct target prefix while learning to predict the next target token. This is called teacher forcing. The loss compares each predicted probability distribution with the known next token.
During generation, the correct target is unavailable. The model must use its own previous predictions. This difference can cause errors to accumulate: an early wrong token can lead the decoder toward a poor later sequence.
Exercise
Why does the decoder need both masked self-attention and cross-attention?
Show answer
Masked self-attention provides information from previously generated target tokens without revealing future tokens. Cross-attention provides relevant information from the encoded source sequence.