A transformer processes a sequence by using attention to weight relationships between its elements, including elements far apart. In an encoder–decoder transformer, the encoder builds a representation of the input; the decoder uses it to predict output tokens one at a time. This makes the architecture useful for translation and, when images are represented as tokens, text-to-image generation. section 2.1 section 2.1.2 section 2.0
Trace a German-to-English translation through the model:
- Tokenize each sentence, turn its tokens into word embeddings, and add positional encodings so the model has information about token order. section 2.1.1 section 2.5
- Pass the German embeddings through repeated encoder layers. Multi-head self-attention relates positions in the input; feed-forward layers, residual connections, and layer normalization complete each layer. section 2.3.2 section 2.5
- In each decoder layer, masked self-attention uses earlier output tokens without looking ahead. Cross-attention incorporates the encoder output, and a generator produces probabilities over the target vocabulary for the next token. section 2.3.2 section 2.1.2
Figure 2.4 An encoder–decoder transformer for machine language translation (e.g., German to English)
Use the encoder–decoder diagram to follow the path from an input sentence through its encoded representation to successive output-token predictions. section 2.1.2
Build and train a translator
For “Wie geht es dir” → “How are you,” implement the encoder, decoder, embeddings, masks, and output generator as a PyTorch Transformer class. Train on batches of German–English pairs, with the next English token as the prediction target. The example uses roughly 29,000 translation pairs. section 2.1.1 section 2.3.2 section 2.2 section 2.4
The same sequence-prediction idea extends to images: a transformer can predict image tokens from a text prompt and previously generated tokens, while a VQGAN decoder reconstructs the image. In min-DALL-E, a BART encoder represents the prompt and a BART decoder predicts the image-token sequence. Attention also appears in diffusion-based systems, but a diffusion model is not simply an autoregressive image-token transformer. Text-to-image systems still face questions about reproduced training data and geometric consistency. section 1.2 section 12.0 section 2.0 section 1.5



























