Deep Black BoxOpen up the black box of AI one problem at a time, with your own hands. 21 problems in all.
Question 14

Looking at two words at once

The four words in green carry a tag (the same directions as in Problem 13). “The newspaper” puts out the query, and the query is compared against the tags.
Your target — there are two stages

Stage 1  With one query, get the lower of the two as high as it will go (it goes up to 0.250)

Stage 2  Switch to two queries and get both coffee and newspaper above one half

Both are values this problem chose. “Above one half” in stage 2 is the same measure as in Problem 13.
You can line up as many queries as you like. One of them is called a head (the original paper uses eight).
Tags (black) and query 1 (red)
Shares from query 1 (the four add up to 1)
Tags (black) and query 2 (red)
Shares from query 2 (the four add up to 1)
↓  mix the contents by those weights, and you get the next word
In the real thing there is one more stage after attention (multiply and add), and that is where “both together” gets picked up (Problem 1, and ④ in the closing diagram below). This problem skips that stage and puts out the third bar directly. The score for the third bar is built from the two shares multiplied together, so the third bar comes out top only once the lower of the two passes 0.500.
0.250 is the ceiling for one query. Nothing is being picked out — all four words are being looked at equally.
The shares add up to 1, so two words both going over a half never happens, wherever you put the tags. All 14,801 combinations counted, and 0 of them did it.
Aim at two words at once, and picking nothing out turns out to be the best you can do.
Scroll a long way back up from here. At the very top of the first panel there is a [2 queries] button. Press it, and get both coffee and newspaper above one half.
What to take away
What you just did is multi-head attention. One head of attention is a weighted average, so it cannot look strongly at two words at once. That is why the number of heads goes up. What went up is the count, not the ability.
With one head, neither of the two words can pass a half (0.500). It never happens, wherever you put the tags (all counted, 0 combinations).
There is one band, so they compete for it
1 query (one head) head 1 coffee 0.511 newspaper 0.081 desk 0.204 hand 0.204 2 queries (two heads) head 1 coffee 0.511 head 2 newspaper 0.511
The band is fixed at length 1. Push coffee up to 0.511 and newspaper drops to 0.081. Aim query 2 at the newspaper, and you can take 0.511 for it there.
The original paper
with a single attention head, averaging inhibits this  The word Fig. 4 is about is 'its'. Head 5 is looking at 'Law' and head 6 at 'application'two heads dividing the work. Isolated attentions from just the word 'its'
With two queries, each word takes its number from whichever query is looking at it more. The real thing does not choose: it lines the two answers up and passes them to the next stage (concatenate, then multiply and add). This problem takes whichever is looking more instead. Either way, the information from both queries survives — that much is the same.
Where this pays off
Heads are added so that different words can be looked at at the same time. With one head the shares get fought over, so two words cannot both be looked at strongly.
What you have seen so far

The Transformer block

coffeedesknewspaperit
↓
① Attention  ●←→●←→●←→●
This chapter = this is the only new part
⊕  ② Skip path  Problem 10
③ Normalization  Problem 7
④④④④
④ Multiply and add (within each word)  Problem 1 (y = a × hours + b) and Problem 6
⊕  ② Skip path  /  ③ Normalization
â–Œ  Stack this N times (six in the original paper)

① Attention is the only new part. ②③④ are pieces you handled in Chapters 1 and 2.
Attention does not see the order of the words. So a mark for the order is added at the entrance (positional encoding). Without it, “the hand hits the coffee” and “the coffee hits the hand” stop being distinguishable.
The old way (RNN) went through the words one at a time. Which is why the title is “Attention Is All You Need” = you do not need the RNN.

The original paper
LayerNorm( x + Sublayer(x) )
Attention is not “understanding” — it is a weighted average, and why training finds those W is not known.
Common explanations that are not in the original paper
How many heads there should be, and what one head does, are widely misunderstood.
What you often hearIn the original paper
the “it → animal” picture Not there. "animal" gets 0 hits in a full-text search
“head 1 does dependencies, head 3 does tense” Not written. No head is given a task name
“the more heads the stronger” It goes the other way too. One head is 0.9 below the best, but 32 heads (25.4) score lower than 8 (25.8)
“the dot product is the best way to measure” The paper leaves it open (the discussion of row B in Table 3)
What can be said stops at individual attention heads clearly learn to perform different tasks (end of §4 / Table 3 row A).
How this problem is built (where the numbers come from)
ItemSource
α = softmax(q·k / √d_k), z = Σ α v, q = W_q x and the rest
why ÷√d_k (the variance of the dot product is d_k)same paper, §3.2.1 footnote 4
the dot product is not claimed to be optimalsame paper, Table 3 row B
with a single head, averaging inhibits thissame paper, §3.2.2
one head is 0.9 lower / 32 heads score lower than 8same paper, Table 3 row A and others
Fig. 4 is about the word 'its' (looking at Law and application)same paper, Figure 4
the block equation and positional encodingsame paper, §3.1 / §3.5, Table 3 row E
RNNs went through the words one at a timesame paper, §1 / Table 1
why it works as well as it does is not known

What this problem placed

The words, the scores, the directions of the tags and the contents are all numbers this problem placed. They are not measurements. This is not “what an LLM puts out”. The list: the six words and scores in Problem 12 (Pacific 3.0 and so on), the directions of the tags (coffee 0° / desk 90° / newspaper 180° / hand 270°), the contents (coffee→spilled 3.0 / newspaper→tore 3.0), and the targets (above one half = 0.500, and the 8.0 times and 2.5 sentences in Problem 12).

Put three words into the same formula (cat 0° / sat 90° / it 60°) with the query at 0° and you get 0.456 / 0.225 / 0.320, matching the worked example in our reference notes, Part 1 §6 (0.46 / 0.22 / 0.32).

What is simplified here

The real thing uses d_k = 64 in 8 sets, with tens of thousands of words. Here it is 2 dimensions, 4 words and at most 2 sets, and the tags are placed by hand rather than learned as W.