"Bank" means something different next to "river" than next to "money". Attention lets each word look at the others and borrow meaning from the ones that matter.
AI · beginner · 12 min · 18 steps · free
What you'll learn
Describe queries, keys and values
Score words with a dot product and turn the scores into weights with softmax
Mix values by their weights to get a word's new meaning
attention.py
import math
# "I sat by the river bank." Which earlier words should "bank" pay attention to?
words = ["I", "sat", "river"]
keys = [[0.1, 0.0], [0.3, 0.1], [2.0, 0.4]] # what each word offers (made-up numbers)
values = [[0.0, 1.0], [0.2, 0.8], [1.0, 0.0]] # what each word passes on: [nature, money]
query = [1.0, 0.5] # what "bank" is looking for
# 1. Score each word: how well does its key match the query? (a dot product)
scores = []
for key in keys:
scores.append(round(query[0] * key[0] + query[1] * key[1], 2))
# 2. Softmax turns the scores into attention weights that add up to 1.
powered = [math.exp(s) for s in scores]
weights = [round(p / sum(powered), 2) for p in powered]
# 3. Mix the values, each weighted by its attention.
nature = round(sum([w * v[0] for w, v in zip(weights, values)]), 2)
money = round(sum([w * v[1] for w, v in zip(weights, values)]), 2)
print("bank now means: nature", nature, "money", money)
Step by step
This brings in Python's built-in math module, for functions like math.exp and math.sqrt.
The words before "bank" in the sentence.
Each word's key: what it offers to a word that is looking. "river" has a strong first number.
Each word's value: what it passes on if chosen. Here the two numbers mean [nature, money]. "river" passes on pure nature; "I" passes on money-ish noise.
"bank"'s query: what it is looking for to decide which "bank" it is.
Step 1: score every word.
Start with "I".
Dot product of the query with I's key: 1 × 0.1 + 0.5 × 0 = 0.1. A poor match.
key now points to a different list: [0.3, 0.1].
"sat": 0.35. Still a poor match.
key now points to a different list: [2, 0.4].
"river": 1 × 2.0 + 0.5 × 0.4 = 2.2. A strong match: river is exactly what bank was looking for.
Python moves to the next item of the loop: for key in keys.
Step 2: softmax, as in the last lesson. e to each score…
…divided by the total. The weights: I 0.10, sat 0.12, river 0.78.
"bank" now means 80% nature, 20% money: the river bank, not the money bank. With "money" in the sentence instead, the same steps would tip it the other way. That is attention.
Common mistakes
Thinking attention weights are fixed: they are worked out fresh for every sentence
Mixing up keys (used to score) and values (what gets mixed)
Best practices
Read attention as a weighted average: the weights say how much each word counts
Key takeaways
Each word has a query, a key and a value.
A word scores every other word by the dot product of its query with their keys.
Softmax turns the scores into weights that add up to 1.
The word's new meaning is the weighted mix of everyone's values.
Check yourself
Which vectors are compared to make the scores?
Answer: The query with each key. The query says what a word is looking for; each key says what a word offers.
Weights are [0.5, 0.5] and values are [[1, 0], [0, 1]]. What is the mix?