Skip to main content

14  Kernel Regression: Attention Before It Was Learnable

Chapter 13 ended with a machine that could translate, and a remaining architectural limitation: every source fact had to cross a single fixed-size bridge before the decoder wrote its first token. The encoder had computed a state at every source position, but the decoder received only the last one. What if every encoder state were kept instead?

The retained states form a memory bank. At each step, the decoder forms a query, compares it with every stored state, and retrieves a weighted mixture of the relevant content. Each entry is still compressed, but the last one is no longer the only path for source information. This chapter asks how a query should read that bank: with a smooth, similarity-weighted rule, not a nearest-neighbor rule whose selected index changes discretely as the query moves.

We start with three observed pairs. Each stored location is a key \(k_i\), and the response stored with it is a value \(v_i\):

observation key \(k_i\) value \(v_i\)
1 1 1.5
2 3 2.8
3 5 1.8

For a query \(q=3.5\), Figure 14.1 works through one read of this bank. Each key’s distance from the query, \(|q-k_i|\), becomes an affinity: a number between 0 and 1 that equals 1 at distance zero and falls off over a scale set by a bandwidth \(h\). The Gaussian rule, \(\exp(-|q-k_i|^2/2h^2)\), is a bell centered on the query, and a key’s affinity is the height of the bell where the key stands. Dividing the three affinities by their sum gives weights \(\alpha_i\) that are nonnegative and sum to one, \((0.0002, 0.9413, 0.0585)\) at \(h=0.6\), and the weighted mixture of the values, \(\sum_i\alpha_i v_i=2.7412\), is the prediction. The second pair dominates because its key, \(k_2=3\), is nearest the query. The first key stands far out in the bell’s tail and receives negligible weight. The metric and the bandwidth were specified in advance. Nothing about the similarity was learned.

14.1 Linear regression as a weighted combination of targets

Linear regression, the model of Chapter 1, already predicts by mixing the observed targets: its hat matrix writes every fitted value as a weighted combination of the training targets, with coefficients that depend on the inputs alone, and the same algebra gives the coefficients at a new query.

Let the augmented design matrix \(\widetilde{\matr{X}}\in\mathbb{R}^{n\times(d+1)}\) include the intercept column, let \(\widetilde{\vect{q}}\in\mathbb{R}^{d+1}\) be a new query with its leading 1, and let \(\vect{y}\in\mathbb{R}^{n}\) contain the observed targets, this chapter’s values. Assuming \(\widetilde{\matr{X}}\) has full column rank,

\[ \hat{\vect{\beta}} = (\widetilde{\matr{X}}^\top\widetilde{\matr{X}})^{-1} \widetilde{\matr{X}}^\top\vect{y}. \]

The prediction at the query is therefore

\[ \hat f(\vect{q}) = \widetilde{\vect{q}}^\top\hat{\vect{\beta}} = {\underbrace{\left[ \widetilde{\matr{X}} (\widetilde{\matr{X}}^\top\widetilde{\matr{X}})^{-1} \widetilde{\vect{q}} \right]}_{\vect{\lambda}(\vect{q})}}^{\!\top}\vect{y}. \tag{14.1}\]

The second equality substitutes \(\hat{\vect{\beta}}\) and uses the symmetry of \((\widetilde{\matr{X}}^\top\widetilde{\matr{X}})^{-1}\).

WarningTrap: weighted combination does not mean weighted average

The OLS prediction \(\hat f(\vect{q})\) is a weighted combination of the training targets, with the coefficients \(\vect{\lambda}(\vect{q})\) of Equation 14.1 as the weights. They are coefficients on the targets, not distances, similarities or regression parameters, and nothing forces them to be positive. With an intercept, they sum to one because OLS reproduces a constant response. But weighted combination is not the same as weighted average.

For keys \((-2,-1,0,1,2)\) and query \(q=1.5\), the coefficients are

\[ \vect{\lambda}(q)=(-0.10, 0.05, 0.20, 0.35, 0.50). \]

They sum to one, yet the first is negative. If the targets are \((1,0,0,0,0)\), OLS predicts \(-0.10\), outside the observed range \([0,1]\). Linear regression is allowed to leave the observed targets’ convex hull even when the query lies inside the observed key range. The kernel rule of Equation 14.3 keeps the two properties that OLS lacks: nearer evidence receives more weight, and every weight is nonnegative.

14.2 Similarity weights and local prediction

