Skip to main content

15  Attention: Making the Kernel Learnable

Chapter 14 assembled the mechanics of reading a memory bank: compare a query to stored keys, normalize the scores with row-wise softmax, and retrieve a weighted blend of values. But its scoring rule was rigid. Euclidean distance treated all coordinates equally, and validation tuned only a single scalar bandwidth \(h\), which scaled space uniformly, with no way to learn which features matter. With a fixed score, only the input representation could adapt.

To break the sequence-to-sequence bottleneck of Chapter 13, the decoder must query the encoder’s retained hidden states. In high dimensions, however, fixed Euclidean distance is brittle. The decoder needs a similarity function that can learn what constitutes a match for the task. This chapter gives the score its own parameters.

Contrast this soft read with the hard lookup of an embedding table, which selects one row by index. Selecting an encoder state by \(\operatorname{argmax}\) would be piecewise constant in the scores, leaving candidates that nearly matched with zero gradient. Instead, a continuous softmax distribution over all keys lets the gradient of the training loss reach the scoring parameters. Replacing fixed geometric distance with learned projections yields attention (Figure 15.1): a content-dependent, fully differentiable read whose geometry adapts to the data.

A query box on the left sends arrows of different thickness to four key rows; arrows to keys display weights 0.62, 0.23, 0.10, and 0.05; each key row pairs with a value row whose shading matches the arrow weight; all values merge into a single output box labeled weighted blend of all rows.
Figure 15.1: The lookup table, softened. A hard lookup would follow one arrow to one row. Attention scores the query against every key, softmax turns the scores into row weights (arrow thickness; the numbers here are illustrative), and the output is the weighted blend of all values. The weighted read is differentiable with respect to the scores, so the loss can train the scoring rule.

The result is visible in the date task before we name all its parts. For the fixed validation source

may 17, 1971  ->  1971-05-17

the trained decoder emits the year first, even though those four characters reside at the end of the source string. Across the fixed 400-example validation audit, the first four attention rows place 97.5% of their probability mass on the contextual encoder states corresponding to the source-year characters. In the example above, emitting the second character (9) allocates a maximum weight of 0.856 to the encoder state at the source position of 9, and emitting 7 assigns a peak weight of 0.985 to the encoder state at the 7 in the year. These soft alignments emerge without explicit supervision. The cross-entropy loss on the target characters alone determines where the weights concentrate.

15.1 The soft lookup and cross-attention

Let the encoder retain its full memory

\[ \matr{H}=[\vect{h}_1,\ldots,\vect{h}_n]^\top \in\mathbb{R}^{n\times d_e}. \]

At decoder step \(t\), use the previous decoder hidden state \(\vect{s}_{t-1}\in\mathbb{R}^{d_d}\) as the query. A learned compatibility function \(a_\theta\) produces one score per source position:

\[ e_{t,i}=a_\theta(\vect{s}_{t-1},\vect{h}_i). \]

To exclude padding in variable-length sequences, let \(M_i=0\) at valid source positions and \(M_i=-\infty\) at padded positions. Then

\[ \alpha_{t,i} =\operatorname{softmax}_i(e_{t,i}+M_i), \qquad \vect{c}_t=\sum_{i=1}^{n}\alpha_{t,i}\vect{h}_i. \tag{15.1}\]

This preserves the familiar \(\matr{A}\matr{V}\) read of Chapter 14, retaining the softmax normalization while replacing the rigid metric with a learned scoring rule. Backpropagation can thus deliver gradients to the scoring parameters through every valid memory slot. We have softened the hard lookup.

Because the query originates in the decoder while the memory resides in the encoder, this operation is cross-attention: an interface bridging two distinct sequences. The encoder’s final state still initializes the decoder, but that initial handoff is no longer the only path from source to output. By supplying every decoding step with its own adaptive context vector \(\vect{c}_t\), the architecture removes the fixed-size state bottleneck. Chapter 14 built the memory bank, and cross-attention now learns the rule that reads it.

In Chapter 9, stacked convolutions expanded receptive fields gradually through depth, leaving a dense head to combine features across the image. By contrast, a single cross-attention read scores every encoder position directly in one step, at the cost of evaluating all query–key pairs. For now, however, the representations it queries still originate from an RNN, which computes them sequentially, one step at a time.

15.2 Additive attention: learn the comparison itself

The most direct way to make similarity learnable is to parameterize the compatibility function with a shallow neural network. With the decoder query and the encoder key both projected into a shared alignment space of width \(d_a\), the additive score is

\[ e_{t,i} =\vect{v}_a^\top \tanh\!\left( \matr{W}_q\vect{s}_{t-1} +\matr{W}_k\vect{h}_i \right), \tag{15.2}\]

where

\[ \matr{W}_q\in\mathbb{R}^{d_a\times d_d}, \qquad \matr{W}_k\in\mathbb{R}^{d_a\times d_e}, \qquad \vect{v}_a\in\mathbb{R}^{d_a}. \]

The two projections put vectors of different native widths, \(d_d\) and \(d_e\), into a common alignment space. The \(\tanh\) nonlinearity extracts joint match features, while \(\vect{v}_a\) weights their individual contributions to produce the scalar score. These parameters are shared across all decoder steps and source positions: as the sliding filter of Chapter 9 applied one learned template across an entire grid, attention evaluates a single compatibility rule across all decoder–source pairs.

In batched execution, inserting a singleton sequence axis into the query tensor \(\matr{S}_{t-1}\in\mathbb{R}^{B\times1\times d_d}\) allows it to broadcast across all \(n\) encoder positions in \(\matr{H}\):

\[ \matr{S}_{t-1}\matr{W}_q^\top \in \mathbb{R}^{B\times 1\times d_a}, \qquad \matr{H}\matr{W}_k^\top \in \mathbb{R}^{B\times n\times d_a}. \]

