VisualAI
Chapter 3 · Language Models

Attention

The idea behind the transformer: every word gets to decide which other words matter.

10 minLesson 7 of 23

This is the idea that unlocked ChatGPT. It has an intimidating name — the transformer — but the core move is something you do instinctively when you read: every word glances at the others and decides which ones matter.

We can turn words into vectors now, but a word's meaning still depends on its neighbours. Read this: “The cat sat on the mat because it was tired.” What does itrefer to — the cat or the mat? You knew instantly, because you let “it” look back at the rest of the sentence. That glancing-around is attention, and it's a thing a model can compute.

Attention Visualizer

Click a word to make it the one that's 'looking'

Click any word to make it the query — the other words light up by how much it pays attention to them.

it attends most to cat. The small numbers are attention percentages.

Attention sharpness (temperature)1.00

Low = lock onto one word. High = spread attention thinly across everything.

A real transformer computes these weights from the words. Here they're set to show the patterns a trained model actually learns.
Try this

On the first sentence, click it — it lights up cat, not mat. Now the mind-bender: switch to the two trophy / suitcasesentences. They're identical except for one final word — big vs small— and watch “it” flip which object it attends to. The model resolves the meaning purely from context. Slide sharpness to see attention focus or blur.

How a word “looks”

Each word produces two things from its vector: a query(“what am I trying to understand?”) and a key (“here's what I am”). To decide how much word A should attend to word B, the model compares A's query with B's key — a strong match means high attention. Every word does this for every other word, and the matches are squashed into the percentages you see. Because each word attends to the others in the same sentence, it's called self-attention.

Key idea

Attention lets every word reach out and pull in meaning from the other words that matter most to it, ignoring the rest. Stacking layers of this is the transformer — the architecture behind essentially every modern language model. The 2017 paper that introduced it was titled, fittingly, “Attention Is All You Need.”

Real models run many attention patterns in parallel — different “heads” that each learn a different kind of relationship (one tracks who-did-what, another matches adjectives to nouns, another follows pronouns like our “it”). And they stack dozens of these layers, so context builds up richer and richer as it flows through.

The payoff

Now you have every ingredient: words as vectors (Lesson 6), attention to mix in context (this lesson), and a deep neural network trained by gradient descent (Lessons 3–5). Wire them together, train the whole thing to do one absurdly simple task — guess the next word — over a good chunk of the internet, and you get a large language model. In the finale, you become the model.