A smoothing kernel assigns a nonnegative affinity to a query and a stored key, taking large values for nearby pairs and small values for distant pairs. To avoid confusion with the key matrix \(\matr{K}\) later, we write it as \(\kappa_h\). The Gaussian choice is

\[ \kappa_h(\vect{q},\vect{k}) = \exp\!\left(-\frac{\norm{\vect{q}-\vect{k}}^2}{2h^2}\right), \qquad h>0. \tag{14.2}\]

This kernel is also called the radial basis function (RBF) kernel: it depends on \(\vect{q}\) and \(\vect{k}\) only through their distance. The bandwidth \(h\) sets the effective scale of the kernel. Smaller values restrict influence to nearby keys, while larger values average over a broader neighborhood.

Call each stored location a key \(\vect{k}_i\) and the response stored with it a value \(\vect{v}_i\). In the opening example both are scalars, and in general both may be vectors. Given the stored pairs \((\vect{k}_i,\vect{v}_i)\), normalize the affinities,

\[ \alpha_i(\vect{q}) = \frac{\kappa_h(\vect{q},\vect{k}_i)} {\sum_{j=1}^{n}\kappa_h(\vect{q},\vect{k}_j)}, \qquad \hat f_h(\vect{q})=\sum_{i=1}^{n}\alpha_i(\vect{q})\,\vect{v}_i. \tag{14.3}\]

This is the Nadaraya–Watson estimator, proposed independently by Nadaraya and Watson in 1964. For the strictly positive Gaussian kernel, each \(\alpha_i>0\) and \(\sum_i\alpha_i=1\). Scalar predictions remain between the smallest and largest observed values: the operator smooths locally and keeps every prediction inside the observed values’ convex hull, which the previous section showed linear regression does not. On the three pairs of the opening at \(h=0.6\), Equation 14.3 is the arithmetic of Figure 14.1.

As \(h\to0^+\), the weight concentrates on the nearest key, approaching the nearest-neighbor rule of the chapter opening, with exact ties splitting the mass. At \(h=0\) the formula is undefined. As \(h\to\infty\), squared distances become negligible relative to \(2h^2\), affinities tend to 1, and weights approach \(1/n\), so predictions approach the mean of the observed responses.

For a query \(\vect{q}\), consider fitting one constant vector to the stored values, weighted by their keys’ similarity to the query.

WarningTrap: the kernel supplies the weights; the constant fit supplies the average

The weighted read \(\sum_i\alpha_i\vect{v}_i\) is the solution of a locally weighted constant-regression problem. It is not the solution of unrestricted global interpolation: an unrestricted function could interpolate the stored values at the keys and take any value at a new query. For a fixed query \(\vect{q}\), fit one constant vector \(\vect{c}\):

\[ \vect{c}^{*}(\vect{q}) =\arg\min_{\vect{c}} \frac{1}{2}\sum_i \kappa(\vect{q},\vect{k}_i) \norm{\vect{c}-\vect{v}_i}^{2}. \tag{14.4}\]

Setting its gradient to zero gives \(\sum_i\kappa_i(\vect{c}^{*}-\vect{v}_i)=\vect{0}\), with \(\kappa_i=\kappa(\vect{q},\vect{k}_i)\), so \(\vect{c}^{*}=\sum_i\kappa_i\vect{v}_i/\sum_i\kappa_i\). With \(\kappa(\vect{q},\vect{k})=\exp(\vect{q}^{\top}\vect{k}/\sqrt d)\), where \(d\) is the key dimension, that is the scaled dot-product attention of Chapter 16. The kernel supplies nonnegative weights, and the local-constant model is what makes their solution an average. After self-attention, Chapter 17 returns to Equation 14.4 and changes its kernel, its predictor class or its update rule, which gives the memory layers of several recent sequence models.

Here is the whole operator in NumPy. The shapes come before the multiplication: queries are \((m,d)\), keys are \((n,d)\), values are \((n,p)\), and the returned mixture is \((m,p)\).

  1. Fix the seed and set NumPy to print four decimals without scientific notation.
  2. Define the reusable helpers: row_softmax and gaussian_attention.
  3. Fixed Gaussian attention, from scores to mixtures.
from __future__ import annotations

import matplotlib.pyplot as plt
import numpy as np
import torch
torch.set_num_threads(4)  # pinned: results depend on the CPU thread count

# [1]
torch.manual_seed(6050)
np.set_printoptions(precision=4, suppress=True)

