DeepLLM Quiz
Neural Network Architectures

Queries, keys and values: giving attention something to learn

10 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 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

    A sequence has 6 tokens, each with 8 features. The query and key matrices turn the table into 6 by 4 tables. What is the shape of the table of query-key scores?

    • 6 by 4
    • 4 by 4
    • 6 by 6

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

The attention mechanism in its simplest form is a clever piece of arithmetic with a flaw: it has nothing to learn. Every output is a blend of the inputs with weights computed from raw dot products, so training cannot change a single thing about it. Worse, every feature of a token counts equally in deciding which tokens are similar, even though some features obviously matter more for the task. To turn attention into a trainable layer, the designers of transformers gave each token three different views of itself, a query, a key and a value, each obtained with its own learnable matrix. This quiz explains why three, not one, and what each contributes.

You will start with the obvious fix, a single learnable matrix applied to every token before the comparison, and discover its limitation: the resulting similarity between two tokens is the same whichever way round you compute it, whereas real relationships are often lopsided. Every poodle is a dog, but few dogs are poodles. You will see how separate matrices for queries and keys remove the symmetry, and how a third matrix for the values frees the content that is passed on from the content used to decide where to look.

The questions are also about shapes, a practical skill in this subject. If a sequence has six tokens and each has a given number of features, what is the size of the table of query-key scores, and the size of the output? Which matrices have to match in size so that a dot product can be formed, and which dimension is chosen to equal the input size so that layers can be stacked and shortcuts added? You will also see why bias terms can be hidden inside the weight matrices.

Common misunderstandings include thinking that queries, keys and values are three different kinds of data, when they are three learned linear views of the same tokens. Others believe that the score table has one row per feature, when it has one row and one column per token. And some imagine that a transformer's signal paths are fixed, when attention multiplies activations by data-dependent factors.

The quiz suits learners who have met the basic idea of attention and want to see how it becomes trainable, and practitioners who read transformer code. It needs only simple counting. There are 10 questions, each with an explanation, and it takes about four minutes. Understanding these three matrices demystifies a great deal of transformer code and literature. When a paper says that a model uses a larger key dimension, a different number of heads or a smaller value size, you will know which table is being resized and what that changes in cost and capacity. You will also recognise the shape bookkeeping that every implementation has to do, and the reason that the score table grows with the square of the sequence length, which becomes the central practical limit when texts get long.

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 each answer, with the sizes of the tables worked out.

›Do I need to know attention already?

Yes, in outline. The quiz on attention basics explains how outputs are blends of inputs with weights from dot products and a softmax. This quiz begins where that one stops, so a quick visit there first is the best preparation.

›Why does unmodified attention have no capacity to learn?

Because the transformation from input vectors to output vectors uses no adjustable parameters, only the dot products of the inputs themselves. Training has nothing to change. In addition, every feature of a token plays an equal role in determining similarity, whereas the network should be able to focus on some features more than others.

›What goes wrong with a single learnable matrix?

Applying one matrix to the tokens before comparing them gives much more flexibility, but the similarity score between two tokens then comes out the same in both directions. Relationships in language are often asymmetric. Although the softmax makes the final weights unequal, a better design lets the query and key be transformed independently.

›What are the query, key and value matrices?

They are three separate learnable matrices, each applied to the input table to give a query matrix, a key matrix and a value matrix. The queries and keys are compared to produce the attention weights, and the values are what get blended. Each matrix has its own independent parameters, learned during the training of the whole transformer.

›Which sizes must match?

The query and key matrices must map each token to vectors of the same length, so that a dot product between a query and a key can be formed. The value vectors can have a different length. A common choice is to make all three equal to the original token size, which makes it easy to stack layers.

›What is the shape of the attention score table?

It has one row and one column for each token, so for N tokens it is N by N, whatever the number of features. Entry n, m records how strongly query n matches key m. Applying the softmax to each row and multiplying by the value matrix gives an output with one row per token.

›Why is it useful that attention weights depend on the data?

In an ordinary network, the weights multiplying activations are fixed. Here activations are multiplied by data-dependent coefficients. If a coefficient is close to zero for a particular input, that signal path is ignored for that input alone, whereas a standard network that learns to ignore an input does so for every input.

More quizzes

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 →Scaled dot-product attention and multi-head attention10 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 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 →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 →Bounding boxes and intersection-over-union: scoring object detection10 questions on bounding boxes, how a network predicts them, why overlap area alone is a poor score, and how intersection-over-union is computed and used.10 questionsPlay now →