Attention scores one query against every key, turns the scores into weights with a softmax and mixes the values by those weights. Three small experiments show what that does, why the scores are divided by √d_k, and why a language model needs a mask. Every number comes from attention.py.
| key | q · k | ÷ √3 | weight |
|---|
A dot product of two random vectors grows like √d_k. Without the division, a wide model's scores get so large that the softmax collapses onto one key before any training has happened. Pick a width and compare.
Averages over 200 fresh random draws, so they move a little each time. The chapter's table fixes the random seed.
A language model scoring position i must not read positions after it. Turn the mask on, then compare setting the blocked scores to minus infinity before the softmax with zeroing them after.
This is Self-Attention. Run attention.py to print the same tables as text.