Queries, Keys, and Values

Dive into Deep Learning · §10.1

Queries, keys, and values
attention as soft lookup · softmax weights · Nadaraya–Watson pooling · why learn the kernel

The fixed-size bottleneck

Many networks either assume a fixed input size, such as 224 \times 224 images, or compress a variable-length sequence into a fixed-dimensional RNN state.

A database need not compress its records into one fixed-size state. A database is a set of (\text{key}, \text{value}) pairs; a query retrieves the matching value.

  • The query stays simple no matter how large the database is.
  • The same query gets different answers from different databases.
  • Lookup does not require compressing the database first.

We want a differentiable layer with these properties.

Attention as differentiable lookup

Over a database \mathcal{D} = \{(\mathbf{k}_1, \mathbf{v}_1), \ldots, (\mathbf{k}_m, \mathbf{v}_m)\}:

\textrm{Attention}(\mathbf{q}, \mathcal{D}) = \sum_{i=1}^m \alpha(\mathbf{q}, \mathbf{k}_i)\, \mathbf{v}_i.

One-hot \alpha gives exact lookup; uniform \alpha gives average pooling; other distributions interpolate between them.

Attention pooling: a linear combination of values, with weights from query–key compatibility.

Softmax makes any score a weight

Given a scoring function a(\mathbf{q}, \mathbf{k}), exponentiate and normalize its scores:

\alpha(\mathbf{q}, \mathbf{k}_i) = \frac{\exp(a(\mathbf{q}, \mathbf{k}_i))}{\sum_j \exp(a(\mathbf{q}, \mathbf{k}_j))}.

  • Nonnegative, sums to one — a convex combination of the values.
  • Differentiable; available in every framework.
  • The rest of the chapter is about the choice of a and where \mathbf{q}, \mathbf{k}, \mathbf{v} come from.

Visualizing attention weights

A queries-by-keys heatmap displays the weights. The identity matrix below represents exact lookup:

attention_weights = torch.eye(10).reshape((1, 1, 10, 10))
d2l.show_heatmaps(attention_weights, xlabel='Keys', ylabel='Queries')

A 1964 attention mechanism

Nadaraya–Watson regression is attention pooling with a hand-picked similarity kernel:

f(\mathbf{q}) = \sum_i \mathbf{v}_i \frac{\alpha(\mathbf{q}, \mathbf{k}_i)}{\sum_j \alpha(\mathbf{q}, \mathbf{k}_j)}.

Keys = training inputs, values = labels, query = where to predict. The estimator requires no parameter training and is consistent if the kernel narrows at a suitable rate as data accumulate.

Gaussian, boxcar, constant, and triangular kernels.

Nadaraya–Watson in action

y = 2\sin(x) + x + \epsilon, 40 noisy points. Four lines of code: distances → kernel → normalize over keys → weighted sum of labels.

def nadaraya_watson(x_train, y_train, x_val, sigma):
    dists = x_train[:, None] - x_val[None, :]
    k = torch.exp(-dists**2 / (2 * sigma**2))
    attention_w = k / k.sum(0)  # Normalize over keys for each query
    return y_train @ attention_w, attention_w

sigmas = (0.1, 0.5, 2.0)
estimates = [nadaraya_watson(x_train, y_train, x_val, s)[0] for s in sigmas]
d2l.plot(x_val, estimates + [y_val], 'x', 'y',
         legend=[f'sigma = {s:g}' for s in sigmas] + ['truth'])
d2l.plt.plot(x_train, y_train, 'o', alpha=0.4);

  • In this example, the bandwidth \sigma has more effect than the choice among the displayed kernel shapes.

The weights explain the fits

  • Narrow kernel: sharp, local, noisy — weight on a handful of keys.
  • Wide kernel: smooth, global — weight spread across the dataset.
  • Either way the kernel is chosen, not learned, and every query gets the same notion of similarity.

Recap

  • Attention = differentiable soft database lookup: \sum_i \alpha(\mathbf{q}, \mathbf{k}_i)\, \mathbf{v}_i.
  • Softmax of any scoring function gives valid weights; exact lookup and average pooling are the extreme weight patterns.
  • A fixed set of parameters can operate on databases of different sizes.
  • Nadaraya–Watson uses fixed kernels; the rest of the chapter learns query and key representations instead.