Transformer

You now have every building block:

PieceWhat it doesWhere you learned it
EmbeddingsTurn token IDs into dense vectorsFoundations
Self-AttentionLet every token look at every other tokenAttention
Multi-Head AttentionRun multiple attention perspectives in parallelAttention
Positional EncodingStamp position information onto each vectorAttention
Layer Norm & ResidualsKeep activations stable across deep stacksFoundations
Cross-AttentionLet one sequence attend to anotherAttention

One piece is missing: the Feed-Forward Network, the “thinking” step that follows attention’s “gathering” step. Once you have that, every component snaps together into a full transformer.

What You’ll Learn in This Chapter

You’ll assemble the final transformer pieces: the Feed-Forward Network, the repeatable transformer block, and the main encoder-decoder, encoder-only, and decoder-only variants. You’ll also see how transformers train, generate text, use the KV cache, and carry real numbers from raw text to a next-token prediction. The chapter ends with a Python lab: train a small GPT from random weights, evaluate it, and save and reload your model.

Chapter Roadmap

ModuleWhat You’ll Do
Architecture OverviewMeet the FFN, assemble a transformer block, compare three architecture variants
TrainingUnderstand next-token prediction, cross-entropy loss, teacher forcing, and the training loop
InferenceSee how autoregressive generation works, why the KV cache matters, and how sampling strategies shape output
End-to-End WalkthroughTrace real numbers through every stage of a decoder-only transformer
Train Your Own GPTPrepare text, train and evaluate a small GPT, save a checkpoint, and generate text

Start with the Architecture Overview →