Foundations — Self-attention mechanism in detail
This page assumes you have seen none of the notation in the parent note. We build every symbol from a picture, in an order where each one leans only on the ones before it. By the end you can read the formula one symbol at a time and know what each stroke means.
1. A word becomes a list of numbers: the vector
Computers cannot store the idea "cat". So we hand each word a short list of numbers, like . That list is called an embedding and, mathematically, a list of numbers with a fixed length is a vector.

Look at the arrow above. Its two numbers tell you how far right and how far up to walk. Why the topic needs this: self-attention never sees the letters "c-a-t"; it only ever sees these arrows. Meaning is now a direction in space, and "similar meaning" will become "similar direction".
The symbol is simply how many numbers are in one word's vector. If every word is a list of 4 numbers, then .
Recall
What does each coordinate of an embedding vector represent physically? ::: A distance to walk along one axis; together the coordinates point the word-arrow in a direction that encodes its meaning.
2. Stacking word-vectors into a grid: the matrix
A sentence is several words, so it is several vectors. Stack them as rows and you get a rectangle of numbers — a matrix.

In the figure, three words become three rows; each row is one arrow from Section 1. Why the topic needs and : every shape you meet later is built from these two counts, and shape-mismatches are the number-one bug in attention code, so keep them in view.
3. The symbol and the notation
is just shorthand for "any real number" — the whole number line, positives, negatives, decimals. The blackboard-bold letter is a set: the bag of all allowed values.
Why the topic needs it: every quantity (, , , the weight matrices) will be introduced with an tag so you always know its shape before you use it.
4. Multiplying a matrix by a matrix: how works
Self-attention constantly re-expresses the grid by multiplying it with another grid . That operation is matrix multiplication, and it is the single mechanical skill you need. (We meet the specific grids and the matrices they produce in Section 5 — here we just learn the machine that combines two grids.)

Follow the highlighted row and column in the figure: three products, then one sum, lands in one output cell. Why the topic needs this: re-expressing words ( times a weight grid), scoring pairs, and mixing content are all this one operation. Master this and the formula stops being magic.
5. The learned "view-changers":
A weight matrix is a grid of numbers the model learns during training. Multiplying by it produces a new grid — a re-expressed version of the same words.
The results have names we will reuse everywhere: (the query matrix, shape ), (the key matrix, shape ), and (the value matrix, shape ).
Why three and not one? The same word plays three roles at once (asker, answerer, content-holder). Giving each role its own learned lens lets the model ask one kind of question, advertise a different kind of answer, and hand over a third thing entirely. See 3.2.05-Attention-mechanism-basics for where query/key/value first appear.
The new symbol is just the width of the query/key space (how many numbers a query has). is the width of the value space. Often where is the number of heads in 4.1.03-Multi-head-attention.
Recall
Why must every have exactly rows? ::: So the inner shapes match in : has columns, so needs rows for the multiplication to be legal.
6. Flipping a grid: the transpose
The little raised means transpose: turn rows into columns. If is , then is — the same numbers, reflected across the diagonal.
Why the topic needs it: to compute every query against every key we need shape times , giving an table — one cell per word-pair. and are both , so is illegal (inner ); transposing to makes the inner numbers () match and works.
7. Measuring "do we point the same way?": the dot product
The core question of attention is "how relevant is word to word ?". We answer it with the dot product of their vectors.

The figure shows three cases so you never meet an unshown scenario:
- Same direction (top): large positive dot product → high relevance.
- Perpendicular (middle): dot product → no relevance.
- Opposite (bottom): negative dot product → they actively disagree.
Why the dot product and not, say, subtraction? Because it rewards alignment of direction, which is exactly what "similar meaning" became in Section 1. Now we can name the whole table of these numbers.
Recall
If a query is perpendicular to a key, what is their attention score before softmax? ::: Zero — perpendicular vectors have dot product 0, meaning "no alignment, no relevance".
8. Why divide by : the scaling factor
When you add up products in a dot product, the total tends to grow as grows — more terms, bigger sums. Left alone, scores get huge and the next step (softmax) misbehaves.
Why and not ? Because the spread (standard deviation), not the count, grows like . We cancel exactly what grows. Full variance argument lives in the parent's Step 3; here just remember: scale to keep scores tame so gradients stay healthy (see 5.3.03-Gradient-flow-in-deep-networks).
9. Turning scores into a choice: softmax
We have a row of scores; we want a row of weights that are positive and add to 1 — a share-out, like slicing a pie. That is softmax.
Three properties, three reasons the topic wants exactly this:
- Always positive ( always) → weights are legal shares.
- Sums to 1 (we divide by the total) → each row is a probability distribution over words.
- Amplifies gaps → a slightly higher score becomes a clearly bigger share, letting a word "focus".
The output symbol (Greek alpha) is "how much word attends to word ". Stack all of them and you get the attention matrix , shape , every row summing to 1.
Recall
After softmax, what must each row of the attention matrix add up to, and why? ::: Exactly 1 — softmax divides by the row's total, turning scores into shares of a single pie.
10. Mixing the content: the final weighted sum
Now use the weights to blend the value vectors.
Word 's new vector is a weighted average of all value vectors: big-weight words contribute a lot, tiny-weight words almost nothing. This is the "each word rebuilds itself as a blend of others" promise from the top of the page, now made of symbols you have all defined.
The cost of doing this scales as : the scores cost (an table, each cell a -length dot product) and the mix costs . The recurring is the price of scoring every word against every word; details in 4.2.02-Computational-complexity-of-transformers.
Prerequisite map
This map shows the load-bearing order: arrows and grids at the bottom, the final self-attention output at the top. Every foundation on this page feeds a node here — and every node feeds the parent formula.
Equipment checklist
Cover the right side and test yourself.
- A vector is ::: an ordered list of numbers, drawable as an arrow whose direction encodes meaning.
- The shape means ::: words (rows), each described by numbers (columns).
- says ::: is a grid of real numbers with rows and columns — just a size label.
- To multiply matrices legally ::: the inner shape numbers must match; result is (rows of first) (columns of second).
- are ::: learned lenses turning each word into a query, a key, and a value respectively.
- (transpose) does ::: swaps rows and columns so has matching inner dimension and yields an pair-score table.
- The score matrix is ::: the grid whose cell is the dot product .
- The dot product measures ::: how aligned two vectors are — positive for same direction, zero for perpendicular, negative for opposite.
- We divide scores by because ::: the dot-product spread grows like , and we cancel that to keep softmax and gradients stable.
- The softmax max-subtraction trick does ::: subtracts the largest score before exponentiating, giving identical weights but preventing overflow/underflow.
- Softmax converts scores into ::: positive weights that sum to 1, amplifying larger scores.
- The output matrix is ::: the result , each row a word rebuilt as a weighted blend of all value vectors.