Transformers and the attention mechanism
Build a complete Transformer in PyTorch, block by block, and understand why this architecture replaced recurrent networks on every language and vision benchmark since 2017.
Course duration: 8h
What you will learn
- Explain the two limits of recurrent networks that attention removes
- Compute attention by hand on three tokens, then vectorise it
- Assemble multi-head attention, positional encoding and residual blocks
- Distinguish an encoder (BERT), a decoder (GPT) and an encoder-decoder (T5)
- Reason about the quadratic cost of attention and the tricks that tame it
- Train a full Transformer from scratch on a toy task and read its attention maps
Prerequisites
- Course 11 (recurrent networks and LSTM): what a sequence model does
- A framework: course 08 (TensorFlow) or course 09 (PyTorch)
- Solid Python, NumPy and basic linear algebra: matrix multiplication, softmax
Course modules
- The limits of recurrent networks that attention removes
- Query, key, value: attention step by step
- Multi-head attention
- Positional encoding: absolute, then rotary
- Residual connections and layer normalization
- The encoder: BERT and understanding models
- The decoder: GPT and generation
- Encoder-decoder: T5 and translation
- Quadratic cost and efficient attention
- Implementing a complete Transformer block
The red thread
Every module extends the same object: a Transformer written by hand in PyTorch, about two hundred lines, built one block at a time. Single-head attention appears in module 2, multi-head in 3, positional encoding in 4, residuals and normalization in 5. The blocks are assembled into an encoder in module 6, a decoder in module 7, an encoder-decoder in module 8. Module 10 trains the finished object on a toy task — first reverse-copy, then date translation, where "3 March 2026" must come out as "2026-03-03". The task is small enough to run on a laptop, and its attention maps are readable, which is why we chose it. Real-world models (BERT, GPT, T5) appear as points of comparison, loaded from the Hugging Face Transformers library.
Assessment and certificate
The course ends with a 40-question exam covering every module: the arithmetic of attention on three tokens, sizing multiple heads, matching a family (encoder, decoder, encoder-decoder) to a task, catching a missing causal mask, and estimating memory as sequence length grows. On success, a certificate of completion is issued; its number can be verified by anyone on the platform.
Free courses, by contrast, end with a 5-question quiz and a certificate preview, without certification.