Attention, Demystified: From Dot Products to Explaining Transformers
Goal: Understand how transformer language models work well enough to explain attention to a colleague · 2 weeks, about 4 hours per week
Explaining attention convincingly requires three things: fluency with the handful of linear-algebra operations transformers are built from, a mechanical understanding of how query/key/value projections turn a sequence into context-aware representations, and a mental model of how attention blocks stack into a working language model. You already have the programming ability to read and run reference implementations, so the gap is mostly conceptual vocabulary plus refreshed matrix intuition. This two-week plan spends the first week rebuilding the math and tracing a single attention head by hand and in NumPy, and the second week scaling up to full transformer blocks, training/inference behavior, and rehearsing a clear verbal explanation.
1Week 1, Part 1: The Math You Actually Need
Reconstruct the specific linear-algebra operations transformers use — dot products as similarity, matrix multiplication as batched projection, and softmax as a weighting function — without needing a full linear algebra course.
- Dot Products as Similarity ScoresstartedCompute the dot product of two vectors by hand and explain why a larger dot product between normalized vectors means the vectors point in more similar directions.
- Matrix Multiplication as a Batch of ProjectionsGiven a (sequence_length × d_model) matrix and a (d_model × d_k) weight matrix, state the output shape and explain what each row of the result represents.
- Softmax and Turning Scores into WeightsApply softmax to a vector of raw scores by hand, verify the outputs sum to 1, and explain how scaling scores up or down sharpens or flattens the resulting distribution.
- Dot Products as Similarity Scoresstarted
2Week 1, Part 2: From Text to Vectors
Trace how a string of text becomes the numeric matrix that attention operates on, and explain why positional information must be added explicitly.
- Tokenization and the Embedding TableExplain how a subword tokenizer splits text into token IDs and how an embedding lookup converts those IDs into a sequence of vectors; predict the shape of the resulting matrix for a given input.
- Why Position Must Be EncodedDemonstrate that attention is permutation-invariant without positional information, and describe at least one scheme (sinusoidal or learned) for injecting position.
- Tokenization and the Embedding Table
3Week 2, Part 1: Attention Itself
Explain scaled dot-product attention mechanically — every matrix, every shape, every reason — and implement a single head in NumPy.
- Queries, Keys, and ValuesDescribe the role of Q, K, and V using a retrieval analogy, and state which learned weight matrix produces each from the input embeddings.
- The Attention Formula, Line by LineWrite out softmax(QKᵀ/√d_k)V from memory and justify each component, including why the scores are divided by √d_k.
- Implementing One Attention Head in NumPyWrite a working single-head attention function in ~20 lines of NumPy and verify the attention weight matrix rows sum to 1.
- Causal Masking and Autoregressive GenerationModify the attention implementation to apply a causal mask, and explain why a language model predicting the next token must not attend to future positions.
- Queries, Keys, and Values
4Week 2, Part 2: The Full Model and the Explanation
Assemble attention into a complete transformer, understand what training and inference actually do, and deliver a clear five-minute explanation to a colleague.
- Multi-Head AttentionExplain why multiple heads with smaller d_k are used instead of one large head, and trace how head outputs are concatenated and projected back to d_model.
- Inside a Transformer BlockDiagram a full block — attention, residual connection, layer norm, feed-forward network — and state the purpose of each residual and normalization step.
- Training and Generation in PracticeExplain next-token prediction with cross-entropy loss, and describe how a trained model generates text one token at a time, including the role of temperature in sampling.
- Rehearsing the ExplanationDeliver a five-minute whiteboard explanation of attention using one worked example sentence, and answer three likely follow-up questions (why not RNNs, what the heads learn, why it scales quadratically with sequence length).
- Multi-Head Attention