4.1.5 · D1Transformer Architecture

Foundations — Multi-head attention

2,787 words13 min readBack to topic

This page assumes you know nothing about the notation used in the parent Multi-Head Attention note. We build every symbol from the ground up, in the order they depend on each other. Nothing is used before it is drawn.


0. What are we even looking at?

A sentence like "the cat sat" is, to a computer, just words. But neural networks only eat numbers. So the very first thing that happens is: each word becomes a list of numbers. Everything else is arithmetic on those lists.

Keep that picture in your head: words in a row, each word its own list of numbers. Every symbol below is a name for some part of this picture.


1. The number — how many words

Picture: in figure s01 above, we had 3 words, so . In the parent's example, .

Why the topic needs it: attention compares every word to every other word. If there are words, that's an grid of comparisons. So controls how big that grid is.


2. A vector — one word as an arrow of numbers

Picture: the list of numbers describing one word. To build intuition we stand a single word's list up as an arrow in space: 2 numbers = an arrow on a flat page, 3 numbers = an arrow in a room.

Why the topic needs it: a word's "meaning" is stored as the direction and length of its arrow. Two words with similar meaning point in similar directions — and measuring "similar direction" is exactly what attention will do (see §6).

See also Query-Key-Value Intuition, which uses these arrows directly.


3. The number — how long each word-vector is

Picture: how many numbers are in one word's list. Longer list = more room to store meaning.

Why the topic needs it: this is the "budget" of numbers the model spends per word. Multi-head attention will split this budget among heads — that is the whole trick of §7.


4. A matrix — a grid of numbers, and what stacking vectors gives you

Picture: figure s01 is a matrix — lay every word down as one row and stack the rows, giving a grid of shape (one row per word, numbers across). In the parent, means "a grid with 10 rows — one per word — and 512 columns".

Why the topic needs it: whole sentences are processed as one grid, not word-by-word. This is what makes transformers fast.


5. Matrix multiply & the superscript — reshaping and mixing

Picture: figure s03 walks one output entry through: slide a row of across a column of , multiply the aligned numbers, add them up — that single sum is one cell of the answer. Do this for every row–column pair to fill the whole output grid.

Why the topic needs it: The parent writes . Because each word is a row (our §4 convention), is and is also — you cannot multiply them directly (inner numbers and don't match). Transposing to fixes the shapes, and the multiply becomes an grid where cell is "row dotted with row " = how much word aligns with word . See §6 for why that number means "similarity".

This reshaping-by-multiply is exactly what Linear Projections in Neural Networks is about, and the projection matrices (defined in §7 below) are what get multiplied in.


6. The dot product — the "how alike are these two arrows?" tool

Picture: two arrows and the angle between them.

All cases (never leaves you guessing):

  • Same direction → large positive → "very relevant".
  • Perpendicular (90°) → zero → "unrelated".
  • Opposite direction → negative → "actively pushing apart".

Why the topic needs it: this is the raw score of "how much word 's question aligns with word 's key" in the parent's walkthrough.


7. Q, K, V and the projection matrices — the three roles a word plays

Why the topic needs it: without these three roles there is no "asking" and "answering". The subscripts you saw in the parent — , — are now fully named: word 3's query in head 1, word 's key in head 1.


8. Splitting the budget: and

Picture: cut the 512-long word-vector into 8 shorter pieces of 64 each.

Why the topic needs it: this is the reason it's called "multi-head". Each head sees the words through its own smaller projection, so one can track grammar while another tracks meaning.


9. Softmax — turning raw scores into attention weights

All cases: one very large score → its weight ≈ 1, others ≈ 0 (near hard-max). All equal scores → equal weights (pure average). Negative scores are fine — is still positive.

Why the topic needs it: the weights in the parent tell word 3 how much of each other word's value to keep. See Self-Attention and Attention Visualization to see these weights drawn as a heat grid.


10. The weighted sum of values — how a word gathers its answer

Picture: figure s05 — the same -long output slot, filled mostly by the value-vector of the word with weight 0.6, a little by the one with 0.3, barely by the one with 0.1.

Why the topic needs it: this final blend is the answer attention produces — a context-aware version of each word, built from the words it decided were relevant.


11. The scale factor

Why divide by it? A dot product of two 64-long vectors adds up 64 products, so it tends to grow large. Large scores make softmax nearly one-hot and kill the gradient. Dividing by keeps scores in a sane range so learning stays stable.


How the pieces feed the topic

vector = word as numbers

matrix = one row per word

n = number of words

d_model = numbers per word

Q K V = query key value roles

matrix multiply and transpose

dot product = similarity score

scale by sqrt d_k

softmax = attention weights

weighted sum of value vectors

h and d_k = split the budget

Multi-Head Attention

Each foundation on the left must be understood before the one it points to. Everything funnels into the topic node at the bottom.


Equipment checklist

Test yourself — reveal only after you have an answer.

Is a word a row or a column when stacked into ?
A row — the parent uses , one row per word.
What does count, in one word?
The number of tokens (words) processed together.
What is a vector, pictured as a shape?
An ordered list of numbers, drawable as an arrow with direction and length.
What does measure?
How many numbers describe a single word.
What does mean out loud?
The set of grids of real numbers with 512 rows and 64 columns.
Why must inner dimensions match to multiply ?
Each output entry is a dot product of a row of with a column of , so those must be the same length.
What does the transpose do, and why is used?
It flips rows and columns; since words are rows, makes shapes line up so produces an score grid.
What are , , in plain words?
Query = a word's question, Key = a word's label, Value = a word's content to hand over.
What is ?
Word 3's query row inside head 1 — one -long vector.
When is a dot product large / zero / negative?
Large when arrows point the same way, zero when perpendicular, negative when opposite.
Why must be divisible by ?
So is a whole number and the heads tile back exactly to .
What does softmax guarantee about its outputs?
They are all positive and sum to 1 — valid attention weights.
What is the weighted sum of values?
Each value-vector times its attention weight, added up — a weighted average landing near the most-attended words.
Why divide scores by ?
To stop large dot products from making softmax saturate and killing gradients.