Skip to main content

Exercises — Scaled dot-product attention

Self-check questions, exercises, and worked answers

← Back to the lesson: Scaled dot-product attention

Check your understanding

Full calculation: For a complete calculation of Q/K scores, scaling, softmax, and the weighted output, use the guided exercise in the Attention collection. You can also browse the full Attention collection, which contains the more advanced exercises.

  1. A row of scores is \((2,0)\) and the key dimension is \(d_k=4\). Calculate the softmax weights after scaling.
Show the reasoning

First divide the scores by \(\sqrt{4}=2\), giving \((1,0)\). Softmax is approximately \((0.731,0.269)\). Scaling precedes normalization; the weights remain relative to the scores in this row only. The variant where one position is forbidden by a causal mask is covered in the self-check on causal masking.

  1. A position receives an attention weight of \(0.95\). Can we conclude that it makes the largest contribution to the output or is the primary cause of the prediction?
Show the reasoning

No. Before the output projection, its direct contribution is \(0.95v_j\): its norm also depends on \(\lVert v_j\rVert\), not only on the weight. The output projection, residual connections, normalization, and MLP then transform the signal further. A high weight is a relative aggregation weight, not a measure of causal importance. Studying causal effect requires an appropriate intervention-based analysis.

Weighted mixture or selection?

After the softmax of one attention row, the weights are \((0.95,\,0.05)\) over two positions, with \(v_1=(10,0)\) and \(v_2=(0,20)\).

  1. A colleague claims that attention “selects position 1 and ignores the rest.” Compute the output and state whether it equals \(v_1\).
  2. Which weight distribution would return exactly \(v_1\)? Can the softmax of a finite score row ever produce exactly that distribution?
  3. In which limiting case does attention approach selection, and why is that not the general behavior?
Show the reasoning
  1. The output is \(0.95(10,0)+0.05(0,20)=(9.5,1)\): it does not equal \(v_1\). Even with a weight of \(0.95\), position 2 transmits \(5\,\%\) of its value vector; the output is a weighted mixture, not a selection.
  2. Only the distribution \((1,0)\) would return exactly \(v_1\). Softmax over a finite score row produces strictly positive weights, so it can never reach exactly \((1,0)\), only approach it.
  3. When one score vastly dominates the others after scaling, the weights approach a one-hot vector and the mixture approaches selection. That is a limiting case: in general weights are not close to one-hot and the other positions retain nonzero contributions. Weights remain mixing coefficients, not selection decisions.

Language: Français · English