# [2]
def row_softmax(scores: np.ndarray) -> np.ndarray:
    """Normalize each row after a stability-preserving shift."""
    shifted = scores - scores.max(axis=1, keepdims=True)
    weights = np.exp(shifted)
    return weights / weights.sum(axis=1, keepdims=True)

def gaussian_attention(
    queries: np.ndarray,
    keys: np.ndarray,
    values: np.ndarray,
    bandwidth: float,
) -> tuple[np.ndarray, np.ndarray]:
    """Fixed Gaussian attention for Q:(m,d), K:(n,d), V:(n,p)."""
    if not np.isfinite(bandwidth) or bandwidth <= 0:
        raise ValueError("bandwidth must be positive and finite")
    if queries.ndim != 2 or keys.ndim != 2 or values.ndim != 2:
        raise ValueError("queries, keys, and values must be matrices")
    if queries.shape[1] != keys.shape[1] or keys.shape[0] != values.shape[0]:
        raise ValueError("Q/K feature dimensions and K/V row counts must agree")

    distances2 = ((queries[:, None, :] - keys[None, :, :]) ** 2).sum(axis=2)
    log_scores = -distances2 / (2.0 * bandwidth**2)  # (m, n)
    weights = row_softmax(log_scores)                # (m, n)
    return weights @ values, weights                 # (m, p), (m, n)

keys3 = np.array([[1.0], [3.0], [5.0]])
values3 = np.array([[1.5], [2.8], [1.8]])
query3 = np.array([[3.5]])
pred3, weights3 = gaussian_attention(query3, keys3, values3, bandwidth=0.6)
# [3]
print("weights:", weights3[0])
print(f"prediction: {pred3.item():.4f}")
weights: [0.0002 0.9413 0.0585]
prediction: 2.7412
A query star connects to three observed points with line widths proportional to their influence. In the adjacent bar chart, the nearby middle key receives most of the normalized weight.
Figure 14.1: A fixed Gaussian lookup. The bars give the normalized weights, and the connecting lines show which observations feed the query. The query lands close to the second location, so its response dominates the mixture; no similarity function was learned.
Watch the mechanism: distance becomes influence

As the query moves, which observation gains influence?

Three observations, one fixed bandwidth. Follow the calculation above, then move only the query.

● Observed value◆ Prediction Query line Distance bracket
Fixed keys at 1, 3, and 5, with values 1.5, 2.8, and 1.8
Query q = 3.50Bandwidth h = 0.60 (fixed)

Key 1 · value 1.5

Distance
2.50
Affinity
0.00017
Weight
0.0002
Weight × value
0.0003

Key 3 · value 2.8

Distance
0.50
Affinity
0.70665
Weight
0.9413
Weight × value
2.6355

Key 5 · value 1.8

Distance
1.50
Affinity
0.04394
Weight
0.0585
Weight × value
0.1053

Affinity = exp(−distance² / (2h²)); weight = affinity / sum of affinities.

Weights sum to 1.0000.

Prediction = 2.7412, within the observed-value range [1.5, 2.8].

The nearby key at 3 receives most of the influence. The same normalized weights multiply the observed values and add to one prediction.

The calculation is readable without playback. Opening this panel loads the optional controls.

Only the query moves. These weights are computed, not learned; the observations and bandwidth stay fixed. Nonnegative weights summing to one keep the prediction within the observed-value range.

Check yourself. Move the query onto the first observation, q = 1. Which observation dominates, and what is the prediction?

The first one: its weight is about 0.996, so the prediction is about 1.505 — almost its own value, 1.5. With a bandwidth this narrow, a query sitting on a key is nearly a nearest-neighbour lookup.

Transcript and keyboard help
  1. Keep keys 1, 3, 5 and their observed values 1.5, 2.8, 1.8 fixed. At q = 3.5, the distances are 2.5, 0.5, 1.5.
  2. With bandwidth h = 0.6, the Gaussian affinities are approximately 0.00017, 0.70665, 0.04394. Divide each by their common sum, approximately 0.75076.
  3. The normalized weights are approximately 0.0002, 0.9413, 0.0585. Multiply each by its observed value and add: the prediction is approximately 2.7412. Calculations use unrounded weights.
  4. Now move the query across the same observations. A closer key generally gains influence relative to farther keys. Weights still sum to one; the prediction stays between 1.5 and 2.8. Return to q = 3.5, the printed witness.