Passing their broadcast sum through \(\tanh\) forms joint match features of shape \((B,n,d_a)\), which \(\vect{v}_a\) contracts into a \((B,n)\) score matrix. Normalizing across the source axis via softmax produces a valid probability distribution over the \(n\) positions, and the resulting weighted read over \(\matr{H}\) yields a \((B,d_e)\) context, one \(d_e\)-dimensional row for each sequence in the batch.

We fix one parameter setting to trace the arithmetic directly. With identity projections and an all-ones selector, the scores of three source states evaluate to \((1.015,1.562,1.124)\), which softmax converts into the weights \((0.260,0.450,0.290)\):

  1. Compute the three-key additive scores and softmax weights.
from __future__ import annotations

import math
import random

import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
torch.set_num_threads(4)  # pinned: results depend on the CPU thread count

# [1]
torch.manual_seed(6050)

query_snapshot = np.array([0.5, 0.5, 0.2, -0.1])
key_snapshot = np.array([
    [-0.3, 0.1, 0.2, 0.0],
    [0.6, 0.5, 0.1, -0.2],
    [0.0, -0.4, 0.3, 0.2],
])
snapshot_scores = np.tanh(query_snapshot + key_snapshot).sum(axis=1)
snapshot_weights = np.exp(snapshot_scores - snapshot_scores.max())
snapshot_weights /= snapshot_weights.sum()
A query branch and key branch pass through separate projections, merge, then pass through tanh and a learned selector to produce one score. The adjacent three-bar chart gives the middle key the largest softmax weight.
Figure 15.2: Additive attention learns a compatibility function. Query and key enter separate projections and interact through \(\tanh\), and a learned selector reduces the result to one score. The right panel fixes one parameter setting to show the resulting scores-to-weights calculation as an explicit arithmetic example rather than a trained result.
Note“Learned kernel” is an analogy

Exponentiating a learned score and normalizing it reproduces the Nadaraya–Watson algebra. Yet this does not make the scoring function a kernel in the positive-semidefinite sense of kernel methods. Separate query and key projections leave \(a_\theta(\vect{q},\vect{k})\) neither symmetric nor positive semidefinite. What survives is the operational pattern: compare \(\rightarrow\) normalize \(\rightarrow\) mix.

15.3 Scaled dot product: from additive alignment to matrix products

Vectorizing additive attention over all \(m\) decoder queries at once forms a \((B,m,n,d_a)\) intermediate tensor, applies an elementwise tanh, and reduces it to scalar scores. Scoring each query–key pair by its dot product avoids that intermediate tensor and turns all comparisons into a single matrix multiplication. As in Chapter 1, where the dot product measured similarity against a template, each key now acts as a template in a space learned during training.

To see how one query goes through the computation, compare a four-dimensional query against three keys:

\[ \vect{q}=(0.9,\,-0.3,\,0.2,\,0.6), \qquad \begin{aligned} \vect{k}_1&=(0.8,\,-0.2,\,0.1,\,0.5),\\ \vect{k}_2&=(-0.5,\,0.9,\,0.0,\,0.2),\\ \vect{k}_3&=(0.3,\,0.0,\,0.6,\,-0.4). \end{aligned} \]

Because \(\vect{k}_1\) matches the sign pattern of \(\vect{q}\) while \(\vect{k}_2\) opposes its largest coordinates, the raw dot products \(\vect{q}^\top\vect{k}_j\) are \((1.10,-0.60,0.15)\). Dividing by \(\sqrt{d_k}=\sqrt{4}=2\) tempers these scores to \((0.55,-0.30,0.075)\), and softmax converts them into the attention weights \((0.488,0.209,0.303)\), placing nearly half the mass on the first key:

  1. Set a four-dimensional query and three keys, then compute raw_walk, scaled_walk, and weight_walk.
  2. Run one query through dot product, scaling, and softmax.
# [1]
query_walk = torch.tensor([0.9, -0.3, 0.2, 0.6])
key_walk = torch.tensor([
    [0.8, -0.2, 0.1, 0.5],
    [-0.5, 0.9, 0.0, 0.2],
    [0.3, 0.0, 0.6, -0.4],
])
raw_walk = key_walk @ query_walk
scaled_walk = raw_walk / math.sqrt(query_walk.numel())
weight_walk = torch.softmax(scaled_walk, dim=0)
# [2]
print("raw scores:", raw_walk.numpy())
print("scaled scores:", scaled_walk.numpy())
print("weights:", weight_walk.numpy(), "sum:", weight_walk.sum().item())
raw scores: [ 1.0999999 -0.6        0.15     ]
scaled scores: [ 0.54999995 -0.3         0.075     ]
weights: [0.4879715  0.20856632 0.3034622 ] sum: 1.0

To run this comparison across an entire batch of sequences, project the decoder features \(\matr{D}\in\mathbb{R}^{B\times m\times d_d}\) and encoder representations \(\matr{H}\in\mathbb{R}^{B\times n\times d_e}\) into learned query, key, and value spaces:

\[ \begin{aligned} \matr{Q}&=\matr{D}\matr{W}_Q\in\mathbb{R}^{B\times m\times d_k}, &\matr{W}_Q&\in\mathbb{R}^{d_d\times d_k},\\ \matr{K}&=\matr{H}\matr{W}_K\in\mathbb{R}^{B\times n\times d_k}, &\matr{W}_K&\in\mathbb{R}^{d_e\times d_k},\\ \matr{V}&=\matr{H}\matr{W}_V\in\mathbb{R}^{B\times n\times d_v}, &\matr{W}_V&\in\mathbb{R}^{d_e\times d_v}. \end{aligned} \]

The raw encoder and decoder widths \(d_e\) and \(d_d\) need not match: \(\matr{W}_Q\) and \(\matr{W}_K\) map both into a shared comparison space of width \(d_k\), while \(\matr{W}_V\) projects values to an output width \(d_v\). With an additive mask \(\matr{M}\in\mathbb{R}^{B\times1\times n}\) broadcast across the \(m\) queries to exclude padded keys, scaled dot-product attention is

