In attention, what do the query, keys and values stand for, and how is the output a weighted average? If the attention weights over three values 10,20,30 are 0.7,0.2,0.1, what is the output?

Sequence models and transformers, Attention
Level L1 Beginner. Length Short. Nature Th+Mat Both.

What's being asked

Whether you understand attention as a soft lookup before you meet matrices, masks or heads.

Key idea

Attention is a soft lookup. A query says what I am looking for. Each position offers a key (a label saying what it holds) and a value (what it will hand over). Compare the query with every key to get a score, turn scores into weights that sum to 1 with softmax, and return the weighted average of the values: output=∑iwivi.

Steps

  1. Use a library picture: your query is the topic you want, each book has a label (key) and contents (value). An ordinary lookup picks the one book whose label matches. Attention instead reads a bit of every book, more from the better matches.
  2. Score: compare the query with each key, usually with a dot product. A larger score means a better match.
  3. Normalise: softmax turns the scores into weights wi≥0 with ∑iwi=1. These are the attention weights.
  4. Mix: output =∑iwivi. For weights 0.7,0.2,0.1 over values 10,20,30: 0.7×10+0.2×20+0.1×30=7+4+3=14.
  5. Interpret: 14 is close to 10 because the query matched the first key most. In self-attention the queries, keys and values all come from the same sequence, so every word can gather information from every other word.

Common mistakes

How long your answer should be

About 2 minutes: the library picture, the three-step recipe and the 14 example.

Read this

Previous in Sequence models and transformers: When we train a model to write a sentence one word at a time, what does it mean to feed it the correct previous words during training, and why might that cause trouble later?
Next in Sequence models and transformers: Why does a transformer need positional encodings?