4

Attention: cat looks back

Cat compares its question with every name tag, turns the matches into shares, and mixes the messages.

Hover or tap a word or a cell to follow it. Tap empty space to reset.

blue frame: learned weights (training changes them)green frame: numbers worked out for this sentence
1Match every question with every name tag
Module 4, step 1: Match every question with every name tag

A high number means “this word has what I'm looking for”. Words can't look at later words.

Scores = Q · Kᵀ (4×3 · 3×4 = 4×4). Grey cells = causal mask.

2Turn cat's matches into shares
Module 4, step 2: Turn cat's matches into shares

Three small moves turn any four numbers into four shares that add up to 100%.

This is softmax(scores / √d), with d = 3. The result is cat's attention weights.

3Mix the messages
Module 4, step 3: Mix the messages

Cat takes that share of each word's message, adds them up, and adds the result to its own row.

z = shares · V; h = x + z. Keeping x is the residual connection.

4Every word at once
Module 4, step 4: Every word at once

The same three moves happen for every row in one go, giving a new 4 × 3 table.

H = X + softmax(mask(Q·Kᵀ / √3)) · V. One attention head.

Real models stack many of these Our model has one attention block with one “head”. Real models run dozens of heads side by side and stack dozens of blocks, each with a few extra steps in between. The idea inside every block is the same as here. See the big picture on the course map
Cat's row now carries a bit of every earlier word: h = [−0.21, 0.81, 0.91]. Only this last row is needed to guess the next word.