Module 13: Transformers and Attention
This final module introduces the transformer architecture that powers modern AI systems like BERT and GPT. You will understand how attention mechanisms work, why they enable processing of long-range dependencies, and how to use pre-trained transformers for text classification and other NLP tasks.
Learning Objectives
- Explain the limitations of recurrent networks that attention addresses.
- Describe the intuition behind attention mechanisms.
- Understand the Query-Key-Value formulation of self-attention.
- Explain the complete transformer architecture.
- Contrast encoder-only, decoder-only, and encoder-decoder models.
- Fine-tune pre-trained transformers using Hugging Face.
The module in two minutes
An animated overview of the module’s through-line. Watch it before the lecture — it is the map, not the territory.
Lecture shorts
Short lectures and code walkthroughs from the instructor that reinforce this module. Some open partway into a longer video — that chapter is the part matched to this module.
Visual shorts
Short silent animations, one idea each. Press play, or open one full screen. All visual shorts
Orientation audio
Attention, step by step — the mechanism under modern AI, built small enough to read.
The through-line
Module 9 left text as averaged word vectors — order ignored, context lost. The transformer is the architecture that fixed this, and it runs essentially every system currently called AI. This module builds its core mechanism small enough to read.
Attention is weighted looking. For each position, compute a query; against every position, compute keys and values; score query against keys, softmax the scores into weights, and take the weighted sum of values. Each word assembles its representation by attending to the words that matter for it — "it" can look at whatever "it" refers to, forty tokens back, in one step. The scaling by the square root of the key dimension is the small detail that keeps softmax gradients alive, and you will see why in code.
Three refinements complete the block. Causal masking zeroes attention to future positions, which is what makes next-token generation honest at training time — and connects directly to the prediction machine you may have met elsewhere: this is how those models are trained. Multi-head attention runs several attention patterns in parallel — one head tracking syntax, another coreference. Positional encodings inject order, because attention alone treats a sentence as a bag.
Assembled — attention, residuals, layer norm, feedforward — these form the encoder block, and stacking such blocks is, to a first approximation, the whole architecture. The course ends on a deliberate note: the exotic frontier of AI is matrix multiplies, softmax, and gradient descent — every piece of which you have now implemented from scratch.
Click any token to make it the query. Its attention weights over every position — softmax of the query-key scores — appear as bars. Then squeeze the scores and mask the future.
Query “it” and the weight lands on “animal” forty characters back, in one step — the long-range link averaged word vectors could never make. The scale slider is why the √dₖ division exists: unscaled scores saturate softmax into a one-hot spike and its gradients die. The causal mask zeroes attention to future positions — what makes next-token training honest, and what turns this machine into a text predictor at all.
Key concepts
- Attention: Learn which parts of input to focus on
- Self-Attention: Relate different positions within a sequence
- Query-Key-Value: The attention computation framework
- Transformer: Architecture based entirely on attention
- Pre-training: BERT, GPT, and transfer learning
The lecture notebook
Six parts, from scratch before any framework. Run every cell; change things and rerun — the notebook is the lecture.
In Colab: File → Save a copy in Drive keeps your work.
Exercise
Attempt the exercise before opening the solution — the attempt is where the learning happens.
Knowledge check
The quiz is self-graded and open-book. If a question surprises you, the corresponding lecture part is the fastest fix.
Reference and discussion
Finish the module
Complete the stages above, then claim the finish here.