Exercises — Scaled dot-product attention
Before we begin, one shared vocabulary card, so no symbol is used before it is named:
See Query-Key-Value Model, Softmax Function, and Self-Attention for the ideas these problems lean on.
Level 1 — Recognition
Can you name and shape the pieces?
Exercise 1.1
is and is . What is the shape of ? What does the entry in row 2, column 4 physically mean?
Recall Solution
What we do: multiplies a matrix by an matrix (the flips from to ). Inner dimensions and match, so the product exists and has shape outer dims = .
Why the widths must match: each score is a dot product — you can only pair up numbers if both vectors have the same length .
Meaning of entry : it is , the raw compatibility score between the 2nd query and the 4th key — "how much does question 2 line up with label 4."
Answer: shape ; entry = alignment of query 2 with key 4.
Exercise 1.2
In , which single operation guarantees that each query's weights are non-negative and sum to exactly ?
Recall Solution
Answer: the softmax step.
Why: softmax on a row returns . Each (exponentials are never negative), and dividing by the sum of all of them forces the row to add to . Neither , the scaling, nor can promise this — only softmax does.
Exercise 1.3
If , what number do we divide the scores by before softmax?
Recall Solution
Answer: .
Why not itself? The spread (standard deviation) of a dot product grows like , not . To cancel that spread you divide by exactly . Dividing by would over-shrink and flatten every score toward zero.
Level 2 — Application
Can you turn the crank end-to-end?
Exercise 2.1
Let , and take one query and two keys: Compute the two scaled scores and .
Recall Solution
Step 1 (raw scores): . . Why: is parallel to (identical), so alignment is maximal; and are orthogonal (dot product ), so no alignment.
Step 2 (scale): , so scaled scores and .
Answer: .
Exercise 2.2
Take the scaled scores from 2.1. Compute the attention weights via softmax.
Recall Solution
Step: . Numerically , , sum . Interpretation: the query pours of its attention onto key 1 (the matching one) and onto the orthogonal key 2. Weights sum to . ✓
Exercise 2.3
With weights and values compute the attention output.
Recall Solution
Step (weighted blend): Why: the output is a soft mixture of the value rows, weighted by how much attention each key received. Because key 1 won most of the weight, the output leans strongly toward . Answer: .
Level 3 — Analysis
Can you reason about why the numbers behave as they do?
Exercise 3.1
A model uses . Query and key entries are independent with mean and variance . What are the mean and variance of a raw dot product ? After dividing by , what is the new variance?
Recall Solution
Mean: (independence lets us split the expectation of a product).
Variance: each term has variance , and independent terms add up: .
After scaling: dividing a random variable by a constant divides its variance by . Here , so new variance .
Answer: mean , raw variance ; scaled variance . The whole point of is to force this last number back to .
Exercise 3.2
Two scores are (unscaled, ). Compute softmax both without scaling and with scaling (divide by ). Then compute the softmax "self-derivative" for the first entry in each case. Which case gives a healthier gradient?
Recall Solution
Without scaling. : differences of exponentials, . Since , Self-derivative .
With scaling (divide by ): scores become . Difference , Self-derivative .
Which is healthier? The scaled case: . Its self-derivative sits nearer the maximum possible value (, reached when ). Bigger derivative = stronger learning signal flowing back through Gradient Descent. The figure below makes this concrete.
How to read the figure below. The blue curve is the softmax self-derivative — the size of the learning signal — plotted against the softmax output . Notice it is a hill that peaks at (dashed gray line, height ) and slides toward at both ends. The red dot is the unscaled case (): it has slid partway down the hill to a weaker signal . The green dot is the scaled case (): it sits almost exactly on the peak, signal . The pedagogical point: scaling drags the softmax output back toward the middle where the gradient is strongest, so the model keeps learning; leaving scores large pushes softmax toward or , off the hill, where learning stalls.

Level 4 — Synthesis
Can you assemble pieces you were never shown together?
Exercise 4.1
You are given a causal Attention Mask: query position is only allowed to attend to keys and (not key , which is "in the future"). The scaled scores for query 2 are across keys . Apply the mask by setting the forbidden score to , then softmax. What weights does query 2 get?
Recall Solution
Step 1 (mask): replace the score for the forbidden key 3 with : scores become . Why and not ? After softmax, , so the forbidden key gets exactly zero weight and vanishes from the sum. A score would leave , still leaking attention.
Step 2 (softmax over surviving keys): , , ; sum . Answer: . Notice key 3 — which had the highest raw score — contributes nothing, because the mask forbade it.
Exercise 4.2
Design a tiny numeric (with , one query, two keys) so that after scaling the softmax gives exactly equal weights . Explain the geometric condition.
Recall Solution
Requirement: equal softmax weights happen iff the two scaled scores are equal, i.e. . Geometric meaning: the query must be equally aligned with both keys — e.g. it bisects them, or is orthogonal to both.
A clean construction: let , , . Then and : equal. Scaling by keeps them equal at each. Softmax of two equal numbers is always . Check: . ✓
Level 5 — Mastery
Can you invent, defend, and connect?
Exercise 5.1
A colleague proposes scaling by (not ) "to be safe." Using and query/key entries of variance , compute the resulting variance of the scaled scores and argue what goes wrong during training.
Recall Solution
Variance after dividing by : raw variance , divided by , gives . What goes wrong: the scaled scores now have standard deviation — far too small. Almost every score sits in a razor-thin band near , so softmax outputs are nearly uniform ( for every key) no matter what the query asks. The model becomes blind to which key is actually relevant. Over-shrinking is just as harmful as not shrinking. The Goldilocks point: makes the variance exactly — scores neither explode (dead gradients) nor collapse (uniform, uninformative attention).
Exercise 5.2
Split one head into heads of each (as in Multi-Head Attention). For a single head, what is the correct scaling factor now, and what variance does each head's scaled score have? Give one reason multi-head is preferred over a single wide head.
Recall Solution
Scaling per head: each head has its own , so it divides by . Variance: raw dot product over terms of variance has variance ; dividing by gives . Every head is independently normalised to variance . Why multi-head wins: four narrow heads can each learn a different kind of relationship (syntax, position, coreference, …) and their outputs are concatenated. One wide head must cram all relationships into a single attention pattern. Same total width, more diverse behaviour, and each head keeps a well-conditioned variance- score distribution.
Exercise 5.3 (capstone)
Full pipeline, no scaffolding. , . One query, two keys: Compute the final attention output, showing scaled scores, weights, and blend. Then state in one sentence what the output "means."
Recall Solution
Step 1 — raw scores: ; . Step 2 — scale by : . Step 3 — softmax: , , sum . Step 4 — blend values: . Answer: output . Meaning: the query aligns strongly with key 1 (parallel) and not at all with key 2 (orthogonal), so it retrieves almost entirely — a soft "lookup" that returns mostly the content tied to the matching key.
Recall One-line self-test before you close the page
Softmax runs along which axis of the score matrix? ::: Along each row — one row is one query spending its full 100% of attention across all keys.
Connections
- Parent: Scaled Dot-Product Attention
- Prerequisites & neighbours: Softmax Function · Query-Key-Value Model · Self-Attention · Cross-Attention · Multi-Head Attention · Attention Mask · Positional Encoding · Gradient Descent