Why Divide by √d_k

Why attention divides its scores by a square root, and what breaks if it does not.

🟡 intermediate4 min readtransformers
Attention scores how well two words match by multiplying their vectors together and adding up the result. The longer those vectors are, the bigger the scores tend to get. Very large scores push the softmax step to extremes, where one word gets nearly all the attention and the gradients almost vanish, so learning stalls. Dividing the scores by the square root of the vector length keeps them in a sensible range, whatever size the model is, so training stays healthy.

What is Scaled Dot Product Attention?

In the Transformer architecture, attention is calculated using:

Attention(Q,K,V)=Softmax(QKTdk)V\text{Attention}(Q, K, V) = \text{Softmax}\left( \frac{Q K^T}{\sqrt{d_k}} \right) V

Why do we explicitly divide by dk\sqrt{d_k}?

Why not use simple unscaled dot product attention Softmax(QKT)V\text{Softmax}(Q K^T) V?

The Mathematical Cause: Exploding Dot Product Variance

Assume Query vector qq and Key vector kk are dkd_k dimensional vectors whose components are independent random variables with mean 0 and variance 1.

The dot product is the sum of dkd_k individual products:

q⋅k=∑i=1dkqikiq \cdot k = \sum_{i=1}^{d_k} q_i k_i
  1. Mean of q⋅kq \cdot k = 00.
  2. Variance of q⋅kq \cdot k = dkd_k.

As key dimension size dkd_k grows larger (for example, dk=64d_k = 64 or dk=128d_k = 128), the variance of the dot product grows linearly to 64 or 128.

This means dot product values frequently reach large positive or negative numbers like +30+30 or −40-40.

The Softmax Derivative Trap

Look at what happens when Softmax receives large input values:

  Unscaled Inputs to Softmax:    [ +35.0,  -20.0,  +2.0 ]
  Softmax Probabilities:         [ 0.999999,  0.000000,  0.000000 ]  (Extremely Sharp Peak!)

When Softmax outputs extreme probabilities near 1.0 and 0.0:

Softmax local derivative approaches ZERO.

During backpropagation, multiplying upstream gradients by near zero derivatives causes Vanishing Gradients, stopping the model from updating early weights.

The Scaling Fix: Dividing by dk\sqrt{d_k}

To restore variance back to 1.0, we divide the dot product by dk\sqrt{d_k}:

Var(q⋅kdk)=1dkVar(q⋅k)=dkdk=1.0\text{Var}\left( \frac{q \cdot k}{\sqrt{d_k}} \right) = \frac{1}{d_k} \text{Var}(q \cdot k) = \frac{d_k}{d_k} = 1.0

Scaling by 1dk\frac{1}{\sqrt{d_k}} keeps dot product magnitude stable around 1.0 regardless of how large key dimension dkd_k grows.

This keeps Softmax inputs in a smooth, well calibrated region where gradients remain strong and healthy during training.

Say this out loud

Scaled Dot Product Attention divides Query Key dot products by the square root of key dimension size d_k. As vector dimension d_k grows large, dot product variance increases linearly to d_k, pushing Softmax into extreme regions where gradients vanish. Scaling by 1 / sqrt(d_k) restores variance to 1.0, keeping Softmax gradients healthy during backpropagation.

Followups to expect

  1. What is Additive Attention (Bahdanau Attention)? Computes similarity using a feedforward layer v_a^T tanh(W_q q + W_k k). It handles large dimensions without scaling issues but is much slower on GPUs than Scaled Dot Product matrix multiplication.
  2. What happens if d_k is very small (like d_k = 4)? Scaling has minimal effect because dot product variance is already small. Scaling becomes critical as d_k scales to 64, 128, or 256.

Check yourself

Question 1 of 3

Why does unscaled dot product attention Q K^T cause vanishing gradients when key dimension d_k is large (such as d_k = 128)?

More in NLP & Transformers

See all →
The Attention Mechanism5 minTransformer Architecture5 minTokenization & BPE5 min