How does attention work, without the maths overload

3 min read

Attention is how a model decides which words matter to each other. Every word looks at every other word in the sentence and picks up the ones that change its meaning. That is the whole trick. Everything else in a transformer is built around it.

Think of it like listening in a noisy room. Ten people are talking, but you only really hear the two whose words change what you are about to say. You tune the rest out. Attention does the same thing with words. It turns the volume up on the words that matter and down on the ones that do not.

I ran a tiny attention calculation to show you real numbers. The sentence is "the cat sat on the mat". Each word gets a small list of numbers, and attention is the score of how much each word listens to each other word. Here is the heatmap, rows are the word listening, columns are the word being listened to.

thecatsatonthemat
the0.160.170.170.170.160.17
cat0.140.240.180.150.140.15
sat0.140.180.250.140.140.15
on0.140.150.150.210.140.22
the0.160.170.170.170.160.17
mat0.130.140.150.210.130.23

Read the "sat" row. It listens to itself most, 0.25, and to "cat" next, 0.18. That makes sense. "Sat" is the action, and "cat" is the thing doing it. Read the "mat" row. It listens to "on" at 0.21, because "on the mat" is one place. The numbers are small, but the pattern is the point. Every word listens to itself first, then to the words it relates to.

Here is what "sat" hears, as a chart.

What the word 'sat' listens to
the0.1attention cat0.2attention sat0.3attention on0.1attention the0.1attention mat0.1attention
Data from the sources listed below.

The code that made those numbers is short. Each word is a list of numbers. You multiply the lists together to get scores, then squash the scores so they add up to one. That squashing is called softmax, and it is the only formula you need to know.

python
import numpy as np
words = ["the", "cat", "sat", "on", "the", "mat"]
E = np.array([
    [0.1, 0.1, 0.1],
    [1.0, 0.2, 0.1],
    [0.4, 1.0, 0.1],
    [0.1, 0.1, 0.9],
    [0.1, 0.1, 0.1],
    [0.1, 0.2, 1.0],
])
scores = E @ E.T / np.sqrt(3)
e = np.exp(scores - scores.max(axis=1, keepdims=True))
attention = e / e.sum(axis=1, keepdims=True)
print(attention[2])

Real models do the same thing with thousands of numbers per word and hundreds of attention heads, each one listening for a different kind of relationship. One head might listen for who did what. Another might listen for which words belong together. The idea never changes. It is just listening, at scale.

Try this

bash
pip install numpy
python3 -c "import numpy as np; E = np.array([[0.1,0.1,0.1],[1.0,0.2,0.1],[0.4,1.0,0.1]]); s = E @ E.T / np.sqrt(3); e = np.exp(s - s.max(1, keepdims=True)); print((e / e.sum(1, keepdims=True))[2])"

See attention in the full pipeline, Topic 6. It shows where this listening step sits between tokens and the final answer.

Sources

  • Attention weights computed on this machine, 14 November 2026, with the numpy code shown above.
  • Attention mechanism: "Attention Is All You Need", Vaswani et al., 2017, arXiv:1706.03762.