\[ \operatorname{Attention}(\matr{Q},\matr{K},\matr{V}) =\operatorname{softmax}\!\left( \frac{\matr{Q}\matr{K}^{\top}}{\sqrt{d_k}}+\matr{M} \right)\matr{V}, \tag{15.3}\]

where \(\matr{K}^{\top}\) transposes the last two axes of \(\matr{K}\) to shape \((B,d_k,n)\). The first matrix product \(\matr{Q}\matr{K}^{\top}\) forms all \(m\times n\) compatibility scores per batch item in a \((B,m,n)\) tensor. Applying softmax along the last axis normalizes each query’s scores over the \(n\) keys into a weight matrix \(\matr{A}\in\mathbb{R}^{B\times m\times n}\), and the second matrix product \(\matr{A}\matr{V}\) pools the values into an output of shape \((B,m,d_v)\). Whereas additive attention forms a four-dimensional intermediate tensor, scaled dot-product attention reduces the forward pass to two batched matrix multiplications (\(\matr{Q}\matr{K}^{\top}\) and \(\matr{A}\matr{V}\)) that map directly onto optimized dense linear algebra routines.1

Although a raw dot product is linear in each argument, the projections \(\matr{W}_Q\) and \(\matr{W}_K\) learn which features and magnitudes to align on top of representations that have already undergone nonlinear transformations.2

Why divide by \(\sqrt{d_k}\)?

Suppose, for the moment, that projected coordinates \(q_\ell\) and \(k_\ell\) are independent and identically distributed with zero mean and unit variance, and that their products are independent across coordinates. Then

\[ \E[\vect{q}^{\top}\vect{k}]=0, \qquad \var(\vect{q}^{\top}\vect{k}) =\sum_{\ell=1}^{d_k}\var(q_\ell k_\ell)=d_k. \]

The score’s standard deviation grows as \(\sqrt{d_k}\), and dividing by that factor returns the idealized variance to one. Geometrically, vectors in \(\mathbb{R}^{d_k}\) with unit-variance coordinates concentrate near a thin spherical shell of radius \(\sqrt{d_k}\) (\(\E[\norm{\vect{q}}^2]=\E[\norm{\vect{k}}^2]=d_k\)). Although two independent directions on that shell are nearly orthogonal, with typical \(\cos\theta\) of order \(1/\sqrt{d_k}\), multiplying both radii scales \(\vect{q}^{\top}\vect{k}=\norm{\vect{q}}\,\norm{\vect{k}}\cos\theta\) to order \(\sqrt{d_k}\).

To illustrate how this growing spread concentrates the softmax weights, we draw 20,000 queries and 16 independent keys per query with standard normal coordinates at widths \(d_k\in\{8,32,128,512\}\). Each query’s 16 raw scores are its dot products with its keys. We compare the sample variance of the scores and the mean largest softmax weight at each width, with and without division by \(\sqrt{d_k}\):

  1. Create a seeded generator and an empty scaling_rows list for per-width results.
  2. Audit score variance and softmax concentration versus width.
  3. Print, for each width, the raw and scaled score variances and largest softmax weights.