Silent; paused initially; 1.5× playback. Focus the animation pane: Space or K plays/pauses, arrows seek, Home/End jump, Escape pauses. The scrubber and speed menu also work by keyboard. Reduced-motion mode uses discrete query positions.

The same code compares the signed OLS coefficients with the nonnegative, normalized kernel weights:

  1. Set five keys from -2 to 2 and the query 1.5, and add an intercept column to each.
  2. Signed OLS coefficients against nonnegative kernel weights.
  3. Print the OLS coefficients and kernel weights with their sums, and both predictions for targets (1, 0, 0, 0, 0).
# [1]
ols_keys = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
ols_query = 1.5
design = np.column_stack([np.ones_like(ols_keys), ols_keys])
query_design = np.array([1.0, ols_query])
# [2]
ols_weights = query_design @ np.linalg.solve(design.T @ design, design.T)

witness_values = np.array([[1.0], [0.0], [0.0], [0.0], [0.0]])
nw_pred, nw_weights = gaussian_attention(
    np.array([[ols_query]]), ols_keys[:, None], witness_values, bandwidth=1.0
)
# [3]
print("OLS weights:", ols_weights, "sum:", ols_weights.sum())
print("NW  weights:", nw_weights[0], "sum:", nw_weights.sum())
print(f"witness predictions: OLS {ols_weights @ witness_values[:, 0]:.4f}, "
      f"NW {nw_pred.item():.4f}; target range [0, 1]")
OLS weights: [-0.1   0.05  0.2   0.35  0.5 ] sum: 1.0
NW  weights: [0.001  0.0206 0.152  0.4132 0.4132] sum: 1.0
witness predictions: OLS -0.1000, NW 0.0010; target range [0, 1]
NoteWhich kernels make a convex average?

Any kernel with nonnegative values makes Equation 14.3 a convex average, provided at least one affinity at the query is positive so that the denominator is not zero. The Gaussian is positive everywhere. A kernel that is zero beyond a cut-off distance (the triangular and Epanechnikov kernels of classical smoothing) gives far keys weight exactly zero and is undefined at a query beyond every cut-off. Positive semidefiniteness, the property kernel methods require of their inner-product kernels, is a different condition and is not needed here. The word kernel also names the convolution filters of Chapter 8. Chapter 15 returns to the distinction.

14.3 Softmax normalization of similarity scores

For a strictly positive kernel, taking the logarithm of each affinity and applying the softmax of Chapter 2 yields the normalized weights:

\[ \alpha_i(\vect{q}) = \frac{\kappa_h(\vect{q},\vect{k}_i)} {\sum_j\kappa_h(\vect{q},\vect{k}_j)} = \operatorname{softmax}_i\!\left( \log\kappa_h(\vect{q},\vect{k}_i)\right). \tag{14.5}\]

The score \(s_i\) is the log-kernel, \(\kappa_h(\vect{q},\vect{k}_i)=\exp(s_i)\). With the Gaussian,

\[ s_i(\vect{q}) = -\frac{\norm{\vect{q}-\vect{k}_i}^2}{2h^2}, \]

a scaled negative squared distance: the score is not Gaussian, its exponential is. Written this way, the bandwidth is a softmax temperature,

\[ \alpha_i(\vect{q}) = \operatorname{softmax}_i\!\left(-\frac{\norm{\vect{q}-\vect{k}_i}^2}{\tau}\right), \qquad \tau=2h^2, \tag{14.6}\]

so the narrowing and broadening of the kernel in the previous section is the sharpening and flattening of a softmax. The 2 in \(\tau\) belongs to the convention that the raw score is the full squared distance. Halving the raw score gives \(\tau=h^2\).

The implementation works with scores throughout: it computes \(s_i\), subtracts the largest score in each row, and only then exponentiates, so \(\kappa_h\) itself is never formed. That is the numerically stable softmax of Chapter 2, applied to kernel scores.

14.4 Bandwidth: bias versus variance

A narrow bandwidth tracks local noise and inflates estimator variance. An overly broad bandwidth smooths away genuine curvature and introduces approximation bias. Chapter 1 called this bias versus variance. Here \(h\) controls the trade-off.

Since we generate the data ourselves, we can do what observed data never permits. The keys are 60 fixed locations \(x_i\). The values are noisy responses \(y_i\), redrawn 1,000 times to give 1,000 noisy worlds that share the keys. Every prediction is compared with the known, noiseless function

\[ f(x)=\sin(1.5x)+0.3\cos(4x), \qquad y_i=f(x_i)+\epsilon_i, \qquad \epsilon_i\sim\mathcal{N}(0,0.25^2). \]

