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?
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.
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
- Select “it”. Which of the seven cells is brightest? Which word does that column represent?
- Try “scared”, then “hid”. Do their brightest cells fall in the same column? One word can receive attention from several others.
- Compare the numbers across a row. However the weights are shared among the seven tokens, their total stays the same.
- 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?
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.
- Multiple heads. Several attention calculations run in parallel, letting the model combine different representations. Their outputs are joined and then projected. A head is not guaranteed to stand for a named grammar rule.
- Positional encoding. Position information gives the vectors a sense of word order. Without an order signal, attention alone cannot distinguish the order in “the cat bites the dog” from “the dog bites the cat”.
- Feed-forward layers. After attention mixes the values, the same feed-forward network processes each position, using both linear and nonlinear operations.
- Stacked layers. Repeating these blocks updates the representations through multiple layers. In autoregressive text generation, the output layer predicts probabilities for the next token. A token need not be a whole word.
- Training. The model learns the projection matrices that produce Q, K and V. Each new input passes through those projections to form the vectors. This page uses fixed, hand-picked vectors instead.
4Connections
Beyond this sentence
- Text generation. The “T” in GPT stands for Transformer. These models use attention to combine context, alongside multiple layers, multiple heads and other operations.
- Machine translation. The 2017 paper Attention Is All You Need evaluated the Transformer on English-to-German and English-to-French translation.
- Code models. Code can also be split into tokens whose representations draw on context. Attention weights alone are not a complete explanation of a model’s reasoning.
- Protein structure with AlphaFold 2. Methods from the attention family process sequence and geometric information. This is not simply the text model shown here applied unchanged.
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.
Explore further
Return to the Neblux graph
Continue from the Deep Learning node in the Neblux graph. Self-attention is one part of that wider field; this page focuses on the core calculation inside a single head.
Open Deep Learning