Gradient

Courses

∇\nabla∇The maths behind AI0/72
π\piπMaths0/84
⋆\star⋆AI for everyone0/10
Γ\GammaΓGame theory0/49
py\texttt{py}pyPython for AI0/15
y^\hat{y}y^​Build your own AI0/15
All courses

Tuition

My tuitionOlympiad trackCompetitions

Practice

ProjectsAsk a questionTrain on a GPUExplorationsMaths StoriesUnsolved problemsPlaygroundScience labsMock testsOlympiad

Reference

GlossaryBlogDiscoveries
Blog › Explore in 3D

25 November 2026 · 3 min readInteractive 3D

Blue and white abstract artwork about neural networks
Photo by Google DeepMind on Unsplash

How does attention work in transformers?

How does a chatbot know that “it” means the cat? Each word scores every earlier word. See the whole table of scores as 3D bars.

In “The cat chased the mouse because it was hungry”, what does “it” mean? You know it is the cat. A language model works this out with attention: each word looks back at every earlier word and decides how much each one matters.

Pick a word to light up its row. Lower the temperature to make it focus.

Rows of percentages

Each row is one word looking back. Its bars are percentages that add up to 100%. Pick “it” and the tallest bar is on “cat”. Pick “chased” and attention spreads between “cat” and “mouse”, the chaser and the chased.

Where the numbers come from

  1. Each word makes a query: what am I looking for?
  2. Each word also makes a key: what do I contain?
  3. The dot product of a query and a key gives a score.
  4. Softmax turns each row of scores into percentages.

The causal mask stops a word looking at words after it, which is how a model writes one word at a time without peeking ahead.

Real models run dozens of these attention tables side by side, called heads, each learning to look for something different.

A worked example

In the sentence “The cat sat because it was tired”, the word “it” should look back at “cat”. Attention gives “it” a score for every earlier word, using the dot product of a query (what “it” is looking for) with each word’s key (what that word offers).

  1. Scores for it → The, cat, sat, because: 0.1, 2.8, 0.4, 0.2
  2. Softmax turns them into weights: about 5%, 79%, 7%, 6% (plus a little for “it” itself)
  3. The new meaning of “it” is mostly the meaning of “cat”
  4. This is one row of the attention table

Queries, keys and values

Each word’s embedding is multiplied by three learned matrices to get a query, a key and a value. Scores come from queries and keys; the output mixes the values using those scores. Dividing the scores by the square root of the key length keeps them from getting too large before softmax.

Many heads at once

A transformer runs several attention heads in parallel. One head might track which noun a pronoun refers to, another the verb that goes with a subject, another nearby words. Their outputs are combined, and the whole block is repeated dozens of times in a large model.

Attention lets every word look at every other word in one step, which is why transformers handle long sentences better than older models that read one word at a time.

Why it was a breakthrough

Older language models read text one word at a time and had to squeeze everything into a memory that faded over long sentences. Attention lets every word look directly at every other word, and all positions can be computed at once on GPUs. That made training on vast text practical and led to today’s chat models.

Glossary

  • Query: what a word is looking for.
  • Key: what a word offers to be found by.
  • Value: the information a word passes on.
  • Head: one independent attention calculation.

Try these yourself

  1. Q1. Scores 1 and 1: softmax shares? 50% each
  2. Q2. In “The girl took her bag”, “her” should attend most to? girl
  3. Q3. What do queries and keys produce? Scores
  4. Q4. Why divide by the square root of the key length? To keep scores from growing too large

In short

Attention scores every word against every other, turns scores into shares and mixes meanings. Many heads learn different relationships at once.

Keep going

  • Lesson: attention
  • Glossary: attention
  • The maths behind ChatGPT
  • What is transformer?
  • What is token?
  • What is residual connection?
  • What is convolution? How AI finds edges
  • What does a matrix actually do? Watch it stretch space
  • What is a determinant? How a matrix grows a shape

Read and listen in

Indian languages

World languages

Questions people ask

What is attention in a transformer?

A way for each word to score every other word with dot products, turn the scores into percentages, and mix in information from the words it scores highly.

What are queries, keys and values?

Three versions of each word made by learned matrices: queries ask, keys answer, and values carry the information that gets mixed.

Why do transformers use many attention heads?

Each head can learn a different kind of relationship, such as grammar or meaning, and the model combines them.

More articles

  • What is convolution? How AI finds edges
  • What does a matrix actually do? Watch it stretch space
  • What is a determinant? How a matrix grows a shape