We generate the key locations as seeded uniform draws on \([-3,3]\) and evaluate the predictions at 241 fixed query locations on \([-2.9,2.9]\), none of which coincides with a key, so that every prediction is a read between stored points rather than a copy of one stored value. Holding the design fixed lets us separate squared bias from variance over independent noise draws. The population identity

\[ \operatorname{MSE}=\operatorname{bias}^2+\operatorname{variance} \]

has no irreducible-noise term because the comparison target is \(f(q)\), the noiseless function value at the query, not a fresh noisy response. Predicting a fresh noisy response would add the irreducible variance \(0.25^2\) to every MSE that the sweep prints. In our finite 1,000-world ensemble, the analogous empirical decomposition holds up to floating-point roundoff.

The weights depend only on query–key geometry and bandwidth, so kernel regression is a linear smoother: for each bandwidth, gaussian_attention forms one \(241\times60\) matrix \(\matr{A}\) that yields every world’s predictions. At a query \(q\), let \(\hat f^{(w)}(q)\) be world \(w\)’s prediction and \(\bar f(q)\) the mean prediction across all 1,000. The empirical squared bias, variance and MSE at \(q\) over these 1,000 worlds are

\[ \begin{gathered} \operatorname{bias}^2(q)=\bigl(\bar f(q)-f(q)\bigr)^2,\qquad \operatorname{variance}(q)=\frac{1}{1000}\sum_{w=1}^{1000}\bigl(\hat f^{(w)}(q)-\bar f(q)\bigr)^2,\\ \operatorname{MSE}(q)=\frac{1}{1000}\sum_{w=1}^{1000}\bigl(\hat f^{(w)}(q)-f(q)\bigr)^2. \end{gathered} \]

We then average all three over the 241 queries.

  1. Define the reusable truth helper.
  2. Draw 60 sorted keys, one noisy sample, and 1,000 noise redraws, and fix 241 queries and eight bandwidths.
  3. Fixed-design bias/variance sweep over 1,000 noisy worlds.
  4. Print each bandwidth’s 2h^2, squared bias, variance, and MSE, then the one with the lowest MSE.
# [1]
def truth(x: np.ndarray) -> np.ndarray:
    return np.sin(1.5 * x) + 0.3 * np.cos(4.0 * x)

# [2]
data_rng = np.random.default_rng(605012)
keys = np.sort(data_rng.uniform(-3.0, 3.0, size=60))
observed_rng = np.random.default_rng(605013)
observed_values = truth(keys) + observed_rng.normal(0.0, 0.25, size=keys.size)
queries = np.linspace(-2.9, 2.9, 241)

mc_rng = np.random.default_rng(605014)
responses = truth(keys)[None, :] + mc_rng.normal(
    0.0, 0.25, size=(1_000, keys.size)
)
bandwidths = np.array([0.05, 0.08, 0.12, 0.18, 0.28, 0.42, 0.65, 1.00])

rows = []
# [3]
for bandwidth in bandwidths:
    _, weights = gaussian_attention(
        queries[:, None], keys[:, None], observed_values[:, None], bandwidth
    )
    predictions = responses @ weights.T                 # (world, query)
    mean_prediction = predictions.mean(axis=0)
    squared_bias = np.mean((mean_prediction - truth(queries)) ** 2)
    variance = np.mean(np.var(predictions, axis=0))
    mse = np.mean((predictions - truth(queries)[None, :]) ** 2)
    rows.append((bandwidth, 2 * bandwidth**2, squared_bias, variance, mse))

# [4]
print(" h     2h^2    bias^2  variance     MSE")
for row in rows:
    print(f"{row[0]:.2f}   {row[1]:.4f}   {row[2]:.4f}   "
          f"{row[3]:.4f}   {row[4]:.4f}")
best = rows[int(np.argmin([row[-1] for row in rows]))]
print(f"grid minimum: h={best[0]:.2f}, MSE={best[-1]:.4f}")
 h     2h^2    bias^2  variance     MSE
0.05   0.0050   0.0119   0.0343   0.0462
0.08   0.0128   0.0101   0.0247   0.0348
0.12   0.0288   0.0090   0.0169   0.0259
0.18   0.0648   0.0127   0.0109   0.0236
0.28   0.1568   0.0304   0.0069   0.0373
0.42   0.3528   0.0747   0.0047   0.0794
0.65   0.8450   0.1721   0.0031   0.1753
1.00   2.0000   0.3142   0.0022   0.3164
grid minimum: h=0.18, MSE=0.0236

