Attention: Words Looking at Words

"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

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

  1. This brings in Python's built-in math module, for functions like math.exp and math.sqrt.
  2. The words before "bank" in the sentence.
  3. Each word's key: what it offers to a word that is looking. "river" has a strong first number.
  4. 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.
  5. "bank"'s query: what it is looking for to decide which "bank" it is.
  6. Step 1: score every word.
  7. Start with "I".
  8. Dot product of the query with I's key: 1 × 0.1 + 0.5 × 0 = 0.1. A poor match.
  9. key now points to a different list: [0.3, 0.1].
  10. "sat": 0.35. Still a poor match.
  11. key now points to a different list: [2, 0.4].
  12. "river": 1 × 2.0 + 0.5 × 0.4 = 2.2. A strong match: river is exactly what bank was looking for.
  13. Python moves to the next item of the loop: for key in keys.
  14. Step 2: softmax, as in the last lesson. e to each score…
  15. …divided by the total. The weights: I 0.10, sat 0.12, river 0.78.
  16. Step 3: mix the values. The nature part: 0.10 × 0 + 0.12 × 0.2 + 0.78 × 1 = 0.80.
  17. The money part: 0.20.
  18. "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

Best practices

Key takeaways

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?

Answer: [0.5, 0.5]. 0.5 × [1, 0] + 0.5 × [0, 1] = [0.5, 0.5].

·

More in Inside a Language Model