Attention explained: it's just a soft lookup
Grow through practice
No need to finish reading — test nowDon't be intimidated by Transformer or the line "Attention Is All You Need". Start with the smallest piece — attention. Get this, and the rest follows.
Recall something you use every day: a hash table
You know hash tables: give a key → get the value.
Attention is the "soft" version of that:
- Instead of exactly matching one key, it **computes similarity against all keys**;
- Instead of taking one value, it **returns a similarity-weighted blend of all values**.
In one line:
Attention = one "soft lookup".
Why "soft"? Because it's continuous and differentiable — so it can backpropagate and be trained. An ordinary hash table can't, and that's the whole point.
Three roles: Q / K / V
| Name | Meaning (one line) |
|---|---|
| Query (Q) | what I'm looking for |
| Key (K) | what I am (to be matched against) |
| Value (V) | my content (what gets taken) |
A minimal example
Three words: "cat sat mat". To keep it simple, represent each word as a 2-d vector (real life is hundreds of dims; same idea).
cat = [1, 0]
sat = [0, 1]
mat = [1, 1]
Simplest case: let Q = K = V = the input (this is self-attention: a sentence looking at itself). Three steps:
- Dot each word's Q against all K → similarity scores;
- softmax → weights (each row sums to 1);
- Weighted-sum all V → the output.
20 lines to run it
import numpy as np
np.set_printoptions(precision=2, suppress=True)
# three words, 2 dims each (illustrative)
x = np.array([[1., 0.], # cat
[0., 1.], # sat
[1., 1.]]) # mat
def softmax(a):
e = np.exp(a - a.max(axis=-1, keepdims=True))
return e / e.sum(axis=-1, keepdims=True)
Q = K = V = x # simplest: Q/K/V are the input itself
scores = Q @ K.T / np.sqrt(2) # similarity matrix (dot products)
weights = softmax(scores) # normalize each row into weights
out = weights @ V # weighted sum
print("weights:\n", weights)
print("output:\n", out)
How to read it: weights[i][j] = "how much word i attends to word j". So weights[1] is where "sat" is looking — whichever weight dominates, that word's info gets mixed in.
The same thing in C
The numpy version just shortens three loops. In C, the logic is identical.
1) Data
float x[3][2] = {
{1, 0}, // cat
{0, 1}, // sat
{1, 1}, // mat
};
x[i] is the 2-d vector of word i.
2) Similarity
scores = Q @ K.T / sqrt(2) is:
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
float s = 0;
for (d = 0; d < 2; d++) s += x[i][d] * x[j][d];
scores[i][j] = s / sqrtf(2);
}
It yields a 3x3 matrix:
[0.71, 0.00, 0.71]
[0.00, 0.71, 0.71]
[0.71, 0.71, 1.41]
scores[i][j] = "how similar word i is to word j". Dividing by sqrt(2) keeps the dot products from getting too large and making softmax too sharp.
3) softmax
for (i = 0; i < 3; i++) {
float m = -INF, sum = 0;
for (j = 0; j < 3; j++) m = fmaxf(m, scores[i][j]); // avoid exp overflow
for (j = 0; j < 3; j++) { w[i][j] = expf(scores[i][j] - m); sum += w[i][j]; }
for (j = 0; j < 3; j++) w[i][j] /= sum; // each row sums to 1
}
Weights:
[0.40, 0.20, 0.40]
[0.20, 0.40, 0.40]
[0.25, 0.25, 0.50]
w[1] = [0.20, 0.40, 0.40] means "sat" looks 20% at "cat", 40% at "sat", 40% at "mat".
4) Weighted sum
out = weights @ V is:
for (i = 0; i < 3; i++)
for (d = 0; d < 2; d++) {
out[i][d] = 0;
for (j = 0; j < 3; j++) out[i][d] += w[i][j] * x[j][d];
}
Output:
[0.80, 0.60]
[0.60, 0.80]
[0.75, 0.75]
Each row is a weighted average of the three word vectors — not a lookup, a blend.
Q = K = V = xis the simplest case. In real models Q/K/V are three learned projections (x @ W_qetc.), but the computation is identical.
What @, .T and shapes actually do
@ is matrix multiply, not elementwise multiply
In numpy, A * B is elementwise; A @ B is matrix multiply:
C[i][j] = Σ_k A[i][k] * B[k][j]
.T is transpose
K is [3,2] (3 words x 2 dims); K.T is [2,3]:
K = [[1,0],[0,1],[1,1]]
K.T = [[1,0,1],[0,1,1]]
The relation is K.T[d][j] = K[j][d].
Why transpose
You want the dot product of word i and word j:
Qis[3,2];K.Tis[2,3];Q @ K.Tis[3,2] @ [2,3] = [3,3].
[3,2] @ [3,2] is invalid (inner dims 2 vs 3), so the transpose is required. Expanded:
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
float s = 0;
for (d = 0; d < 2; d++)
s += Q[i][d] * K[j][d]; // same as Q[i] · K.T[:,j]
scores[i][j] = s / sqrtf(2);
}
A concrete number
scores[2][0] = mat · cat:
[1,1] · [1,0] = 1
1 / sqrt(2) = 0.71
scores[2][2] = mat · mat:
[1,1] · [1,1] = 2
2 / 1.414 = 1.41
The later weights @ V
[3,3] @ [3,2] = [3,2]:
out[i][d] = Σ_j weights[i][j] * V[j][d]
It blends the three word vectors by weight.
Just remember three things
- Attention = a soft lookup: dot-product similarity → softmax weights → weighted sum;
- Q / K / V: what I'm looking for / what I am / my content;
- Because it's differentiable, it can be trained — that's what beats a plain lookup table.
Next: why multi-head? And why attention is "order-blind" (so we need positional encoding).
Comments 0
No comments yet — be the first!