Attention

To understand a sentence, a model has to figure out how the words relate. Read the sentence "the animal didn't cross the street because it was tired." What does "it" refer to? Almost certainly the animal, not the street, because streets do not get tired. You resolved that by letting "it" look back at the other words and deciding which one matters. Attention is how a transformer does the same thing.

Every word looks at every other word

For each word, attention asks a simple question: to understand this word, how much should I focus on each of the other words? The answer is a set of weights, one per word, showing how much focus each word gets. A high weight means "this other word is important for me right now," a low weight means "I can mostly ignore it."

Crucially, every word does this for every other word, in both directions and all at once. That is what lets a model link "it" to "animal" even though several words sit between them. Distance in the sentence does not matter; only the learned relationship does.

Transformers

Attention in "the animal didn't cross the street because it was tired"

The numbers are hand-set to make the lesson clear and do not add up to 1 the way a real model's would.

theanimaldidn'tcrossthestreetbecauseitwastiredthethe to animal: 0.45the to didn't: 0.00the to cross: 0.00the to the: 0.00the to street: 0.00the to because: 0.00the to it: 0.00the to was: 0.00the to tired: 0.00animalanimal to the: 0.60animal to didn't: 0.40animal to cross: 0.50animal to the: 0.00animal to street: 0.30animal to because: 0.00animal to it: 0.45animal to was: 0.00animal to tired: 0.30didn'tdidn't to the: 0.00didn't to animal: 0.45didn't to cross: 0.45didn't to the: 0.00didn't to street: 0.00didn't to because: 0.00didn't to it: 0.00didn't to was: 0.00didn't to tired: 0.00crosscross to the: 0.00cross to animal: 0.70cross to didn't: 0.50cross to the: 0.40cross to street: 0.80cross to because: 0.00cross to it: 0.00cross to was: 0.00cross to tired: 0.00thethe to the: 0.00the to animal: 0.00the to didn't: 0.00the to cross: 0.45the to street: 0.45the to because: 0.00the to it: 0.00the to was: 0.00the to tired: 0.00streetstreet to the: 0.00street to animal: 0.35street to didn't: 0.00street to cross: 0.70street to the: 0.60street to because: 0.00street to it: 0.25street to was: 0.00street to tired: 0.00becausebecause to the: 0.00because to animal: 0.30because to didn't: 0.00because to cross: 0.40because to the: 0.00because to street: 0.00because to it: 0.50because to was: 0.00because to tired: 0.70itit to the: 0.08it to animal: 1.00it to didn't: 0.08it to cross: 0.15it to the: 0.08it to street: 0.20it to because: 0.15it to was: 0.35it to tired: 0.50waswas to the: 0.00was to animal: 0.00was to didn't: 0.00was to cross: 0.00was to the: 0.00was to street: 0.00was to because: 0.00was to it: 0.45was to tired: 0.45tiredtired to the: 0.00tired to animal: 0.55tired to didn't: 0.00tired to cross: 0.00tired to the: 0.00tired to street: 0.00tired to because: 0.35tired to it: 0.70tired to was: 0.50
weakmediumstrong
Loading 3D scene…
Click a word to see what it attends to
words
10
selected
—
top links
—

Reading the figure

Click a word to see its attention as links to the rest of the sentence. Thicker, brighter links mean stronger attention. Click "it" and the strongest link snaps back to "animal," with only a faint link to "street." That is the model resolving what "it" stands for. Try "cross" and you will see it lean on "street" and "animal," the words that say what is being crossed and who is crossing.

The weights here are hand-set to make the lesson clear. A real model is not told that "it" means "animal." It learns weights like these from data, and it computes them on the fly from each word using learned query, key, and value vectors, usually across many attention heads at once, each head watching for a different kind of relationship. In the grid above, each cell's shade is the relative strength of one word attending to another, brightest for the strongest link.

Why it matters

Attention is the core mechanism of the transformer, the architecture behind modern language models. Before attention, models read text strictly in order and tended to forget what came far earlier. Attention removed that limit: any word can draw on any other word directly, no matter how far apart, which is what let these models handle long passages and capture the relationships that make language mean something.

What the demo shows

Each point is a word of the example sentence. Drag to orbit, scroll to zoom, and click a word to draw its attention links to the others, weighted by strength. The "it" to "animal" link is the headline case, hand-set so the idea of attention resolving a reference is easy to see.

Query, key, and value

So far you have seen that words attend to each other with different strengths, but not where those strengths come from. The mechanism is query, key, and value. Each word starts with its embedding and produces three vectors from it: a Query, representing what this word is looking for, a Key, representing what it offers to other words, and a Value, the information it passes along if another word attends to it.

To decide how much one word attends to another, the model compares the first word's Query against the second word's Key. It does this with a dot product, which is just a single number measuring how well the two vectors line up. A bigger number means a closer match.

But raw match numbers are not weights yet. Two things happen to turn them into weights. First, each match is divided by the square root of the vector size. This scaling keeps the numbers from getting too large, which would make the next step too extreme. Then all the scaled matches go through softmax, which turns a list of numbers into a set of weights that are all positive and add up to 1. Softmax also exaggerates the gaps: a slightly higher match becomes a clearly higher weight, so the strongest match stands out.

This is the real formula from the transformer paper: attention weights are softmax of the Query-Key match, scaled by the square root of the key size.

Try it in the figure. Click "it" and watch where its attention goes. Its strongest weight lands on "animal," which is what "it" refers to in the sentence. The model has no grammar rules telling it this. It falls out of the Query and Key vectors and the softmax over their matches.

The attention weights here are the real computation: a Query-Key dot product, scaled by the square root of the vector size, then softmax, exactly the transformer paper's formula. What is hand-authored is the Query, Key, and Value vectors themselves. A real model learns those from data over billions of words; here they are set by hand so the example is small enough to read. The math on top of them is genuine.

Transformers

Query, key, and value, step by step

wordQ queryK keyV valueanimalstreetittired

Compare my Query to your Key; the better the match, the more of your Value I take.

The Q, K, and V vectors are hand-authored to illustrate the mechanism. A real model learns them from each word and computes the matches on the fly.

Pick a word to use its Query
picked query
—
top matches
—