# [1]
scaling_gen = torch.Generator().manual_seed(6050132)
scaling_rows = []
# [2]
for width in (8, 32, 128, 512):
    raw_parts, raw_max_parts, scaled_max_parts = [], [], []
    for _ in range(20_000 // 500):
        q = torch.randn(500, 1, width, generator=scaling_gen)
        k = torch.randn(500, 16, width, generator=scaling_gen)
        raw = (q * k).sum(-1)
        raw_parts.append(raw.reshape(-1))
        raw_max_parts.append(torch.softmax(raw, dim=-1).max(-1).values)
        scaled_max_parts.append(
            torch.softmax(raw / math.sqrt(width), dim=-1).max(-1).values
        )
    raw_scores = torch.cat(raw_parts)
    scaling_rows.append((
        width,
        raw_scores.var(unbiased=True).item(),
        (raw_scores / math.sqrt(width)).var(unbiased=True).item(),
        torch.cat(raw_max_parts).mean().item(),
        torch.cat(scaled_max_parts).mean().item(),
    ))

# [3]
print("d_k   raw var   scaled var   raw max-w   scaled max-w")
for row in scaling_rows:
    print(f"{row[0]:3d}   {row[1]:8.3f}   {row[2]:10.3f}   "
          f"{row[3]:9.3f}   {row[4]:12.3f}")
d_k   raw var   scaled var   raw max-w   scaled max-w
  8      8.023        1.003       0.564          0.241
 32     32.142        1.004       0.779          0.246
128    127.985        1.000       0.889          0.246
512    510.347        0.997       0.944          0.247
As projected width grows, raw score variance and the largest softmax weight both rise sharply. Their scaled counterparts remain nearly horizontal, around variance one and largest weight one quarter.
Figure 15.3: The scaling argument under its stated random-feature assumptions. Left: raw dot-product variance grows with width, while division by the square root of the width keeps it near one. Right: without scaling, a 16-key softmax becomes nearly one-hot as width grows; scaling keeps its mean largest weight near one quarter (0.24 in this draw). These are seeded synthetic draws about concentration, not a claim about every trained layer’s gradients.

As \(d_k\) increases from 8 to 512, raw variance rises from \(8.02\) to \(510.35\), whereas scaled variance remains between \(0.997\) and \(1.004\). Over the same range, the mean largest unscaled softmax weight climbs from \(0.564\) to \(0.944\), while the scaled value stays between \(0.24\) and \(0.25\), well above the uniform baseline of \(1/16=0.0625\) and far below one-hot saturation at \(1.0\). This is the temperature dial of Chapter 2, set by the dimension. Scaling corrects the dimension-induced spread without normalizing the vectors, and trained projections need not remain independent or keep unit variance.

15.4 Excluding padded positions before softmax

Padding affects two computations, as Chapter 13 showed: the loss and an encoder’s final state. Attention introduces a third site where sequence lengths must be enforced: the score matrix. A padded key must not merely receive a low score. It must be excluded from normalization entirely. Setting its score to zero fails because \(\exp(0)=1\) still contributes a positive term to the normalizing sum. Instead, scores at padded positions must be set to \(-\infty\) before softmax so that \(\exp(-\infty)=0\).

The scaled-dot operator below accepts already projected queries, keys, and values, then audits a batch whose second example has two padded keys:

  1. Define the reusable scaled_dot_attention helper.
  2. Run and audit scaled dot-product attention with a source-padding mask.
# [1]
def scaled_dot_attention(
    queries: torch.Tensor,
    keys: torch.Tensor,
    values: torch.Tensor,
    key_is_valid: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
    """Attend with Q:(B,m,d_k), K:(B,n,d_k), V:(B,n,d_v)."""
    if queries.ndim != 3 or keys.ndim != 3 or values.ndim != 3:
        raise ValueError("Q, K, and V must be three-dimensional")
    if queries.shape[-1] != keys.shape[-1]:
        raise ValueError("projected Q and K widths must match")
    if keys.shape[:2] != values.shape[:2]:
        raise ValueError("K and V must have the same batch and key axes")
    if key_is_valid.shape != keys.shape[:2] or not key_is_valid.any(dim=1).all():
        raise ValueError("every example needs at least one valid key")

    scores = queries @ keys.transpose(-2, -1) / math.sqrt(keys.shape[-1])
    scores = scores.masked_fill(~key_is_valid[:, None, :], -torch.inf)
    weights = torch.softmax(scores, dim=-1)
    return weights @ values, weights

# [2]
mask_gen = torch.Generator().manual_seed(6050133)
Q_demo = torch.randn(2, 2, 4, generator=mask_gen)
K_demo = torch.randn(2, 4, 4, generator=mask_gen)
V_demo = torch.randn(2, 4, 3, generator=mask_gen)
valid_demo = torch.tensor([[True, True, True, True],
                           [True, True, False, False]])
context_demo, mask_weights = scaled_dot_attention(
    Q_demo, K_demo, V_demo, valid_demo
)
assert torch.allclose(
    mask_weights.sum(-1), torch.ones_like(mask_weights[..., 0])
)
assert torch.count_nonzero(mask_weights[1, :, 2:]) == 0
print("Q", tuple(Q_demo.shape), "K", tuple(K_demo.shape),
      "V", tuple(V_demo.shape), "A", tuple(mask_weights.shape),
      "AV", tuple(context_demo.shape))
print("maximum row-sum error:",
      f"{(mask_weights.sum(-1) - 1).abs().max().item():.2e}")
print("maximum padded weight:",
      f"{mask_weights[1, :, 2:].max().item():.1f}")
Q (2, 2, 4) K (2, 4, 4) V (2, 4, 3) A (2, 2, 4) AV (2, 2, 3)
maximum row-sum error: 0.00e+00
maximum padded weight: 0.0
A two-row by four-column attention heatmap contains numeric weights in the first two key columns. The final two columns are labeled PAD in both rows and have zero weight.
Figure 15.4: A source-padding mask acts before softmax. The second example has only two real keys, so its last two score cells are excluded and receive exactly zero weight. Cross-attention may read every real source position, so no causal triangle belongs here.
Watch the mechanism: zero is not a padding mask

Does giving padding a score of zero exclude it?

Follow query 2 of the audit's second example. These are its existing four keys, with the intermediate scores made visible.

Zero is not a padding mask Example 2 · query 2PAD scores at −∞: maskedkey 1−0.350exp0.70480.4090key 20.018exp1.01840.5910PAD−∞exp0PAD−∞exp0sum = 1.7233divide each by the same sum: weightsPAD share 0Guard: at least 1 real key Example 2 · query 2PAD scores at −∞: maskedkey 1−0.350exp0.70480.4090key 20.018exp1.01840.5910PAD−∞exp0PAD−∞exp0sum = 1.7233divide each by the same sum: weightsPAD share 0Guard: at least 1 real key

\( \class{mbs-probability}{\predictionpart{\alpha_j}}=\dfrac{\class{mbs-numerator}{\exp(s_j)}}{\class{mbs-denominator}{\sum_k\exp(s_k)}} \) \( \class{mbs-zero}{\exp(0)=1} \) \( \class{mbs-exclude}{\exp(-\infty)=0} \)

Keep at least one real key. An all-masked row has no distribution to normalize.

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

This expands the existing seeded audit, not a new sample or a trained alignment.

Scope and caveats

The formula uses the scores currently shown. Setting padded scores to zero is deliberately wrong; negative infinity excludes them before normalization. This is source-padding masking, not target-loss or causal masking: all real source keys remain available. Bars share a ruler within each level, but exponentials and weights have different units. An all-masked row is rejected, never treated as four valid zero weights.

Check yourself. Mask only one of the two padded keys with −∞ and leave the other at 0. What share of the weight still lands on padding?

About 0.367. The surviving padded key still contributes e⁰ = 1 to a sum of 2.7233. Zero is a score like any other; every padded key has to be excluded before normalization.

Transcript and keyboard help
  1. Query 2 of the second example has four source/key slots. Only the first two represent real keys. Predict whether a score of zero excludes padding.
  2. Try the wrong repair: overwrite both padded scores with zero. Leave the real-key scores unchanged.
  3. Exponentiate each shown score; the four bars grow from their baselines. Each padded slot contributes one, because the exponential of zero is one.
  4. One bracket gathers all four contributions into a shared sum of 3.7233. The two padded slots supply two of it.
  5. Divide every contribution by that sum. Padding receives about 0.5372 of the total weight, even though those source positions do not exist.
  6. Now push the padded scores down from zero toward negative infinity, on the same picture. Their contributions shrink from one to zero, the shared sum falls from 3.7233 to 1.7233 and its bracket closes onto the real keys, and the weight padding held moves onto them.
  7. With the padded scores at negative infinity, the first two weights are 0.4090 and 0.5910; both padded weights are exactly zero. The complete row sums to one.
  8. The guard remains essential: at least one key must be valid. With no valid key, stop before normalization rather than returning an all-zero distribution.

Silent; paused initially; 1.5× playback. Focus the animation pane: Space or K plays/pauses, arrows seek between beats, Home/End jump, Escape pauses. The scrubber and speed menu also work by keyboard. Reduced-motion mode holds each beat's state instead of moving between them.

WarningTrap: conflating the three sequence masks

The target loss mask specifies which output positions should not be graded. It is included for generality, although all ISO targets here share the same length. The cross-attention padding mask specifies which source positions do not exist. A causal mask prevents positions from attending to future tokens. Because our recurrent decoder generates one step at a time and the full source is already available, only the source-padding mask is needed here. Chapter 16 will introduce causal masking inside decoder self-attention.

An all-masked row has no valid probability distribution and makes softmax over all \(-\infty\) undefined (NaN). An implementation should assert that every example contains at least one real key, as the function above does.

15.5 Return to the date task of Chapter 13

We rerun the date task, altering a single architectural component while holding these settings fixed:

  • the same date generator and duplicate-source rejection;
  • the same train/validation/test split;
  • the same embedding width 32 and LSTM hidden width 128;
  • the same optimizer, learning rate, batch size, 25 epochs, teacher forcing, gradient clipping, and seed;
  • the same first 400 unambiguous validation sources for every learning curve;
  • one final exact-match evaluation on the same 437 unambiguous test sources.

Below, we copy the generator and split of Chapter 13 byte for byte, so both models train on identical examples:

  1. List the 12 month names and their day counts, using 28 days for February.
  2. Define the reusable make_date helper.
  3. Reconstruct Chapter 13’s sealed date benchmark.
  4. Print four pairs and the source overlaps between splits, then build both character vocabularies and print their sizes.
import random, torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt

# [1]
MONTHS = ["january", "february", "march", "april", "may", "june", "july",
          "august", "september", "october", "november", "december"]
DAYS = dict(zip(MONTHS, [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]))

# [2]
def make_date(rng: random.Random) -> tuple[str, str]:
    mo = rng.randrange(12)
    d = rng.randrange(1, DAYS[MONTHS[mo]] + 1)
    y = rng.randrange(1950, 2026)
    iso = f"{y:04d}-{mo+1:02d}-{d:02d}"
    style = rng.random()
    if style < 0.35:
        src = f"{MONTHS[mo]} {d}, {y}"
    elif style < 0.6:
        src = f"{d} {MONTHS[mo]} {y}"
    elif rng.random() < 0.7:
        src = f"{mo+1:02d}/{d:02d}/{y}"            # US convention: mm/dd
    else:
        src = f"{d:02d}/{mo+1:02d}/{y}"            # EU convention: dd/mm
    return src, iso

rng = random.Random(6050)
pairs, seen_sources = [], set()
# [3]
while len(pairs) < 9000:
    pair = make_date(rng)
    if pair[0] not in seen_sources:
        seen_sources.add(pair[0])
        pairs.append(pair)
train_pairs = pairs[:8000]
valid_pairs = pairs[8000:8500]
test_pairs = pairs[8500:]
# [4]
print(*pairs[:4], sep="\n")
train_sources = {s for s, _ in train_pairs}
valid_sources = {s for s, _ in valid_pairs}
test_sources = {s for s, _ in test_pairs}
print("exact-source overlaps: "
      f"train/valid {len(train_sources & valid_sources)}, "
      f"train/test {len(train_sources & test_sources)}, "
      f"valid/test {len(valid_sources & test_sources)}")

PAD, BOS, EOS = 0, 1, 2                             # special tokens first
SRC = sorted(set("".join(s for s, _ in pairs)))
TGT = sorted(set("".join(t for _, t in pairs)))
src_stoi = {c: i + 3 for i, c in enumerate(SRC)}
tgt_stoi = {c: i + 3 for i, c in enumerate(TGT)}
tgt_itos = {i: c for c, i in tgt_stoi.items()}
V_src, V_tgt = len(SRC) + 3, len(TGT) + 3
print(f"source vocab {V_src}, target vocab {V_tgt}")
('26 september 2024', '2024-09-26')
('april 7, 1962', '1962-04-07')
('12/15/1950', '1950-12-15')
('14 february 1997', '1997-02-14')
exact-source overlaps: train/valid 0, train/test 0, valid/test 0
source vocab 37, target vocab 14

Because numeric dates can be ambiguous, we retain the unambiguous subsets defined in Chapter 13. Heatmap inspections, like design decisions, use the validation split alone.

Put the learned read inside the recurrent decoder

The packed encoder now returns three outputs: its final \((\vect{h},\vect{c})\) state, which still initializes the decoder; a padded memory tensor containing all real encoder representations; and a validity mask excluding padded slots. At step \(t\):

  1. use the previous decoder hidden state \(\vect{s}_{t-1}\) as the query;
  2. compute additive scores over the encoder memory and mask padding;
  3. form the context vector \(\vect{c}_t\);
  4. concatenate \(\vect{c}_t\) with the embedding of the previous target token;
  5. advance the decoder LSTM by one step, then predict from \([\vect{s}_t;\vect{c}_t]\).

This timing follows the Bahdanau-style formulation. Luong-style variants often score with the current decoder state instead. Both are valid, but silently mixing their indexing obscures which decoder state supplied the query for each context vector.

For this date model, every tensor shape is concrete:

quantity shape
previous hidden query \((B,128)\)
encoder memory \((B,T_{\text{src}},128)\)
attention weights \((B,T_{\text{src}})\)
context \((B,128)\)
token embedding joined with context \((B,1,160)\)
decoder output \((B,1,128)\)
output-head input \([\vect{s}_t;\vect{c}_t]\) \((B,256)\)
  1. Define the reusable helpers: batchify, unambiguous, and LearnedAlignment.
  2. Filter validation and test pairs with unambiguous, and keep the first 400 validation pairs as a fixed set.
  3. Run a masked additive-attention LSTM decoder.
  4. Define the reusable helpers: AttentiveSeq2Seq, greedy_batch, and exact_match.
# [1]
def batchify(prs: list[tuple[str, str]], idx: list[int]) -> tuple[
    torch.Tensor, torch.Tensor, torch.Tensor
]:
    srcs = [torch.tensor([src_stoi[c] for c in prs[i][0]]) for i in idx]
    lengths = torch.tensor([len(s) for s in srcs])
    tgts = [torch.tensor([BOS] + [tgt_stoi[c] for c in prs[i][1]] + [EOS])
            for i in idx]
    S = nn.utils.rnn.pad_sequence(srcs, batch_first=True, padding_value=PAD)
    T = nn.utils.rnn.pad_sequence(tgts, batch_first=True, padding_value=PAD)
    return S, lengths, T

def unambiguous(prs: list[tuple[str, str]]) -> list[tuple[str, str]]:
    return [(s, t) for s, t in prs
            if "/" not in s or int(s[:2]) > 12 or int(s[3:5]) > 12]

# [2]
valid_unamb = unambiguous(valid_pairs)
test_unamb = unambiguous(test_pairs)
valid_fixed = valid_unamb[:400]
# [3]
print(f"fixed validation {len(valid_fixed)}; final test {len(test_unamb)}")

class LearnedAlignment(nn.Module):
    def __init__(self, hidden: int = 128, alignment: int = 128):
        super().__init__()
        self.key_map = nn.Linear(hidden, alignment, bias=False)
        self.query_map = nn.Linear(hidden, alignment, bias=False)
        self.selector = nn.Linear(alignment, 1, bias=False)

    def forward(
        self,
        memory: torch.Tensor,
        query: torch.Tensor,
        valid: torch.Tensor,
    ) -> tuple[torch.Tensor, torch.Tensor]:
        match_features = torch.tanh(
            self.key_map(memory) + self.query_map(query).unsqueeze(1)
        )                                                   # (B, T_src, d_a)
        scores = self.selector(match_features).squeeze(-1)  # (B, T_src)
        scores = scores.masked_fill(~valid, -torch.inf)
        weights = torch.softmax(scores, dim=-1)
        context = torch.bmm(weights.unsqueeze(1), memory).squeeze(1)
        return context, weights

# [4]
class AttentiveSeq2Seq(nn.Module):
    def __init__(self, v_src: int, v_tgt: int, embed: int = 32,
                 hidden: int = 128, alignment: int = 128):
        super().__init__()
        self.src_emb = nn.Embedding(v_src, embed, padding_idx=PAD)
        self.tgt_emb = nn.Embedding(v_tgt, embed, padding_idx=PAD)
        self.encoder = nn.LSTM(embed, hidden, batch_first=True)
        self.attention = LearnedAlignment(hidden, alignment)
        self.decoder = nn.LSTM(embed + hidden, hidden, batch_first=True)
        self.out = nn.Linear(2 * hidden, v_tgt)

    def encode(
        self, S: torch.Tensor, lengths: torch.Tensor,
    ) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor], torch.Tensor]:
        packed = nn.utils.rnn.pack_padded_sequence(
            self.src_emb(S), lengths, batch_first=True, enforce_sorted=False)
        packed_memory, state = self.encoder(packed)
        memory, _ = nn.utils.rnn.pad_packed_sequence(
            packed_memory, batch_first=True, total_length=S.shape[1])
        valid = torch.arange(S.shape[1])[None, :] < lengths[:, None]
        return memory, state, valid

    def decode_step(
        self,
        token: torch.Tensor,
        state: tuple[torch.Tensor, torch.Tensor],
        memory: torch.Tensor,
        valid: torch.Tensor,
    ) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor], torch.Tensor]:
        query = state[0][-1]                              # previous hidden state
        context, weights = self.attention(memory, query, valid)
        decoder_input = torch.cat(
            (self.tgt_emb(token), context.unsqueeze(1)), dim=-1
        )
        decoder_output, state = self.decoder(decoder_input, state)
        logits = self.out(torch.cat((decoder_output[:, 0], context), dim=-1))
        return logits, state, weights

    def forward(
        self, S: torch.Tensor, lengths: torch.Tensor, T_in: torch.Tensor,
    ) -> torch.Tensor:
        memory, state, valid = self.encode(S, lengths)
        logits = []
        for t in range(T_in.shape[1]):
            logit, state, _ = self.decode_step(
                T_in[:, t:t + 1], state, memory, valid)
            logits.append(logit)
        return torch.stack(logits, dim=1)                 # (B, T_tgt, V_tgt)

