← Back to modules

Attention Mechanisms — Core

Why attention replaced recurrence, scaled dot-product attention, Q/K/V, multi-head attention, self vs cross attention, causal masking, positional encoding, and the KV cache.

Medium30 questions

Sample questions

1

What core limitation of RNNs did attention mechanisms overcome for long sequences?

  • RNNs required far too many parameters, which attention reduced by sharing a single weight matrix across all timesteps
  • RNNs could only process fixed-length inputs, while attention was the first mechanism able to accept variable-length text
  • RNNs compress the whole past into one fixed-size state, so distant information decays and processing stays sequential
  • RNNs demanded labeled alignment data for every token pair, which attention removed by using unsupervised pretraining

Why

A recurrent network folds everything it has seen into a single fixed-size hidden state and updates it one step at a time, so information from an early token must survive many nonlinear updates to influence a later one, and gradients through those updates shrink, causing the vanishing-gradient problem. This both limits how far dependencies can reach and forces strictly sequential computation that cannot use the parallelism of GPUs across the sequence. Attention fixes this by letting any position read directly from any other position in a single step, giving constant path length and full parallelism. The too-many-parameters option misdiagnoses the problem: parameter count was not the core issue, and RNNs already share weights across timesteps. The fixed-length-input option is wrong, since RNNs handle variable-length sequences natively. The labeled-alignment option is wrong because RNNs do not require token-pair alignment labels, and attention weights are learned, not supervised. The real breakthrough was direct, learned all-to-all connections that remove the sequential bottleneck.

2

In scaled dot-product attention, what do the query and key vectors represent?

  • The query is the token's final output and the key is its gradient during the backward training pass
  • The query and key are identical copies, duplicated only so the attention matrix comes out perfectly symmetric
  • The query stores the token's position and the key stores its embedding, keeping content and order in separate tensors
  • The query encodes what a token is looking for and the key encodes what another token offers for matching

Why

Attention projects each token into a query, a key, and a value. The query vector encodes what the token is looking for, the key vector encodes what a token offers as a match, and their dot product measures compatibility, so a high query-key score means one token should attend to another. The output-and-gradient option is wrong: the query is an input-side projection used to compute scores, not an output or a gradient. The identical-copies option is wrong and misses the whole point of separating Q and K, which lets attention be asymmetric so token A can attend to B without B attending equally to A. The position-and-embedding option confuses query and key with positional encoding; both Q and K are content projections, and position is injected separately. Keeping query and key as distinct learned projections is what lets the model represent directional relationships, such as a verb attending to its subject. The value projection then carries the actual content transmitted when a token is attended to.

3

Why are attention scores divided by the square root of the key dimension before the softmax?

  • To convert the raw scores into probabilities, a job the scaling factor performs instead of the softmax function
  • To reduce the parameter count of the query and key projection matrices as the head dimension grows larger
  • To keep dot-product magnitudes from growing with dimension, which would saturate softmax and vanish gradients
  • To guarantee the attention weights are always exactly uniform across positions regardless of the input content

Why

A dot product between a query and key is a sum of d_k component products, so its variance grows with d_k and the raw scores spread to large magnitudes as the head dimension increases. Feeding large logits into softmax pushes it toward a near one-hot distribution where gradients are almost zero, which stalls learning. Dividing by the square root of d_k rescales the scores back to roughly unit variance, keeping softmax in a responsive, high-gradient range. The convert-to-probabilities option is wrong because softmax, not the scaling constant, produces the probability distribution; scaling only adjusts magnitudes. The reduce-parameters option is wrong: the scale is a fixed constant, not a learned parameter, and it changes no matrix shapes. The always-uniform option is wrong because scaling does not force uniform weights; the model still learns sharp, content-dependent attention. This variance-control trick is why dot-product attention needs the scale while additive attention, bounded by a tanh, does not.

4

After the softmax in attention, what does each output vector for a token actually contain?

  • The single value vector belonging to whichever position received the highest attention weight for that token
  • The elementwise product of the query and key vectors, passed forward without any of the value vectors
  • The concatenation of every key vector in the sequence, later compressed back down by the feed-forward layer
  • A weighted average of all value vectors, weighted by that token's attention distribution over positions

Why

The softmax turns each token's scores into a probability distribution over positions, and the output for that token is the weighted sum of all value vectors using those probabilities as weights. So the output blends content from across the sequence in proportion to how well each key matched the query. The single-value option is wrong because attention is soft: even the top position rarely gets all the weight, and the output mixes multiple values rather than copying one. The query-times-key option is wrong because the values, not the queries and keys, form the output; Q and K only determine the weights. The concatenate-keys option is wrong because keys are used to score, not to build the output, and there is no such concatenation step. This weighted-average-of-values structure is what lets a token pull in information from wherever it attends, and the sharpness of the distribution controls how focused that mixing is. In a trained model these distributions are often concentrated on a few positions.

5

Why does the Transformer use multiple attention heads instead of a single large one?

  • To increase total attention compute several fold, since more heads always multiply the floating-point work required
  • To store a separate copy of the full sequence in each head so the model has redundant backups against data loss
  • To remove the need for positional encoding, because each head can infer token order from its own private index
  • To let different heads learn different relationships in parallel, such as syntax, coreference, or positional adjacency

Why

A single attention head learns one projection of queries and keys, which defines one notion of which tokens are related, so it cannot simultaneously capture, say, subject-verb agreement and long-range coreference. Multi-head attention runs several attention operations in parallel, each with its own projections, so different heads can specialize in different relationship types, and these roles emerge from training rather than being assigned. The increase-compute option is wrong: the model dimension is split across heads, so multi-head attention costs about the same as one head on the full dimension. The redundant-backups option is wrong because heads do not store copies of the sequence; they compute different views of the relationships. The remove-positional-encoding option is wrong because heads do not encode order on their own, and position must still be injected explicitly. The payoff of multiple heads is representational: several distinct similarity metrics operating at once, later recombined by the output projection.

Free account

Take the full module

These are the first few of 30 questions. A free account opens the rest as a scored drill.

  • Every question in this module
  • Instant feedback and supporting reading
  • Your score and progress, saved

Free · your email is used for progress only.