Advanced Mathematics

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 ii scores every token jj by q⃗i⋅k⃗j/d\vec q_i\cdot\vec k_j/\sqrt{d}, turns the scores into weights with softmax, and outputs the weighted sum of values: softmax(QK⊤/d) V\mathrm{softmax}(QK^\top/\sqrt d)\,V, where dividing by d\sqrt d 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 d\sqrt d scaling and of the causal mask in a language model.
  • Follow the shapes of QQ, KK, VV and the output through one transformer block.

Test yourself

Three tokens are x⃗1=(1,1)\vec x_1 = (1, 1), x⃗2=(1,−1)\vec x_2 = (1, -1), x⃗3=(−1,−1)\vec x_3 = (-1, -1), and the query, key and value matrices are all the identity, so q⃗i=k⃗i=v⃗i=x⃗i\vec q_i = \vec k_i = \vec v_i = \vec x_i and d=2d = 2. Compute the attention output for token 1. Use e2≈4.113e^{\sqrt2} \approx 4.113 and e−2≈0.243e^{-\sqrt2} \approx 0.243.
Show the worked answer
Scores q⃗1⋅k⃗j=2,0,−2\vec q_1\cdot\vec k_j = 2, 0, -2; divided by 2\sqrt2: 2,0,−2\sqrt2, 0, -\sqrt2. Exponentials 4.113,1,0.2434.113, 1, 0.243, sum 5.3565.356, so the weights are ≈0.768,0.187,0.045\approx 0.768, 0.187, 0.045. Output =0.768(1,1)+0.187(1,−1)+0.045(−1,−1)≈(0.909,0.536)= 0.768(1, 1) + 0.187(1, -1) + 0.045(-1, -1) \approx (0.909, 0.536). Token 1 mostly keeps itself, mixed with a little of token 2.

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.

On the way to

Recommended