@torch.inference_mode()
def greedy_batch(
    model: AttentiveSeq2Seq,
    prs: list[tuple[str, str]],
    batch: int = 128,
    maxlen: int = 12,
) -> list[tuple[str, str, torch.Tensor]]:
    model.eval()
    outputs = []
    for start in range(0, len(prs), batch):
        subset = prs[start:start + batch]
        S, lengths, _ = batchify(subset, list(range(len(subset))))
        memory, state, valid = model.encode(S, lengths)
        token = torch.full((len(subset), 1), BOS)
        token_steps, weight_steps = [], []
        for _ in range(maxlen):
            logits, state, weights = model.decode_step(
                token, state, memory, valid)
            token = logits.argmax(-1, keepdim=True)
            token_steps.append(token[:, 0])
            weight_steps.append(weights)
        predicted = torch.stack(token_steps, dim=1)
        attention = torch.stack(weight_steps, dim=1)
        for row, (source, _) in enumerate(subset):
            chars = []
            for index in predicted[row].tolist():
                if index == EOS:
                    break
                chars.append(tgt_itos.get(index, "?"))
            outputs.append((
                source,
                "".join(chars),
                attention[row, :, :lengths[row]].clone(),
            ))
    return outputs

@torch.inference_mode()
def exact_match(
    model: AttentiveSeq2Seq, prs: list[tuple[str, str]],
) -> float:
    generated = greedy_batch(model, prs)
    return sum(
        guess == truth
        for (_, truth), (_, guess, _) in zip(prs, generated)
    ) / len(prs)
