Chapter 04II · Inside the Transformer8 min read · ~12 min hands-on

II · Inside the Transformer

In this lesson

Attention, where tokens start talking

Watch one fixed starting vector produce two different outputs across contexts, then inspect one toy attention head from scores to output.

By the end, you can

  • Distinguish the roles of queries, keys, and values in one attention head.
  • Trace scores, mask, softmax, and weighted sum into the output vector.

Before you start: Tokenization, or how a model reads, Embeddings: meaning becomes geometry

“Eat an orange.” “Paint it orange.” Both phrases contain orange, token #901, at the same starting point [−0.68, 0.44]. Only its neighbors change. Can context alone move its output?

One attention head will settle the wager. Follow one trip through a library: form a query, score the available keys, mask forbidden shelves, then mix the values.

Observe one token in two contexts

The lab shows both contexts with orange selected, its starting row repeated in each column. Before you compare the output rows, commit to an answer. Should either coordinate move at all?

Predict the fate of orange

The lab reports the output as [x, y]. Bet on the direction of the change before revealing the numbers.

Your bet

With orange selected in both columns, how does its output differ between “eat an orange” and “paint it orange”?

Commit before manipulating the instrument. This choice is final.

Attention workbench / toy head

Target token #901 starts from the same vector in both contexts. Inputs and Q/K/V projections are authored; scores, softmax, and mixtures are computed live.

Starting vector, sharedorange · #901 · [-0.68, 0.44]

eat an orange

Scores

  1. orange compares with eat-0.672
  2. orange compares with an-0.336
  3. orange compares with orange0.348

Weights after softmax

  1. eat0.193
  2. an0.271
  3. orange0.536

Contextualized vector

orange[0.08, 0.22]

Σ weight × V

paint it orange

Scores

  1. orange compares with paint-0.247
  2. orange compares with it-0.124
  3. orange compares with orange0.348

Weights after softmax

  1. paint0.253
  2. it0.287
  3. orange0.460

Contextualized vector

orange[-0.01, 0.44]

Σ weight × V

Same starting vector in both contexts, yet the two outputs differ: context alone moved orange.

With the causal mask on, one row of the softmax matrix has a weight pinned to exactly 1. Which position’s row?

Live missions

  • Claim the row with the weight pinned to 1
  • Reveal the second head and watch it diverge
Inspect Q, K, V, and the full matrix
eat an orange · Queries, keys, and values
TokenQKV
[1.00, 0.50][1.00, -0.50][1.00, 0.50]
[0.50, 0.25][0.50, -0.25][0.50, 0.25]
[-0.90, 0.10][-0.46, 0.78][-0.46, 0.10]
eat an orange · Weights after softmax
Q ↓ / K →eatanorange
1.0000.0000.000
0.5330.4670.000
0.1930.2710.536
paint it orange · Queries, keys, and values
TokenQKV
[-0.50, 1.00][0.50, 1.00][0.50, 1.00]
[-0.25, 0.50][0.25, 0.50][0.25, 0.50]
[-0.90, 0.10][-0.46, 0.78][-0.46, 0.10]
paint it orange · Weights after softmax
Q ↓ / K →paintitorange
1.0000.0000.000
0.5330.4670.000
0.2530.2870.460

score = (Q · K) × sharpness / √2

Each displayed row sums to 1.000; the blue surface redundantly represents each weight.

Manipulate how tokens look around

Keep orange selected and compare the two output rows. The starting vector never moved, yet the outputs disagree. Each column offers a different set of values to mix, so context wins the bet.

This fixture is hand-authored to expose the computation. It does not show that a trained head would discover this exact linguistic relation.

First, while orange is still selected, turn on Second head or nudge the Score sharpness slider: the two contextualized outputs settle your bet.

Then open Inspect Q, K, V. For one selected position, trace its query against every key, then follow the mask, softmax weights, and weighted values into the output.

Leave the causal mask on and select the first token. Later keys are unavailable because next-token training must not reveal future tokens. Disable the mask and watch their weights return.

