Attention and Transformers
Each token builds its new representation as a softmax-weighted average of all tokens' values, weighted by how well its query matches their keys.
Needs a step first: 48
In a transformer every token's embedding is mapped by three learned matrices to a query, a key and a value. Token scores every token by , turns the scores into weights with softmax, and outputs the weighted sum of values: , where dividing by keeps scores from growing with dimension and saturating the softmax. Several heads run in parallel, and blocks of attention plus small feed-forward networks, with residual connections and normalisation, are stacked many times. Because every token can look at every other in one step, transformers capture long-range context and train well in parallel.
Where it shows up: Transformers are the architecture of language models and of many image, audio and protein models.
How to hold it: In your head
See attention as each token asking a question with its query, scoring every key by a dot product, and taking the softmax-weighted average of the values.
What knowing it looks like
- Compute one attention head's output for three 2-dimensional tokens by hand.
- Explain the role of the scaling and of the causal mask in a language model.
- Follow the shapes of , , and the output through one transformer block.
Test yourself
Three tokens are , , , and the query, key and value matrices are all the identity, so and . Compute the attention output for token 1. Use and .
Show the worked answer
Needs a step first: 48
Learn it with an AI
Copy this prompt into any AI assistant. It will take you through step by step at your level and finish with the test above.
Be my maths tutor. I'm learning "Attention and Transformers" (Frontier tier): Each token builds its new representation as a softmax-weighted average of all tokens' values, weighted by how well its query matches their keys.
I already know: Embeddings and Similarity, Softmax and Language Models, Training Deep Networks.
Teach it Socratically: first ask what I already know; then explain the core idea with one concrete example and why it's true; then give me three problems, one at a time, easy to hard. Wait for my answer, check every step, and point out mistakes without giving the answer away.
Finish by testing me with this: Three tokens are $\vec x_1 = (1, 1)$, $\vec x_2 = (1, -1)$, $\vec x_3 = (-1, -1)$, and the query, key and value matrices are all the identity, so $\vec q_i = \vec k_i = \vec v_i = \vec x_i$ and $d = 2$. Compute the attention output for token 1. Use $e^{\sqrt2} \approx 4.113$ and $e^{-\sqrt2} \approx 0.243$.
Use LaTeX for formulas. When you say where it's used later (machine learning, quantum mechanics), be specific.Needs first
- Embeddings and SimilarityRepresenting words, images or items as vectors learned so that similar things point in similar directions, compared by cosine similarity.Frontier · Machine Learning
- Softmax and Language ModelsSoftmax turns a list of scores into probabilities; a language model uses it to predict the next token, trained by cross-entropy.Frontier · Machine Learning
- Training Deep NetworksThe practical craft that makes deep networks learn: sensible initialisation, normalisation, learning-rate schedules, minibatches and watching validation loss.Frontier · Machine Learning
Leads to
This is the end of a path.
On the way to
Recommended
- Neural Networks: Zero to HeroAndrej Karpathy · VideosFree lectures that build backpropagation, a language model and a small GPT from scratch in Python, explaining every line.
- Understanding Deep LearningSimon J. D. Prince (MIT Press) · BookA clear, well-illustrated 2023 textbook covering networks, training, transformers, diffusion models and reinforcement learning, free as a PDF with Python notebooks.
- 动手学深度学习阿斯顿·张、李沐等 · Book · 中文The Chinese edition of Dive into Deep Learning: a free textbook in which every model, from linear regression to transformers, runs as code beside the maths.
- 跟李沐学AI李沐 · Videos · 中文Mu Li's own Bilibili channel: the video course for Dive into Deep Learning and paragraph-by-paragraph readings of key papers, including the Transformer paper.