The variance falls at every step, from \(0.0343\) at \(h=0.05\) to \(0.0022\) at \(h=1.00\). Squared bias is not perfectly monotone at the sharp end of this sparse, random design, reaching a minimum of \(0.0090\) at \(h=0.12\) before rising to \(0.3142\) at \(h=1.00\). Their sum is the MSE. On this predeclared grid, \(h=0.18\) has the smallest MSE, \(0.0236\).

One separate noisy sample, in Figure 14.2, shows what those numbers mean:

Noisy observations surround a smooth black truth curve. The narrow-bandwidth fit wiggles around local noise, the middle fit follows the truth most closely, and the broad fit smooths away smaller oscillations.
Figure 14.2: One fixed noisy sample under three bandwidths. The narrow rule follows local noise, the middle rule tracks the signal, and the broad rule erases the function’s smaller-scale structure. The blue bandwidth is the grid minimum in the 1,000-world audit.
TipSelecting \(h\) when the truth is hidden

In empirical applications where the true function is unknown, choose the bandwidth using a validation set, then report once on a sealed test set. Training error alone will usually reward an extremely narrow kernel for copying each noisy target back to itself. The generalization protocol from Chapter 6 still applies even when nothing is trained by gradient descent.

14.5 From one query to an attention matrix

Now write the operator for many queries at once, in the vocabulary of Part IV. Let

\[ \matr{Q}\in\mathbb{R}^{m\times d},\qquad \matr{K}\in\mathbb{R}^{n\times d},\qquad \matr{V}\in\mathbb{R}^{n\times p} \]

hold \(m\) queries, \(n\) keys, and one \(p\)-dimensional value per key. The score and weight matrices are

\[ S_{ri}=-\frac{\norm{\vect{q}_r-\vect{k}_i}^2}{2h^2}, \qquad \matr{A}=\operatorname{rowsoftmax}(\matr{S}) \in\mathbb{R}^{m\times n}. \tag{14.7}\]

Every row of \(\matr{A}\) sums to one, but columns need not. The pooled outputs are one matrix multiplication,

\[ \operatorname{Attention}_h(\matr{Q},\matr{K},\matr{V}) =\matr{A}\matr{V}\in\mathbb{R}^{m\times p}. \tag{14.8}\]

\(\matr{A}\) is an attention-weight matrix: row \(r\) holds the weights that query \(r\) places on the \(n\) keys. For given queries and keys, Euclidean distance and the chosen bandwidth determine every entry. The weights are computed from these inputs, not fitted as separate parameters.

  1. Form the first fixed attention map and audit its shapes and row sums.
# [1]
map_queries = np.linspace(-2.9, 2.9, 31)
map_predictions, attention_map = gaussian_attention(
    map_queries[:, None], keys[:, None], observed_values[:, None], best[0]
)
print(f"Q {map_queries[:, None].shape}, K {keys[:, None].shape}, "
      f"V {observed_values[:, None].shape}, A {attention_map.shape}, "
      f"AV {map_predictions.shape}")
print("maximum row-sum error:",
      f"{np.max(np.abs(attention_map.sum(axis=1) - 1)):.2e}")
Q (31, 1), K (60, 1), V (60, 1), A (31, 60), AV (31, 1)
maximum row-sum error: 2.22e-16
The left panel shows variance falling as bandwidth grows, bias eventually rising, and their summed error reaching an interior minimum. The right heatmap has a narrow bright diagonal band where each query assigns weight to nearby sorted keys.
Figure 14.3: Left: the fixed-design bias–variance audit. Right: the same Gaussian rule written as a 31-query by 60-key attention matrix at \(h=0.18\). Bright cells receive more of a row’s unit mass. The columns are the 60 keys in sorted order, so equal horizontal steps are index steps, not equal steps in \(k\), and the white trace marks where \(k_i=q\). The map is local and interpretable, but specified, not learned.

Each query row is a probability distribution over the keys, concentrated near the query. Where keys are dense, weight is divided among several neighbors. Where keys are sparse, one or two keys carry most of a row’s mass. The width of the band is the temperature \(\tau=2h^2\) of Equation 14.6 made visible.

