Skip to content
AI360Xpert

Modern ML

Attention

Attention is not a metaphor. It is a matrix of real numbers saying how much each token reads from each other token, and every row of it sums to exactly one.

Attention is not a metaphor. It is a matrix of real numbers saying how much each token reads from each other token, and every row of it sums to exactly one.

Stage 1 of 4: Tokens

Focused token is bank. Temperature is 1.00.

  • Token
  • Focused token
Row bank sum4.70Row bank sum: 4.70
Temperature1.00Temperature: 1.00

A short sequence. Each token carries a vector; nothing is connected yet.

Check your understanding

4 questions in the bank. Each attempt draws a fresh set in a fresh order, so a second go is a real second go.

What you are looking at

The core of a Transformer model, visualized two ways. On the top is a sequence of tokens, representing words or subwords in a sentence. Below it is the attention matrix, where the row represents the token doing the reading, and the column represents the token being read from.

Language models don't process words sequentially left-to-right. They process them all at once. To figure out what a word means in context, it needs to look at the other words around it. The attention matrix dictates exactly how much information flows between any two words.

The budget

Notice that in the Weights stage, every row in the matrix sums to exactly 1.0.

This is the most critical structural constraint of attention: it is a fixed budget. A token cannot simply pay 100% attention to every other token. If a token needs to pay more attention to the word "bank", it is mathematically forced to pay less attention to the word "river".

This constraint is enforced by the softmax function applied to each row. It takes the raw, unbounded compatibility scores and squeezes them into a probability distribution.

Asymmetry: Reading vs Being Read

Attention is directional. If token A pays a lot of attention to token B, it does not mean token B pays a lot of attention to token A.

You can see this asymmetry in the matrix: the cell at (Row B, Col A) might be very different from the cell at (Row A, Col B). In the graph, the edges show where the focused token is reading from, governed by its specific row in the matrix.

This asymmetry exists because tokens don't just have one representation. They are split into a Query (what I am looking for) and a Key (what I contain). Token A's Query might strongly match Token B's Key, but Token B's Query might be looking for something entirely different.

The Temperature dial

The slider controls the softmax temperature, which divides the raw scores before they are normalized. This single parameter completely changes the behavior of the network.

At a high temperature (flattening), the differences between the raw scores shrink. The softmax function returns values that are nearly uniform. Every token pays equal attention to every other token. The matrix washes out, the edges all look the same, and the mechanism loses its ability to select specific information.

At a low temperature (sharpening), the differences are exaggerated. Softmax acts like a harsh winner-takes-all gate. Each token pays 99% of its attention to exactly one other token, ignoring the rest entirely.

What to take away

"Paying attention" in a neural network is just taking a weighted sum of vectors. The attention matrix provides those weights. By forcing the weights to sum to 1, the network learns to make hard choices about which contextual clues matter most.

Reference

Scores
S = Q·Kᵀ / √d — the √d keeps the scores from growing with dimension
Weights
A = softmax(S / T), applied row by row
Softmax
aᵢ = exp(sᵢ/T) / Σⱼ exp(sⱼ/T)
Row sum
every row of A sums to exactly 1 — attention is a budget
Output
each token’s output is A·V, a weighted blend of value vectors
T → ∞
uniform attention: the mechanism selects nothing
T → 0
one-hot attention: each token reads from exactly one other

Break it on purpose

Raise the temperature and the softmax flattens: every token attends equally to every other, the matrix goes uniform, and the mechanism stops selecting anything. Drop it toward zero and it collapses the other way — each token reads from exactly one other and ignores the rest.