Select orange again and raise the Score sharpness slider: in the eat an orange column, orange’s own weight climbs from about 0.54 to 0.72.

This teaching control is not chapter 7’s generation temperature. Standard attention holds the scale fixed at 1 / √dₖ.

Challenge The lab awaits your claim: with the causal mask on, one row of the softmax matrix holds a weight pinned to exactly 1. Pick that position’s chip before reading on; the tempting answer is not the right one.

Compare your answer

It is the first token’s self-weight, the top-left cell. The causal mask leaves it alone in the library, with exactly one key it may consult: its own.

Softmax over one unmasked score always returns 1. Change that score, change the neighbors, and the head still has nobody else to consult.

Optional: compare two heads.

Second head applies another authored set of projections and produces another mixture. Trained heads can differ, but this fixture does not prove that each head learns one clean linguistic role.

A multi-head layer combines these mixtures before the next operation.

Explain the Q, K, V library

Self-attention builds a new vector for each position by mixing information from the context it may read. That is how the two occurrences can diverge despite sharing one lookup row.

  1. Same lookuporange · #901 · [−0.68, 0.44]
  2. Compare Q with Kscores → weights
  3. Mix the V vectorscontextualized output

In a trained Transformer, three learned projections map every input vector e into:

  • a query Q: what this position is looking for;
  • a key K: how this position can be found;
  • a value V: the information it contributes if selected.

Because Q and K use different learned projections, the vector a position searches with need not be the vector that gets it found.

Back at the library desk, your query is the request you bring, each key a call number you compare it against, each value the contents you walk out with. Better matches receive larger shares.

Softmax is the head’s accountant. It converts the unmasked scores into positive weights, and the books must close at exactly 100%. The weighted sum of the values is a contextualized output vector.

  1. Each token’s vectore
  2. Three projectionsQ · K · V
  3. Q meets every Kscores + mask
  4. Softmaxweights, Σ = 100%
  5. Weights × V, summedcontextualized output
The same story, in formulas

For one query, every key receives the score Q · K / √dₖ, where dₖ is the key dimension. Dividing by √dₖ keeps dot products from ballooning as the key dimension widens.

The causal mask replaces forbidden scores with −∞. Their softmax weights become zero, so an earlier position cannot use a later value while learning to predict the next token.

Softmax turns the remaining scores into positive weights summing to 1:

softmax(sᵢ) = exp(sᵢ − max(s)) / Σⱼ exp(sⱼ − max(s))

Math pause: softmax without calculus

Think of each score as a bid. Exponentiation magnifies a lead, then division by the total turns all bids into shares that add to 100%.

That − max(s) is pure bookkeeping: the accountant shifts every score by the same amount, which keeps the exponentials small and leaves every weight untouched. Finally, the head computes Σ weightᵢ × Vᵢ.

An attention weight is not a complete explanation.

It describes one mixture at one position, head, and layer. Final behavior also depends on other heads, later layers, residual paths, and nonlinear transformations.

Reflect on where the information travels

Checkpoint

After Q·K scores, masking, and softmax produce the weights, what gets multiplied by those weights and added into the output?

After Q·K scores, masking, and softmax produce the weights, what gets multiplied by those weights and added into the output?

Pick an answer first.

Checkpoint

When another token attends to orange, which orange vector sets the score, and which one flows into the other token’s output?

When another token attends to orange, which orange vector sets the score, and which one flows into the other token’s output?

Pick an answer first.

Connect attention to the missing signal

Context moved orange through Q, K, V, a mask, softmax, and a weighted sum.

But put those same tokens in a different order, and with the mask off this head sees content and nothing else. What tells it which one came first?

The next lesson gives every token the one thing this head cannot see: its place in line.

Sources and scope
  • Vaswani et al. (2017) define scaled dot-product attention, causal masking, multi-head attention, and the Transformer architecture.
  • Jain & Wallace (2019) show why attention weights alone should not be treated as a complete explanation of model behavior.
  • The lab is one handcrafted 2D head. Its score-sharpness slider is pedagogical and is not generation temperature.
  • Content and claims reviewed on July 28, 2026.