Queries, keys and values
Explain the roles of Q = XW_Q (what I'm looking for), K = XW_K (what I contain) and V = XW_V (what I pass on if attended to). Compute them and their dot-product match scores for a toy sequence.
Builds on: Why positions must exchange information
What the animation says
One score, three jobs
Last time, each position took a weighted average over the others, with weights set by content. But where do those weights come from? The obvious score is the dot product of two embeddings. Take the pronoun it. It is looking for a noun, but it is not a noun itself. So, with plain embeddings: how does the score of it on cat compare with cat on it?
Query, key, value
Always equal. A plain dot product can't tell seeking apart from offering. So each position makes three vectors from its own embedding, each with its own learned matrix. The query says what I'm looking for. For it: a singular noun earlier. The key advertises what I contain. For cat: I'm a singular noun. The value is what I hand over if someone attends to me. For cat: its meaning. It's like a search where every document gets a relevance score. Tap the vector of cat that the query of it is compared with.
All positions at once
Stack the embeddings into a matrix X, one row per position. Multiply X by three learned matrices to get Q, K and V. The same three matrices serve every position. Their width, called d head, is a design choice, often smaller than the embedding size. Row t of Q is the query of position t. Same for keys and values. The score of position t on position j is query t dotted with key j. All pairs at once: Q times K transposed, a T by T grid of scores.
Toy: scores by hand
A toy: three positions, two dimensions. X has rows one zero, zero one, and one one. The key matrix is the identity, so the keys are just X. The query matrix moves the first coordinate into the second slot. Queries: zero one, zero zero, zero one. Row one: query one against each key gives zero, one, one. Query two is all zeros, so row two is zero, zero, zero. Row three repeats row one. Position one scores position two at one. What score does position two give position one?
Values wait their turn
Zero. Position one seeks the second coordinate, and position two's key has it. Position two seeks nothing. Plain embeddings would give a symmetric grid. Separate query and key matrices break that symmetry. In numpy, these lines build Q, K and V, and this line computes the scores. The scores use only queries and keys. Values wait until the weighted average. Tap the matrix that can't change any score.
Three learned roles per position
The formulas
Toy: T = 3, d = $d_{\text{head}}$ = 2
Same toy in numpy
import numpy as np
X = np.array([[1,0],[0,1],[1,1]], float) # [T, d]
W_Q = np.array([[0,1],[0,0]], float)
W_K = np.eye(2) # identity matrix
W_V = np.array([[1,0],[0,2]], float)
Q, K, V = X @ W_Q, X @ W_K, X @ W_V # each [T, d_head]
S = Q @ K.T # [T, T]: S[t, j] = q_t . k_j
print(S) # rows: [0,1,1], [0,0,0], [0,1,1]
print(V) # what each position would pass on
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses