liveBook · Manning

Transformer architecture

As explained in Build a Text-to-Image Generator (from Scratch)

A transformer architecture is a neural-network architecture that uses attention mechanisms to process relationships among elements in a sequence or other structured input.

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

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Builds an encoder–decoder translator from embeddings through attention, masks, training, and prediction, then extends the pipeline to image tokens.
Examples
Works through German-to-English translation and image generation from a panda prompt, including intermediate image-token outputs.
What it assumes
Assumes machine-learning fundamentals and PyTorch neural-network training while teaching transformer attention and architecture through implementation.
What you can do after
Build it from scratch in PyTorch as a trained encoder–decoder translator, then build a transformer-based text-to-image generator.
How it connects
Connects self-attention, multi-head attention, positional encoding, machine translation, vision transformers, VQGAN, and diffusion models.

Also explained in

Compare how 28 books explain transformer architecture →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.