101 puffs

Chapter 4 · The transformerPuff 14 of 23

Attention: which words matter

Read this: “The animal didn’t cross the street because it was too tired.” You knew at once that “it” is the animal. Change “tired” to “wide” and “it” becomes the street. A chatbot has to make the same link between words that sit far apart. How does a pile of numbers do that?

Guess first

To work out what “it” means in that sentence, which word must the model connect it to?

Play with it

Fig. 1 · Attention Where each word looks

Toy model: every word has two hand-made lists of 4 numbers, one saying what it is, one saying what it is looking for. Real models learn hundreds of numbers per word from data. The percentages are computed live from these lists.

Tap any word to see where it looks. Each word can only look back at itself and earlier words. Then swap the last word.

How it works

Tap a step to see it in the figure.

Check yourself

Swap “tired” for “wide”, and “it” now means the street. What changed inside the model?

Go deeper: the mathOne formula, and the numbers behind the figure

The formula

Put every word’s “looking for” list in the rows of a table Q (the queries), every word’s “what I am” list in a table K (the keys), and what each word hands over in a table V (the values). Then attention is one line:

Attention(Q, K, V) =
softmax(QKT)√dV

QKT holds every query matched with every key (a dot product: multiply the lists number by number, then add). d is how many numbers each list has; dividing by √d stops scores growing just because the lists are long. Softmax turns each row into percentages:

softmax(s)j = esjΣk esk

On this page the keys and the values are the same list, the word’s “what I am” list. Real models keep them separate; the next puff splits them.

With this page’s numbers

Here d = 4, so scores are divided by √4 = 2. For “tired.”:

The blend, Σ share × value, is … in the order alive, place, noun, glue. That is the new list of numbers this word carries forward.

The causal mask

A chatbot writes one word at a time, so no word may peek at words after it. Before the softmax, every score for a later word is set to −∞. Since e−∞ = 0, those words get exactly 0%. In table form, a mask M with 0 on and below the diagonal and −∞ above it is added to the scores:

softmax(QKT)√d becomes
softmax((QKT)√d + M)

The hand-made lists

Each word has two lists of 4 numbers, one per feature: alive, place, noun and glue (little linking words like “the”, “was”, “because”). A word with nothing to look for has a list of zeros, so all its scores tie and it spreads its attention evenly.

What this page simplifies

Real models learn their lists from data: typically 64 to 128 numbers per list, made from each word’s embedding by learned tables of knobs, with many attention “heads” side by side and dozens of layers stacked up. The famous animal-or-street picture comes from Google’s 2017 translation model, which read the whole sentence at once, so there “it” could look ahead at “tired”. A chatbot cannot, which is why it settles the question a few words later, as in the figure.

Sources

  1. A. Vaswani et al., “Attention Is All You Need”, 2017. The formula above, including the √d scaling and the mask.
  2. D. Bahdanau, K. Cho and Y. Bengio, “Neural Machine Translation by Jointly Learning to Align and Translate”, 2014. The first widely used attention for language.
  3. Google Research Blog, “Transformer: A Novel Neural Network Architecture for Language Understanding”, 2017. The source of the animal and street example.
  4. The word lists in the figure are hand-made for this page; the formula is the real one.