fixed validation 400; final test 437

Several elements change relative to Chapter 13. The embeddings and encoder width remain untouched. Values are now the contextual encoder states themselves, while learned linear projections construct match features for keys and queries. The decoder must now advance one step at a time because each subsequent context depends on the preceding decoder state. Teacher forcing still supplies the true previous token, but it no longer permits a single fused RNN call over the target sequence.

Attention on the matched schedule

The fixed-state baseline curve is the validation trajectory recorded for the packed, teacher-forced model in Chapter 13 (Figure 13.3). We reuse those recorded values, train the attentive model once, and inspect its validation alignments, and we evaluate on the test split once, after the model is fixed:

  1. Set the checkpoint epochs and Chapter 13’s frozen curve, then build AttentiveSeq2Seq and its optimizer.
  2. Matched-schedule date rematch and reused-endpoint audit.
  3. Print both validation curves, parameter counts, one decoded example, alignment audits, and the test result.
# [1]
checkpoints = (2, 4, 6, 8, 12, 16, 20, 25)
baseline_curve = {
    2: 0.0025, 4: 0.1475, 6: 0.5375, 8: 0.7575,
    12: 0.9500, 16: 0.9500, 20: 0.9950, 25: 0.9575,
}

torch.manual_seed(6050)
attention_model = AttentiveSeq2Seq(V_src, V_tgt)
optimizer = torch.optim.Adam(attention_model.parameters(), lr=1e-3)
attention_curve = {}

