The one operation every frontier model is built around, derived from a soft dictionary lookup, with every shape, gradient, mask, and FLOP accounted for.
Attention as a soft dictionary lookup
Chapter 3 ended with one operation in three steps. A query scores every stored vector, a softmax turns the scores into weights, and the output is the weighted average of the stored vectors (§3.6). In its barest form, the one you wrote in Exercise 3.6d, every row of plays every part at once,
You also met the knobs. The score can be additive (Bahdanau) or a dot product (Luong), dividing the scores by a temperature slides the result from a plain average to a hard pick of one vector, and padding is hidden by setting its scores to before the softmax.
That operation is not yet a layer that could replace an RNN, and Chapter 3 closed with a list of what it lacks. This chapter removes the items one at a time. This section takes the first two. In Exercise 3.6d (and in Bahdanau's model, where the annotations were both scored and averaged) the same vector was what got matched and what got read out. What changes when those are two different things, as they are in every dictionary? And Chapter 3 said the soft version of the lookup is the one a network can learn. Why exactly, how strong is the learning signal, and how does temperature change it? The answers frame the rest of the chapter, from the learned projections of §4.2 to the scaling of §4.3.
Keys are what you match, values are what you get
Every program you have written uses lookups. rent["two-bedroom"] finds the key that matches and hands back its value, and the key and the value are different objects, often of different types. Let's make that numeric. We have entries. Entry has a key , which is what a query is compared against, and a value , which is what comes back when the entry is chosen. A query arrives. A hard lookup scores every key with some similarity function, picks the winner, and returns its value,
The query and the keys must live in the same space, since they are compared. The values need not, which is why can differ from . Chapter 3's attention is the special case .
It helps to write the same lookup as a weighted sum. Let be the one-hot vector . Then
In plain words, a hard lookup is a weighted sum of all the values in which one weight is 1 and the rest are 0.
Here is a tiny example we will use throughout the section. The keys are apartment sizes, 40, 60, 80, and 100 square meters, and the values are monthly rents of 900, 1200, 1500, and 2100. The similarity is closeness, . The question is about size and the answer is about money, so neither could stand in for the other. That is the separation of keys from values in its simplest form, and §4.2 shows why a Transformer wants it even when both come from the same token. Ask for a 69.9 m² apartment and the nearest key is 60, so you get 1200. Ask for 70.1 m² and you get 1500.
A lookup you cannot learn
A change of 0.2 m² in the query moved the answer by 300, while a small change almost anywhere else moves it by nothing at all. That staircase is the problem. The output is piecewise constant in and in every , so its derivative is zero almost everywhere and undefined at the jumps. Gradient descent asks "if the query were slightly different, how would the output change?" and the hard lookup answers "not at all" until, suddenly, it changes completely. A network built on hard lookups gets no signal about what it should be looking for. There is a second, quieter problem. A hard lookup cannot blend. If the right answer lies between two entries, it cannot say so.
Soft weights, and why softmax makes them
The fix is to let the weights be any probability vector, not just a one-hot one,
In plain words, the output is a weighted average of the values, and the weights say how much each entry counts.
A weighted average with nonnegative weights that sum to 1 is a convex combination. Geometrically, always lies inside the convex hull of the values, the smallest convex shape containing all the . A hard lookup sits at a corner of that shape, and a uniform average sits near its middle. Everything in between is now available.
Chapter 3 made the weights with a softmax of the scores , , and you have used the same function since §0.4. It is worth asking why this function, of all the ways to turn scores into weights. We want weights that are positive for any real score (including negative ones), that grow with the score, that are smooth so that gradients flow, and that can come as close to the hard argmax as we like. The exponential gives all four, through one property,
Every extra unit of score multiplies an entry's weight relative to another by . Only differences between scores matter. Adding the same constant to every score changes nothing, because
This shift invariance is more than a curiosity. It is why we can subtract the largest score before exponentiating (the stable softmax of §0.4), and in §4.2 it will tell us that one of the biases people put in attention layers does nothing at all.
Why not just divide each score by the sum of the scores? Scores can be negative, so the "weights" could be negative. The sum can be zero, so you could divide by zero. And is not invariant to shifts, so the answer would depend on an arbitrary choice of zero point. The name also deserves a correction. §0.4 showed that log-sum-exp is the smooth version of the max, , and that its gradient is the softmax. The gradient of the max itself is the one-hot indicator of the winner, so softmax is really a soft argmax, a smooth stand-in for "which one is biggest", written as a probability vector. That is exactly what a soft lookup needs.
Back to the rents. With the similarity and a query of 75 m², the softmax weights depend on the width , which sets how quickly similarity falls off with distance.
| (m²) | weights on 40, 60, 80, 100 | soft rent | hard rent |
|---|---|---|---|
| 20 | 0.090, 0.315, 0.404, 0.191 | 1466 | 1500 |
| 10 | 0.002, 0.259, 0.704, 0.035 | 1442 | 1500 |
| 5 | 0.000, 0.018, 0.982, 0.000 | 1495 | 1500 |
With a narrow the soft lookup is almost the hard one. With a wide it blends the neighbors. Every entry is a smooth function of the query, so the network can learn.
Temperature decides how hard the lookup is
The width in the rent example is a special case of the knob you met in §3.6. Divide every score by a temperature before the softmax,
In plain words, a low temperature exaggerates the differences between scores, and a high temperature shrinks them.
Chapter 3's widget showed the two limits. Here is why they hold. The ratio of two weights is now . Suppose the largest score is unique. For any other entry, , so as the ratio . All the weight flows to the winner, and the soft lookup becomes the hard one. As every ratio tends to , so the weights become uniform and the lookup returns the plain average of all the values.
What happens in between is less obvious. Here are the numbers for three scores , together with the entropy of the weights, which measures how spread out they are (0 for a one-hot vector, for a uniform one, see §0.5).
| weights | entropy | |
|---|---|---|
| 0.25 | 0.982, 0.018, 0.000 | 0.094 |
| 0.5 | 0.864, 0.117, 0.019 | 0.454 |
| 1 | 0.659, 0.242, 0.099 | 0.847 |
| 2 | 0.502, 0.304, 0.194 | 1.026 |
| 10 | 0.366, 0.331, 0.303 | 1.096 |
The entropy rises steadily with temperature. That is not a coincidence of this example. It is a theorem, and the proof is short enough to be worth knowing.
∂ Entropy always increases with temperature
Write (the inverse temperature), , and . Then , so
where is the average score under the weights. Two derivatives are needed. First, . Second, , so
Putting the pieces together,
The variance is zero only when all the scores are equal. Otherwise entropy strictly decreases as grows, which means it strictly increases with . ∎
Monotonicity is what makes the temperature a well-behaved control. For any target sharpness between "one-hot" and "uniform" there is exactly one temperature that achieves it, and you can find it by bisection (Exercise 4.1d does).
You will meet temperature all over the book. Dividing attention scores by is a fixed temperature chosen for a statistical reason, the subject of §4.3. Sampling from a language model at temperature is the same formula applied to the next-token logits (§6.4). And contrastive image-text models learn their temperature as a parameter, because the right sharpness for matching images to captions is not known in advance. CLIP initializes it at and clips it so that the logits are never multiplied by more than 100 (Radford et al. 2021).
Drag the query in the widget below. Switch between the two similarity functions, and sweep the temperature from nearly hard to nearly uniform.
That last point deserves a sentence of its own. With dot-product scores, the length of the query acts as an inverse temperature. A network that can change the norms of its queries and keys can therefore choose, token by token, how sharp each lookup should be. This is part of why dot-product attention is so flexible, and also, as §4.3 will show, part of why it can become unstable.
Why the soft version can be learned
Now the payoff. How does the output change when one score changes? We need the derivative of a softmax weight with respect to a score, which §0.2 derived with the quotient rule as the softmax Jacobian,
where is 1 when and 0 otherwise. The output is , so
In plain words, raising the score of entry pulls the output toward that entry's value, with a strength equal to the entry's current weight.
This little formula explains a lot. If entry currently has almost no weight, nudging its score barely matters. If it already has nearly all the weight, then and nudging it barely matters either. Gradients flow best when the weights are spread over a few candidates. That is our first glimpse of saturation, the failure mode the scaling of §4.3 exists to prevent. With a temperature, the formula picks up a factor , so very hot or very cold lookups both learn slowly, for opposite reasons. The hard lookup is the limit , where the weights are one-hot and the gradient is exactly zero.
We can go one step further and differentiate with respect to the query itself, using dot-product scores , so . By the chain rule,
where . The second form is allowed because , so subtracting adds nothing.
In plain words, the Jacobian of the output with respect to the query is the covariance between values and keys under the attention weights. The query learns to move in key directions that are associated with the values the loss wants. With , as in Chapter 3, this is the covariance of the stored vectors with themselves, so a query can find an entry only by resembling what it wants to read. Separate keys let it find an entry by one property and read out another, the way the rent example finds an apartment by its size and reads its price.
Let's verify that claim numerically before believing it.
A batched soft lookup with separate keys and values
Write soft_lookup_batch(Q, K, V, tau=1.0) for a whole batch of queries at once. Q has shape (m, d_k), K has shape (n, d_k), and V has shape (n, d_v). Score with dot products, divide by tau, and return (O, A), where A of shape (m, n) holds one row of weights per query and O of shape (m, d_v) holds one output per query. Use matrix products, no loops. This is Exercise 3.6d with the keys and values separated from the queries, and with a temperature.
The soft lookup's gradient is not just nonzero, it points somewhere useful. The next exercise uses it to learn a query, which is the whole reason for making the lookup soft.
Learn a query by gradient descent
A soft lookup with dot-product scores at temperature tau returns . We want it to return a target vector, so the loss is . Implement query_step(q, K, V, target, tau=1.0) returning (loss, grad), where grad is of shape (d_k,), computed from the covariance formula above (no finite differences). K is (n, d_k), V is (n, d_v). The tests check your gradient numerically, run gradient descent with it, and confirm that a saturated lookup passes no gradient at all.
Look at the third test. A query that has locked onto the wrong entry at a tiny temperature has a large loss and a zero gradient. Nothing in the loss can move it. That is the hard lookup's failure, recovered as the cold limit of the soft one, and the reason trained attention keeps its scores in a moderate range.
Which similarity? Dot product, distance, or a small network
The softmax does not care where the scores come from. §3.6 compared Bahdanau's additive score with Luong's dot and general scores, and found the dot product cheapest, since a whole batch of scores is one matrix product. One more family is worth adding, because it connects attention to a much older idea, and it shows that the choice matters less than you might think.
Negative squared distance, , is what the rent example used. Expand it,
The term is the same for every , so by shift invariance it disappears inside the softmax. What is left is a dot product (times 2) plus a per-key bias that penalizes long keys. When all keys have the same norm, distance attention and dot-product attention give identical weights. This is the same identity, , that tied cosine to distance in §1.3.
Additive attention, , is more flexible per score but cannot be written as one matrix product. Vaswani et al. (2017) report that the two perform similarly for small and that additive attention wins over unscaled dot products for large , the observation that led them to scale the dot product (§4.3).
| similarity | formula | parameters | batched cost | used by |
|---|---|---|---|---|
| dot product | none | one matmul | Transformers, Luong "dot" | |
| bilinear | two matmuls | Luong "general", and Q K V in disguise (§4.2) | ||
| negative distance | none | one matmul plus norms | kernel regression, RBF networks | |
| additive | about | an MLP per pair | Bahdanau 2015 |
Let's see kernel regression as attention, with three bandwidths.
A small bandwidth (low temperature) chases the noise, since each query copies its nearest neighbors. A large one (high temperature) over-smooths toward the global mean. The middle one is close to the truth. That is the bias-variance trade-off, and it is the same trade-off a network faces when it decides how sharp each attention head should be.
Kernel regression as attention, without NaN
Implement nadaraya_watson(xq, xs, ys, bandwidth) returning predictions of shape (m,) for queries xq of shape (m,), using training points xs, ys of shape (n,) and a Gaussian kernel. Write it as a softmax over scores. The naive version, np.exp(-(xq - xs)**2 / (2 * bw**2)) divided by its sum, returns 0/0 = NaN for a query far from every training point, because every kernel value underflows to zero. Your version must return the value of the nearest training point in that case, which is the correct limit.
Find the temperature that gives a target entropy
Given scores s of shape (n,) (not all equal) and a target entropy H_target strictly between 0 and , return the temperature tau for which , accurate to in entropy. The derivation above proved that entropy increases strictly with , so the answer is unique. Calibrating a temperature to a target entropy is a real technique, used for example to set the per-point bandwidths in t-SNE (§1.6), where the target is stated as a perplexity.
In PyTorch
In PyTorch the soft lookup is three lines, and autograd confirms the covariance formula for the query gradient.
Read more
- Bahdanau, Cho & Bengio (2015). Neural Machine Translation by Jointly Learning to Align and Translate. The paper that introduced soft attention, as an alignment model between a decoder and an encoder.
- Radford et al. (2021). Learning Transferable Visual Models From Natural Language Supervision. CLIP, whose learned softmax temperature starts at 0.07 and is clipped at a logit scale of 100.
- Graves, Wayne & Danihelka (2014). Neural Turing Machines. Content-based addressing of a memory with a softmax over cosine similarities and a learned key strength that acts as an inverse temperature, a differentiable dictionary in its purest form.
- Zhang et al. Dive into Deep Learning, attention pooling. Nadaraya-Watson regression developed step by step into attention.
- Martins & Astudillo (2016). From Softmax to Sparsemax. A sparse alternative to softmax and its Jacobian.
- Ramsauer et al. (2020). Hopfield Networks is All You Need. Attention as the update rule of an associative memory, with temperature as inverse sharpness.