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 are , 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: .
Steps
- 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.
- Score: compare the query with each key, usually with a dot product. A larger score means a better match.
- Normalise: softmax turns the scores into weights with . These are the attention weights.
- Mix: output . For weights over values : .
- 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
- Thinking the keys are what is returned. Keys only decide the weights; the values are what gets averaged.
- Forgetting that the weights sum to 1. If they do not, the output is not a weighted average.
- Believing attention picks one position (hard lookup). It blends all positions, which keeps it differentiable and trainable.
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?