# [2]
for epoch in range(1, 26):
    attention_model.train()
    permutation = torch.randperm(len(train_pairs))
    for start in range(0, len(train_pairs), 128):
        ids = permutation[start:start + 128].tolist()
        S, lengths, T = batchify(train_pairs, ids)
        logits = attention_model(S, lengths, T[:, :-1])
        loss = F.cross_entropy(
            logits.reshape(-1, V_tgt),
            T[:, 1:].reshape(-1),
            ignore_index=PAD,
        )
        optimizer.zero_grad()
        loss.backward()
        nn.utils.clip_grad_norm_(attention_model.parameters(), 5.0)
        optimizer.step()
    if epoch in checkpoints:
        attention_curve[epoch] = exact_match(
            attention_model, valid_fixed)

# [3]
print("epoch   fixed-state   attention")
for epoch in checkpoints:
    print(f"{epoch:2d}       {baseline_curve[epoch]:6.2%}       "
          f"{attention_curve[epoch]:6.2%}")

validation_outputs = greedy_batch(attention_model, valid_fixed)
source_example, truth_example = valid_fixed[0]           # fixed before training
_, generated_example, alignment_example = validation_outputs[0]

year_mass, year_top1, row_errors = [], [], []
for (source, _), (_, _, weights) in zip(valid_fixed, validation_outputs):
    year_positions = list(range(len(source) - 4, len(source)))
    for step in range(4):
        year_mass.append(weights[step, year_positions].sum().item())
        year_top1.append(int(weights[step].argmax().item() in year_positions))
    row_errors.append((weights[:10].sum(1) - 1).abs().max().item())

attention_params = sum(p.numel() for p in attention_model.parameters())
baseline_params = 169_326
print(f"parameters: baseline {baseline_params:,}; "
      f"attention {attention_params:,} "
      f"(+{(attention_params / baseline_params - 1):.1%})")
print(f"validation example: {source_example!r} -> "
      f"{generated_example!r} (truth {truth_example})")
print(f"validation year-region mass: {np.mean(year_mass):.3%}; "
      f"top key in region: {np.mean(year_top1):.3%}")
print("maximum validation row-sum error:", f"{max(row_errors):.2e}")

test_exact = exact_match(attention_model, test_unamb)     # one final test audit
print(f"one final test audit ({len(test_unamb)} sources): "
      f"baseline 93.1%; attention {test_exact:.1%}")
epoch   fixed-state   attention
 2        0.25%        9.50%
 4       14.75%       74.50%
 6       53.75%       93.25%
 8       75.75%       99.00%
12       95.00%       99.75%
16       95.00%       99.75%
20       99.50%       99.75%
25       95.75%       99.75%
parameters: baseline 169,326; attention 269,550 (+59.2%)
validation example: 'may 17, 1971' -> '1971-05-17' (truth 1971-05-17)
validation year-region mass: 97.469%; top key in region: 99.688%
maximum validation row-sum error: 2.38e-07
one final test audit (437 sources): baseline 93.1%; attention 100.0%
The left learning curves show the attention model reaching high exact-match accuracy much earlier than the fixed-state baseline. The right character-by-character heatmap concentrates the first four output rows inside a box around the source year positions.
Figure 15.5: Left: exact-match accuracy on the fixed 400-source validation subset of Chapter 13. Both models use the same data, embedding and recurrent hidden widths, optimizer, top-level seed, and epoch schedule. Because attention adds parameters and a stepwise decoder, this is not a parameter- or compute-matched ablation. Right: the predeclared first validation example. Rows are emitted ISO characters, columns are contextual encoder states indexed by source character, and the orange box marks the source year. The first four rows concentrate there.

At epoch 6, the fixed-state baseline reaches \(53.75\%\) while the attentive model attains \(93.25\%\), a difference of 39.5 percentage points. By epoch 12, the gap is narrower: \(95.0\%\) versus \(99.75\%\). On the final test audit of 437 unambiguous sources, the models score \(93.1\%\) and \(100.0\%\), respectively.

WarningTrap: confounding bottleneck removal with model capacity

Although we matched the dataset, hidden dimensions, optimizer, initial seed, and epoch budget, this comparison is not a single-variable ablation. The attentive model has 269,550 parameters compared to the baseline’s 169,326 (a 59.2% increase), and its per-step attention mechanism incurs more FLOPs. Initializing these additional projection layers also consumes pseudo-random draws before the first shuffle, subtly altering minibatch ordering. Faster convergence across epochs shows an empirical advantage of the attentive architecture in this single seeded run, but it does not isolate bottleneck removal from added model capacity as the sole underlying cause.

