Transformer Self-attention

In self-attention, a word takes in context by mixing what other words contribute.

1Explore

Which words does each word attend to?

Selected word (q, row) Word attended to (k, column) Brighter = more attention (w)

The weights in each row sum to 1. Hover over a cell to see which word attends to which.

① Choose a row

The sentence is “The cat hid because it was scared.” Here, each of its seven words is one token. Select a word to outline the row showing how it attends to the sentence.

② Read the brightest cell

From “it”, “cat” has the highest attention weight (0.83).

③ Change the distribution

Raise T to spread attention more evenly. The weights in each row still sum to 1.

Think of listening in a noisy room: you give some voices more attention than others. That offers a picture of how each word “listens” to other words. Here, though, the weights come from a calculation, not a human judgment of importance.

For each word, attention assigns a weight to every word in the sentence. Those weights set how much of each value vector goes into the result. In this example, the hand-picked vectors make “it” attend strongly to “cat” so you can see the mixing clearly. They do not show how a real model decides what a pronoun refers to.

The central idea: this attention head builds an output for each word by taking a weighted mix of the sentence’s value vectors.

You are exploring one attention head and one fixed sentence. Its Q, K and V vectors are hand-picked for clarity, not learned through training. The wider view below shows where multiple heads, position information, feed-forward layers and training fit.

2Observe

Let the map answer

  1. Select “it”. Which of the seven cells is brightest? Which word does that column represent?
  2. Try “scared”, then “hid”. Do their brightest cells fall in the same column? One word can receive attention from several others.
  3. Compare the numbers across a row. However the weights are shared among the seven tokens, their total stays the same.
  4. Move T from a concentrated distribution toward a more even one. Does the total change?

3The wider view

Where does this head fit in a Transformer?

Text → vectors + position Multi-head attention Feed-forward × N Next token probabilities ↑ You explored one head inside this block

This sketch is for text generation. It leaves out residual connections, normalization and masks. In the interactive map, all seven positions can attend to one another; it does not model the mask that restricts generation to earlier context.

4Connections

Beyond this sentence

5The mathematics

From match scores to a weighted mix

Open to follow the calculation

scoreij = (qi · kj) / √d

wij = softmaxj(scoreij) = exp(scoreij) / Σk exp(scoreik)

outputi = Σj wij · vj

Hover over a symbol, or tap it on a phone, to see what it describes in the map.

These equations describe T = 1. When you adjust the teaching control, the calculation becomes wij = softmaxj(scoreij / T). Higher T makes the distribution more even. Here, d = 4 and √d = 2 stay fixed.

The bar’s segment widths show the weights w, not the components of the output vector. To get that output, multiply each value vector v by its weight and add the vectors.

qi
The query: what the selected word is looking for. Teal links it to the selected token and its heatmap row.
kj
The key: what token j offers for comparison. It corresponds to a column, labeled along the top of the heatmap and highlighted in amber.
scoreij
qi·kj / √d is the match score before softmax. Each cell in the selected row has a score; the heatmap shows the weights calculated from those scores.
√d
The scale factor is 2 here. Dividing by it helps keep softmax from becoming too concentrated on one cell. T is a separate teaching control. This page fixes d = 4; choosing d = 64 would give √d = 8, but not all models use that dimension.
softmax
Turns the row’s seven scores into proportions that sum to 1. Those proportions fill the bar below the heatmap.
wij
An attention weight, shown by an amber cell and a bar segment. A larger weight gives that value vector a larger share of the mix. Each row sums to 1.
vj
The content contributed by token j. This demo simplifies the calculation by setting v = k; a real Transformer learns a separate value projection W_V.
outputi
The resulting vector. For it at T = 1, output ≈ 0.83·vcat + 0.07·vit + …, with the largest component on this example’s hand-defined ENTITY axis. These numbers do not establish what “it” refers to.

Remove the √d scaling at T = 1 and the it-to-cat weight rises from 0.83 to 0.99. With the score differences enlarged, softmax puts almost all the weight on one cell. Scaling helps avoid that concentration.

Sources and scope

Vaswani et al.: Attention Is All You Need (§3, equations 1–3)

This interactive model uses the conditions stated above. The map helps you see what the equations describe; their assumptions still matter.