There is one numerical trap. A Gaussian is mathematically positive everywhere, but a distant query and small bandwidth can make every directly exponentiated kernel value underflow to zero. Normalizing those zeros produces 0/0. The log-score version still has a largest entry, so the shifted softmax remains finite. Appendix C collects this with the other stable algorithms.

  1. Weight the 60 keys by normalizing the kernel and by row_softmax of its log, for queries 0.37 and 1,000.
  2. Verify the log-kernel identity and survive underflow.
# [1]
identity_query, identity_h = 0.37, 0.28
log_kernel = -0.5 * ((identity_query - keys) / identity_h) ** 2
direct_kernel = np.exp(log_kernel)
direct_weights = direct_kernel / direct_kernel.sum()
softmax_weights = row_softmax(log_kernel[None, :])[0]

far_log_kernel = -0.5 * ((1_000.0 - keys) / 0.1) ** 2
far_kernel = np.exp(far_log_kernel)
with np.errstate(divide="ignore", invalid="ignore"):
    naive_far_weights = far_kernel / far_kernel.sum()
stable_far_weights = row_softmax(far_log_kernel[None, :])[0]

# [2]
print("max |normalize(K)-softmax(log K)|:",
      f"{np.max(np.abs(direct_weights - softmax_weights)):.2e}")
print(f"far query: naive denominator={far_kernel.sum():.1f}, "
      f"all finite={np.all(np.isfinite(naive_far_weights))}")
print(f"stable: sum={stable_far_weights.sum():.1f}, "
      f"nearest key={keys[np.argmax(stable_far_weights)]:.4f}")
max |normalize(K)-softmax(log K)|: 2.78e-17
far query: naive denominator=0.0, all finite=False
stable: sum=1.0, nearest key=2.9886
WarningTrap: similarity weights are not uncertainty probabilities

Each row of \(\matr{A}\) is a probability distribution over the key indices. Its weights are not calibrated probabilities that the corresponding key is “correct,” and a concentrated row can reflect a small bandwidth rather than statistical confidence.

14.6 Return the fixed operator to the memory bank

Each of the five roles of the fixed operator has a counterpart in a memory bank of encoder states from Chapter 13, and the table below lists them:

role kernel regression sequence memory preview
query location \(\vect{q}\) where we need an answer what the decoder needs now
keys stored coordinates \(\vect{k}_i\) descriptors used to match encoder memory slots
values observed responses \(\vect{v}_i\) content retrieved from those slots
weights normalized fixed similarities \(\alpha_i(\vect{q})\) how much each slot contributes
output \(\sum_i\alpha_i\vect{v}_i\) a query-specific context vector

The opening example fills every kernel-regression role: the query is the location \(q=3.5\), the keys are the stored locations, the values their responses, the weights the normalized Gaussian affinities of Figure 14.1, and the output their mixture.

Queries specify what is needed, keys provide what to compare against, and values provide what to retrieve. In the simplest sequence model, the same encoder state can serve as both key and value. Our one-dimensional example keeps the two apart: it matches on the location and reads the response. Later, learned projections will let keys and values carry different representations.

Chapter 1 also used one more similarity primitive, the dot product:

\[ \vect{q}^\top\vect{k} =\norm{\vect{q}}\,\norm{\vect{k}}\cos\theta. \]

It is pure angular similarity only when the norms are controlled. If \(\vect{q}=(1,0)\), \(\vect{k}_1=(1,0)\), and \(\vect{k}_2=(2,0)\), both keys point in the same direction as the query, but their dot products are 1 and 2. Vector magnitude therefore contributes to the score alongside orientation, and the score itself defines what counts as similar.

In this chapter, the Gaussian smoother defines the score as a fixed negative squared Euclidean distance, with \(h\) selected on a predeclared grid rather than optimized by gradient descent. The operator stores data, computes a fixed map, and pools values. It takes the form of attention (a normalized, similarity-weighted average of values) without a learned compatibility function.

At the end of Chapter 8 we had a fixed image kernel and asked what would happen if the filter became learnable. Chapter 9 answered with a CNN. Our similarity rule is fixed in the same way:

TipWhat if the similarity itself were learnable?

The query, keys, values, scores, row-wise softmax, and weighted pooling establish the full dataflow of attention. The compatibility function is the next component to make learnable. Chapter 15 learns the score and puts the sequence-memory roles of the table above to work on the date task of Chapter 13: when the decoder is about to write the year, its state supplies the query and the encoder states supply the keys and the values, and the chapter measures how strongly the weights concentrate on the four year characters at the end of the source.

NoteCheck yourself

