Compute a tiny attention score matrix entry
Type the indices of each value in their original order. · 178 words
Attention scores measure how strongly a query and key match. The model projects token vectors into q and k. For vectors of length d, a common raw score is their dot product: q dot k = sum q_j k_j. A larger score means a stronger match. Because long vectors can produce large dot products, transformers usually divide the score by the square root of d. This keeps the scale steadier before softmax turns each score row into weights. A score matrix has one row per query token and one column per key token.
Suppose four-dimensional vectors are available for 'The blue mug is on the shelf.' For query token 'mug', let q=[1,0,1,0]. For key token 'blue', let k=[1,1,0,0]. Their dot product is 1*1 + 0*1 + 1*0 + 0*0 = 1. Since d=4, divide by sqrt(4)=2, so the score is 1/2=0.5. Place 0.5 in the 'mug' row and 'blue' column. For comparison, key 'shelf' has k=[0,1,0,1]. The same query gives a dot product of 0, so that score is 0.0. Scores are not yet weights; softmax comes next.
Check your recall
What is the scaled attention score for q=[1,0,1,0] and k=[1,1,0,0] when the vector length is 4?
Check your answer
The dot product is 1, and sqrt(4)=2, so the scaled score is 1/2, or 0.5.
Sources and credits
Original KeyTouch lesson. Background reading · Vaswani et al. · Attention Is All You Need.