The heatmap addresses a more specific question. On the fixed validation example, the year must travel from the last four source positions to the first four outputs. The boxed region catches that movement, and across all 400 fixed validation examples, the first four attention rows place 97.5% of their probability mass on the contextual encoder states corresponding to the source-year characters. The top-weighted state falls within that region 99.7% of the time.

NoteReading the heatmap

Columns are indexed by source characters, but their values are contextual hidden states. Because each state summarizes a recurrent prefix, the representation at a delimiter can readily encode information about preceding tokens. Attention weights reflect internal computational routing, not calibrated predictive confidence or a causal explanation of the model.

15.6 One head in the date model

The date model uses a single learned additive scorer and one weighted context vector. Its query and key projections compute alignment scores, while the encoder states supply retrieval values. Multi-head routing, in which parallel projections allow distinct heads to attend to multiple features simultaneously, is developed in Chapter 16.

15.7 Recurrence remains: the serial bottleneck

Cross-attention resolves the fixed-state bottleneck of sequence-to-sequence models: every decoder output can read any encoder state through a learned, differentiable soft address. For the year field of the date task, the learned weights concentrate on the encoder states indexed by the source-year characters, without explicit alignment supervision.

Yet the host networks are still recurrent: \(\vect{h}_i\) depends on \(\vect{h}_{i-1}\), and \(\vect{s}_t\) depends on \(\vect{s}_{t-1}\). Because the hidden states of a sequence cannot be computed in parallel during training, the number of sequential steps grows with sequence length. This constraint motivates the next architectural transition: can self-attention replace recurrence entirely by allowing every token to attend across its own sequence in parallel? Chapter 16 answers that question.

NoteCheck yourself

Close the book for one minute and reconstruct the soft lookup.

  • What distinct jobs do queries, keys, and values perform?
  • Why does masking belong inside score normalization?
  • What does a learnable compatibility function gain over a fixed kernel?

15.8 Okay, so: attention softens the address

  1. Attention softens the address. Chapter 13 learned embedding representations while keeping lookup indices hard. The softmax of Chapter 2 makes the lookup differentiable: learned scores become a probability distribution over memory locations.
  2. Additive attention learns the compatibility function. Query and key are projected into a shared alignment space, a \(\tanh\) nonlinearity creates match features, and a linear selector produces each scalar score. The same scorer is reused across all decoder–source pairs.
  3. Cross-attention bridges two sequences. Queries originate in the decoder while keys and values reside in the encoder. Each step forms its own context vector, so the final state of Chapter 13 is no longer the only path from source to output.
  4. Scaled dot product learns comparison spaces. Projected \(\matr{Q}\matr{K}^{\top}/\sqrt{d_k}\) maps directly to dense matrix products. Under the idealized independent unit-variance model, scaling holds score variance near one instead of letting it grow with \(d_k\).
  5. Masking is part of the operator. Padding must be excluded before softmax. Setting a padded score to zero fails to mask it. Cross-attention attends over all valid source positions, while causal masking belongs to the self-attentive decoder in Chapter 16.
  6. On dates, attention learns where to read. Under the shared schedule, attention reaches 93.25% validation exact match at epoch 6 and 100.0% on the final test audit. Across the validation set, the four rows that emit the year allocate 97.5% of their weight to states indexed by the source-year region. Parameter count and compute were not matched: the attentive model has 59.2% more parameters.

The recurrent networks that construct this memory bank remain in place for now. Chapter 16 lets a sequence query itself, routing context across positions so that recurrence becomes optional.

Sources and further reading

Exercises

  1. (Pencil.) Starting from Equation 15.2, write every batched shape for \(B=32\), source length 17, \(d_e=128\), \(d_d=96\), and \(d_a=64\). Which axis must softmax normalize, and why can the raw encoder and decoder widths differ?

  2. (Code.) Pass a batch whose mask contains one all-false row to LearnedAlignment, and inspect the result of softmax over all \(-\infty\). Add an assertion that every row keeps at least one real key. Then set the padded scores to zero instead of \(-\infty\) and explain why padding receives weight.

  3. (Pencil.) For encoder width \(d_e\), decoder width \(d_d\), and alignment width \(d_a\), count the parameters in \(\matr{W}_q\), \(\matr{W}_k\), and \(\vect{v}_a\). Show that the parameter count is independent of both sequence lengths \(m\) and \(n\), and identify the tensor shape that depends on both.

  4. (Pencil.) (a) Generalize the scaling derivation to independent coordinates with variances \(\sigma_q^2\) and \(\sigma_k^2\). (Code.) (b) Modify the seeded audit and check which divisor returns score variance near one.

  5. (Code.) Retrain the date model with the LearnedAlignment scorer at alignment widths \(d_a\in\{32,64,256\}\). Keep the split, schedule, and validation subset fixed. Select any design choices on validation, report the score on the previously used test set once, and compare both convergence and year-region alignment. Explain why a large weight is neither calibrated confidence nor a causal explanation.

  6. (Pencil.) The heatmap of Figure 15.5 labels columns by source characters even though each value is a recurrent prefix state. Give two reasons the encoder state at a delimiter might carry useful month or day information. Then state one conclusion the heatmap supports and one conclusion it cannot support.

  7. (Audit.) The date model’s nn.Embedding learned a vector for each of the characters 0–9. Predict their geometry before inspection. Common misconception: “the embeddings must form a clean number line ordered by numeric value.” The training objective does not require that geometry. Extract the ten rows, project them to two dimensions, and compare against the same rows from an untrained model, an initialization baseline. What structure did training actually build, and which of the model’s jobs (months, days, years) explains it?


  1. Appendix C turns that intuition into a conditional performance model: dense products can reach high arithmetic intensity through reuse, but shapes, data movement, implementation, and hardware determine the actual regime.↩︎

  2. In Chapter 16, a position-wise feedforward network will provide that nonlinear feature transformation after recurrence disappears.↩︎