DeepLLM Quiz
Neural Network Architectures

Scaled dot-product attention and multi-head attention

10 questions on the scaling factor in attention, the variance of dot products, what a head is, why several heads run in parallel and how their outputs are combined.

  • 10 questions
  • Stopwatch
  • Instant feedback

Shown publicly with your score. Don’t use your full real name.

Sample questions

Play now ↑

Answers stay hidden. Start the quiz to play them.

  1. Sample 1

    Query and key vectors have length 64, with independent elements of zero mean and unit variance. By what number should their dot products be divided to restore a spread of about one?

    • 64
    • 4
    • 8

How it works

  • One question per screen. An answer is final once sent.
  • You see right away whether you were right, with a short explanation.
  • The stopwatch runs: on equal scores, the faster player ranks first.
  • Only your best attempt counts, so replaying is never risky.

About this quiz

Two small refinements turned the attention mechanism into the version used in real transformers. The first is a division by a number so unassuming that it is easy to overlook: the square root of the length of the key vectors. The second is the decision to run several copies of the whole mechanism side by side, each free to look for something different. Both are easy to state and both have clear reasons behind them, and this quiz explains the reasons in a way you can check with arithmetic.

The scaling comes from a problem familiar from earlier in deep learning: saturation. The softmax squashes large inputs so hard that its gradients almost vanish, just as the old sigmoid and tanh units did. The inputs to the softmax in attention are dot products, and a dot product of two long vectors tends to be large. The quiz asks how large, working from the rule that the spread of such a sum grows with the number of terms, and what divisor brings it back to a comfortable size. You will calculate the divisor for a typical vector length.

Multi-head attention rests on the observation that a single set of attention weights has to settle for one pattern of attention at a time. Language has many patterns at once: some relate to tense, some to vocabulary, some to who did what to whom. One head would average these together. Several heads, each with its own learned query, key and value matrices, can specialise, rather like the many filters in one layer of a convolutional network. You will see how the outputs of the heads are glued side by side and mixed by a final matrix to return to the original width, and how the size of each head is usually chosen so that the sizes add up exactly.

There are some details that trip people up. The scaling uses the key length, not the number of tokens. Heads have separate parameters, not a shared set. And more heads do not simply multiply the cost, since each head is made smaller, so that the total width stays the same.

The quiz suits learners who have met queries, keys and values, and practitioners who configure transformer layers. It needs only simple arithmetic. There are 10 questions, each with an explanation, and it takes about four minutes. These two refinements are good examples of how deep learning designs are often justified: a small piece of reasoning about numbers, here the spread of a sum, explains a constant that looks arbitrary, and an analogy with an older architecture, the many filters of a convolutional layer, explains why parallel copies help. Both are worth carrying to other settings, since the same questions of keeping signals in a sensible range and of letting a layer look for several things at once arise in many other models.

Questions and answers

›How long does the quiz take?

About 3 to 4 minutes for 10 questions, at roughly 20 seconds each. An explanation follows every answer, with the arithmetic shown.

›Do I need to know about queries, keys and values?

It helps. The quiz on queries, keys and values introduces the three matrices and the table of scores. The questions here recall what is needed, and the new ideas, scaling and heads, are introduced from scratch.

›Why does attention scale the dot products?

The softmax has very small gradients when its inputs are large in magnitude, which slows or stalls learning, as happens with saturating activation functions such as tanh. Dot products of long vectors tend to be large, so the scores are divided by a number that reduces them to a moderate size before the softmax is applied.

›What is the right amount of scaling?

If the elements of the query and key vectors were independent random numbers with zero mean and unit variance, the variance of their dot product would equal the length of the vectors. So the standard deviation is the square root of that length, and dividing by it restores a spread of about one. That is why the divisor is the square root of the key length.

›What is an attention head?

It is one complete attention calculation, with its own query, key and value matrices, producing an output for every token. A layer with a single head must use one pattern of attention for everything. A head is the unit that can be repeated, with different parameters, to give the layer several patterns at once.

›Why use several heads?

Several attention patterns may matter at the same time. In language, some relate to tense and others to vocabulary, for example. A single head would average over these effects. Multiple heads in parallel can each pick up a different pattern, in a way analogous to using several different filters in each layer of a convolutional network.

›How are the heads combined?

The outputs of all the heads are concatenated side by side into a single wide table, which is then multiplied by one more learned matrix. This matrix brings the combined width back down to the original token size, so that the output of the layer has the same shape as its input and layers can be stacked.

›How big is each head?

The length of each head's value vectors is usually chosen as the token size divided by the number of heads, so that the concatenated table has exactly the original width. The cost of the layer is therefore similar to that of a single large head, but its capacity is spread across several patterns.

More quizzes

Queries, keys and values: giving attention something to learn10 questions on learnable query, key and value matrices, symmetry of similarity, matrix shapes, why the value size matches the input, and data-dependent signal paths.10 questionsPlay now →Attention: building each output as a weighted mix of the inputs10 questions on attention weights, why they depend on the input, soft versus hard attention, queries, keys and values, and dot-product self-attention.10 questionsPlay now →Inside a transformer layer: residual paths, the per-token network and the cost10 questions on residual connections, layer normalisation, pre-norm, the per-token MLP, why attention alone is limited, parameter counts and how cost scales with length.10 questionsPlay now →Positional encoding: how a transformer knows the order of words10 questions on permutation equivariance, adding position vectors, bounded and unique encodings, sine and cosine encodings, relative offsets and learned positions.10 questionsPlay now →Adversarial examples: how tiny changes fool image classifiers10 questions on adversarial images, the fast gradient sign method, why attacks transfer, physical attacks and why the problem is not just overfitting.10 questionsPlay now →Training recurrent networks: backpropagation through time, bottlenecks and why transformers replaced them10 questions on backpropagation through time, vanishing gradients, the bottleneck, gated units, sequential processing and the move to transformers.10 questionsPlay now →