1. One token, several relationships
Consider the sentence “He likes the painting she made.”
Understanding “likes” involves its subject, “he”, and its object, “the painting”. Understanding “painting” may also involve the person who made it. Different relationships can matter at the same time.
In the previous lesson, one attention head calculated a set of weights and used them to combine value vectors:
A single head can assign weight to several positions. It can also carry multiple kinds of information in different coordinates. However, it produces one weighted mixture for each query, using the same weight for every coordinate of each value vector.
A simple average of two value vectors
An average is not a record of its separate inputs. For example, averaging and gives the same result as averaging with itself. Once we keep only the average, we cannot generally recover which pair produced it.
This motivates a useful extension: calculate several mixtures in different learned spaces and keep their outputs separate until we combine them.
2. Several heads in parallel
Multi-head attention repeats the attention calculation with separate query, key and value projections. Each head receives the input sequence :
Head then calculates:
Softmax operates across the available keys for each query row. Because the projections differ, the same input position can produce different attention distributions in different heads.
Head A · query “likes”
Head B · query “likes”
Our small example uses the tokens “he”, “likes” and “painting”. With “likes” as the query, one set of hand-picked projections favours “he”; the other favours “painting”. The numbers are calculated from the matrices shown later, rather than inferred from a trained language model.
We concatenate the head outputs along their feature dimension and apply a learned output projection:
Concatenation keeps the separate head outputs available to . It does not average them together. The output projection can mix their coordinates to produce the representation passed to the rest of the network.
Each head has its own parameters in standard multi-head attention, but all heads are trained jointly with the network. They are not independently trained models with fixed, human-assigned responsibilities.
3. Full inputs, smaller working spaces
Do eight heads make attention eight times as expensive? The answer depends on the width assigned to each head.
A common arrangement keeps the total output width fixed. If the model width is and the number of heads is , choose:
The original Transformer used and , giving coordinates per head. This is an example of a design choice, not a universal rule for all models.
With input positions, the shapes are:
Every projection receives the complete input row
Head 1
Full model width → a smaller learned subspace
Head 2
Full model width → a smaller learned subspace
Every head projects the full input. We do not give the first head only the first group of input coordinates and the second head only the next group. A projection can combine information from any input coordinate.
A smaller projected space can discard information. Returning to the original width after concatenation does not prove that the operation preserves all information or is invertible.
What stays fixed, and what grows?
Under this equal-width arrangement, ignoring biases, the query, key and value projections together contain parameters. The output projection adds :
The leading work for all score products is proportional to:
The value-mixing products have a similar leading cost. Narrower heads therefore avoid multiplying these matrix-product costs by the full head count.
But more heads are not free. A separate attention table for each head contains entries, and Softmax, masking and execution overhead can grow. Optimised implementations may avoid storing every table; actual memory use and speed depend on the implementation.
Query/key/value width per head: 64
Concatenated width: 512
Q/K/V/output projection weights, excluding biases: 1,048,576
Attention weight entries across heads for a 16-token sequence: 2,048
This uses equal head widths and full multi-head Q/K/V projections. Projection parameter count stays fixed; the number of weight-table entries grows with head count. Actual memory and runtime depend on the implementation.
Increasing the head count at fixed width also makes each head narrower. More heads do not automatically produce a better model: capacity, training and the task all matter.
A complete numerical example
For Experiment 20-A, use three four-coordinate input rows:
The rows stand for “he”, “likes” and “painting”. These are teaching vectors, not learned word embeddings. Each head maps four input coordinates into two query, key and value coordinates.
The complete projection matrices used by Experiment 20-A
Change the query below to see how its dot products, weights and weighted values change. The two two-coordinate head outputs concatenate into four coordinates, then the displayed mixes them.
Head A · Query [2.00, 0.10]
he: score 1.414 · weight 66.97%
likes: score 0.021 · weight 16.63%
painting: score 0.007 · weight 16.40%
Head output: [1.0877, 0.7495]
Head B · Query [2.00, 0.10]
he: score 0.000 · weight 16.32%
likes: score 0.014 · weight 16.55%
painting: score 1.414 · weight 67.13%
Head output: [0.9184, 0.2471]
Concatenated: [1.0877, 0.7495, 0.9184, 0.2471]
After output projection: [1.5469, 0.8730, 0.9184, 0.2471]
Actual matrix arithmetic with fixed teaching inputs and projections. These heads were designed to show different patterns; they did not learn grammar from a corpus.
With causal masking, a query may use its own position and earlier positions, but cannot read later positions. We add a mask to the scaled scores before Softmax, with allowed entries and forbidden entries . For example, “likes” cannot attend to “painting” when the mask is enabled.
Within this attention operation, each head computes its mixture separately; the output projection combines them. Shared inputs, joint training and subsequent layers also connect their contributions across the network.
Some architectures use multi-query attention or grouped-query attention, where query heads share key and value projections. These variants can reduce key-value cache costs during generation. Sharing keys and values is not a mechanism that assigns grammatical roles or makes heads explicitly discuss their choices.
4. What do heads attend to?
A heatmap is a useful way to inspect attention. Each row is a query position, each column is a key position, and darker cells indicate larger weights.
Previous position
Same position
Earlier matching item
Special position
Patterns worth looking for include attention to an adjacent position, the same position, an earlier related token or a special token. A trained model can also show patterns associated with syntax or references. Such observations depend on the model, layer, input and training.
These diagrams are deliberately schematic. They do not establish that a particular model contains exactly these four types of heads.
A related idea is an induction pattern: after seeing a sequence such as , a model may use the earlier occurrence to help predict . Some studied attention circuits support this kind of copying from context. That does not mean one head alone explains every form of in-context learning.
Heads can be redundant or serve several purposes. A head that often attends to a separator is not necessarily confused; that position may carry useful information, or the pattern may have little effect on the final result.
5. What you have discovered
- A head produces a weighted mixture for each query. Several heads allow separate mixtures in separate learned spaces.
- Standard multi-head attention gives each head its own query, key and value projections of the full input.
- Head outputs are concatenated and passed through an output projection.
- Dividing a fixed width among heads keeps leading matrix-product work comparable, while head-specific overhead and attention-table size can grow.
- Head roles emerge through joint training. Neither specialisation, lossless compression nor improved performance is guaranteed by adding heads.
Attention is only one part of a Transformer. Next, we will put it together with positional information, feed-forward networks, residual connections and normalisation to understand the complete architecture.