Close the book for one minute and reconstruct the weighted prediction.

  • How do scores become nonnegative weights that sum to one?
  • What does bandwidth change in a kernel smoother?
  • Which component is still hand designed at the end of this chapter?

14.7 Okay, so: attention is normalized memory mixing

  1. The linear prediction of Chapter 1 already mixed training targets. OLS gives \(\hat f(\vect{q})=\vect{\lambda}(\vect{q})^\top\vect{y}\), but its coefficients on the targets may be signed, so the prediction can leave the targets’ convex hull.
  2. Nadaraya–Watson is a convex mixture, and a local one. Nonnegative affinities and normalization make the read a convex mixture. A kernel that decays with distance makes it local.
  3. Bandwidth and temperature are one dial. For Gaussian distance scores \(\tau=2h^2\), and it balances variance against bias: in the fixed-design sweep over the 1,000 noisy worlds, the lowest MSE on the grid was \(0.0236\), at \(h=0.18\).
  4. The softmax of Chapter 2 is the normalization. Positive-kernel weights equal softmax(log kernel). Computing log-scores directly and shifting each row avoids the distant-query 0/0 trap.
  5. Fixed attention is \(\matr{A}\matr{V}\). Queries score keys, row-softmax produces an \(m\times n\) attention-weight matrix, and that matrix pools the values. The algebra is complete, with only the score still hand designed.
NoteConnection to test-time regression

Work from 2024–26 uses a forward pass as test-time regression: the prefix supplies key–value training pairs, the current token supplies a query, and a memory operator fits or updates a predictor before answering. Test-Time Training, the older Delta-style fast weights, Titans, and MesaNet differ in which part they change: the learned views of a token, the weighting of its history, the model or regularizer, or the online solver. Wang and colleagues’ Beyond Test-Time Memory changes the objective itself, from regression to finite-horizon optimal control.

That shared regression perspective is a design language, not a claim that the architectures are equivalent. Softmax attention performs a query-dependent local fit while retaining its key–value rows. Other methods compress the history into a fixed-size parametric state and no longer retain those rows. Chapter 17 develops this regression perspective by varying the kernel, predictor class, and update rule, then comparing the information retained and the computation required.

Sources and further reading

Exercises

  1. (Pencil.) Prove that Equation 14.3 is a convex combination when \(\kappa_h\ge0\) and its denominator is positive. Then prove that a scalar prediction lies in \([\min_i v_i,\max_i v_i]\). Which step fails for the OLS coefficients in Equation 14.1?
  2. (Pencil.) For keys \((-2,-1,0,1,2)\), derive the five OLS target coefficients at \(q=1.5\) without using code. Verify that they sum to one, identify the negative coefficient, and construct a second target vector for which OLS leaves the target range.
  3. (Code.) Split a fresh noisy sample from this chapter’s function into training, validation, and sealed test sets. Select \(h\) on validation data, report the test MSE once, and compare your selected curve with the synthetic-truth grid minimum above. Explain why the two procedures need not select the same bandwidth.
  4. (Pencil.) (a) Derive Equation 14.6. (Code.) (b) Use keys \([0,0,1]\), query 0, and successively smaller positive bandwidths. What weights does stable softmax assign to the exact nearest-key tie, and why is \(h=0\) still invalid?
  5. (Code.) Apply gaussian_attention to two-dimensional keys whose coordinates have disparate units. Compare the attention map before and after standardizing each feature using training-set statistics. What does this reveal about the metric hidden inside a fixed kernel?
  6. (Pencil.) Differentiate Equation 14.4 with respect to the vector \(\vect{c}\), set the gradient to zero and solve for \(\vect{c}\), then substitute \(\kappa(\vect{q},\vect{k}_i)=\exp(\vect{q}^{\top}\vect{k}_i/\sqrt d)\). Why would the stronger claim that an unrestricted function fitted with no regularizer must average be false?
  7. (Code.) Return to Figure 14.1 and parameterize a positive inverse bandwidth as \(w=\operatorname{softplus}(\rho)\) in the score \(-w(q-k_i)^2\). Fit the scalar \(\rho\) by SGD on training queries, choose the stopping point on held-out queries, and evaluate once on a sealed test grid. Then replace \(w\) by one positive \(w_i\) per key and repeat under matched splits, updates, and seed. Plot the learned weights and train/validation errors. Common misconception: “the lower-training-loss bandwidth is the better estimator.” The sharp limit can interpolate the stored targets while degrading held-out prediction.