In scaled dot-product attention, why divide by , where and are the query and key matrices and is the key dimension?
Sequence models and transformers, Attention
Level L3 Intermediate. Length Short. Nature Th+Mat Both.
What's being asked
Whether you can do a quick variance calculation and connect it to softmax saturation, where softmax gets stuck near a one-hot output.
Key idea
Take one query and one key . If their components are independent with mean 0 and variance 1, then has variance . Dividing by keeps the logits at unit variance, so softmax does not saturate and gradients do not vanish.
Steps
- Write , where is the value matrix.
- Compute under the independence assumption. Each term has variance 1, and the variances of independent terms add.
- Explain that large logits push softmax towards a one-hot output, where its gradient is nearly zero.
- Conclude that the scaling keeps training stable as grows.
Common mistakes
- Saying it is to “normalise the output” or keep values in .
- Not knowing why it is and not . The standard deviation, not the variance, sets the scale.
How long your answer should be
About 1 minute; one line of variance and softmax saturation.
Read this
Previous in Sequence models and transformers: A single-layer LSTM has input size and hidden size . How many trainable parameters does it have (one bias vector per gate), and how does the count compare with a GRU and a plain RNN of the same sizes? What changes if the LSTM is bidirectional?
Next in Sequence models and transformers: For a vanilla RNN with loss , how does backpropagation through time give for , and why do gradients over long time spans vanish or explode?