Chapter C28 · Interactive Course

Attention: match, weight and mix

Query, key, value, scores, shares, and the weighted mixture.

Start chapter in web app

Core Learning Concept

Follow one attention calculation using the values supplied. Choose the position being updated, compare its query with available keys, convert scores to shares, and mix values proportionally. Trace what each part contributes carefully.

What You’ll Learn in This Chapter

  1. Start from one position

    Activity 1 of 10 · 4 min

    Identify which position is being updated in a toy attention example.

    Identify the sequence position whose representation is being updated. It receives a weighted mixture of information from allowed positions. Following a single position calculation first makes the full attention diagram straightforward to trace.

    Start activity →

  2. A query describes what to match

    Activity 2 of 10 · 4 min

    Identify the query's role in an attention operation.

    A query is a vector computed from the position being updated, indicating what features to seek. Comparing this query with candidate keys produces compatibility scores. This internal numerical representation differs entirely from an end-user’s prompt.

    Start activity →

  3. Keys provide matching information

    Activity 3 of 10 · 4 min

    Identify what the query is compared against.

    A key is a vector representing a candidate position for matching. Comparing a query against candidate keys computes compatibility scores. Keys do not carry human labels; their projection transforms are learned during training.

    Start activity →

  4. Values carry the information to combine

    Activity 4 of 10 · 4 min

    Distinguish a key's role from a value's role.

    The value is the content vector mixed into the result. While keys determine how much a position contributes, values provide what it actually contributes. Each value vector is multiplied by its calculated attention share.

    Start activity →

  5. These roles come from learned transformations

    Activity 5 of 10 · 4 min

    Recognize that query, key and value strips are computed from representations.

    Queries, keys, and values are computed by applying learned projection weights to input representations. The same position supplies three separate vectors with distinct roles, allowing the model to decouple matching criteria from transferred content.

    Start activity →

  6. Compare the query with the keys

    Activity 6 of 10 · 4 min

    Read supplied compatibility scores.

    A compatibility score measures alignment between a query and a key, commonly via dot product. Higher scores yield larger relative shares. This reflects learned numerical alignment rather than human truth or factual correctness.

    Start activity →

  7. Keep score scale manageable

    Activity 7 of 10 · 5 min

    Explain the purpose of scaled scores without deriving a formula.

    Scaling divides dot products by the square root of key dimensions to keep score sizes manageable. Without scaling, extreme scores flatten softmax gradients. Scaling stabilizes internal training without altering the core matching objective.

    Start activity →

  8. Softmax turns scores into contribution shares

    Activity 8 of 10 · 4 min

    Identify softmax's role using the shares already learned.

    Softmax converts raw compatibility scores into positive shares that sum to one. Greater scores yield larger mixing proportions. These dynamic weights are computed on the fly for each input rather than stored as parameters.

    Start activity →

  9. Combine the values

    Activity 9 of 10 · 4 min

    Trace the weighted mix to a new representation.

    The attention output is a weighted sum of value vectors across all positions. Each value is multiplied by its attention share and summed, producing a newly updated contextual vector for the target position.

    Start activity →

  10. Follow a new attention example

    Activity 10 of 10 · 6 min

    Identify query, keys and values, read supplied shares, and select the matching combined representation.

    Start activity →