Foundation Models: Tokens, Attention, And Generation · Lesson 6

Calculate Attention With A Simple Example

Normalize the supplied allowed-position scores, multiply the corresponding value vectors by those shares, and add their coordinates. A mask changes the allowed set and its normalization.

Study And Save Progress

Watch The Lesson

6 min 58 sec

Read the video transcript

We will now calculate 1 attention step using 3 supplied source positions, A, B and C. Their compatibility scores are 2, 1 and 0. These scores have already been scaled. We also supply small value vectors so every multiplication can be inspected.

What combination will pass forward, and what changes if position C becomes disallowed? This is a separate classroom construction following the scaled dot-product method described by Vaswani and colleagues in 2017. It is not a measured attention map from the package example. Query-key matching and its scaling

have already produced our given scores. Here an attention weight means a computed share for this step; the learned projection matrices are a different kind of weight, and this arithmetic does not update them. Begin with the scores. Softmax raises the constant e to each allowed score, then divides

by the sum of those exponentials. The constant e is about 2.718; the supplied approximate exponentials are 7.389 for score 2, 2.718 for score 1, and 1 for score 0. Their displayed sum is 11.107. Notice that score 0 still produces a positive contribution because e

raised to 0 equals 1. Divide each exponential by the total. To 3 decimal places, the attention shares are 0.665 for A, 0.245 for B, and 0.090 for C. They add to 1 under this displayed rounding. A gets the largest share,

while B and C still contribute. These shares are normalized over the supplied source positions, with no output-vocabulary choice made at this stage. Now attach the values. A has coordinates 2 and 0. B has coordinates 0 and 2. C has coordinates 1 and 1.

Multiply every coordinate in each value by its position’s share. Then add coordinate 1 across the contributions, and do the same for coordinate 2. Keeping the coordinate columns aligned prevents us from mixing unrelated entries. Using the displayed 3-decimal shares, coordinate 1 is 0.665 times 2,

plus 0.245 times 0, plus 0.090 times 1. That gives 1.420. Coordinate 2 is 0.665 times 0, plus 0.245 times 2, plus 0.090 times 1. That gives 0.580. The resulting vector is approximately 1.420

and 0.580 under this rounded working convention. The reading also records the unrounded calculation so the precision boundary stays visible. We have formed a mixture. The output is different from each of the 3 supplied value vectors. The attention shares sum to 1, while the value coordinates follow their own scale:

coordinate 1 of our result exceeds 1. That is valid for a representation vector. It is not a probability that a word is true or a signal that only A was selected. Several weighted contributions produced the result. Now hold the supplied scores and values fixed, but change the

allowed set so C is excluded. The mask gives C no contribution. Return to the exponentials for A and B and normalize over those allowed positions. Their displayed total is now about 10.107. The new shares are about 0.731 and 0.269, with 0 for the excluded position. This is a changed denominator and mixture,

rather than merely deleting C’s old contribution. Using those new 3-decimal shares, the output becomes about 1.462 and 0.538. Coordinate 1 is 0.731 times 2; coordinate 2 is 0.269 times 2. Removing C’s balanced value changes the mixture even though C previously had a smaller share.

A finite score of 0 and an excluded position therefore have different effects. The mask determines which information may contribute before normalization. Score scale also matters. Multiplying all 3 given scores by 10 would produce 20, 10 and 0, concentrating weight much more strongly on A.

The original Transformer’s dimension-dependent scaling addresses the numerical behavior of query-key scores before this step. Our given scores already include that operation. The 2 coordinates shown for the value vectors do not tell us an otherwise unspecified query-key dimension or justify applying another scale factor.

Pause for a checkpoint. All 3 positions are allowed, but now every score is 0. Keep the original values: 2 and 0, 0 and 2, then 1 and 1. What are the attention shares and combined output? Does score 0 exclude a position?

Finish your calculation before continuing. Each exponential is 1, so each share is 1/3. Combining the values gives coordinates 1 and 1. All 3 positions contributed, even though the result happens to equal C’s supplied value. A score of 0

did not exclude any position. Exclusion requires the defined mask, followed by normalization over what remains allowed. For a separate practice task, use 2 allowed positions with scores 0 and 0. Their values have coordinates 4 and 0, then 0 and 2. Calculate the mixture.

Next, exclude the 2nd position while keeping the 1st allowed. What are the new shares and result? Pause and complete the worksheet before consulting the key. The initial equal shares are 0.5 and 0.5, producing coordinates 2 and 1.

After excluding the 2nd position, the 1st receives share 1 and the excluded one receives 0. The output is therefore 4 and 0. The shares describe allowed-position contributions; the output coordinates remain a representation rather than a vocabulary probability distribution. You can now reproduce the normalized

value mixture, state the rounding you used, and recalculate it after a mask change. You can also distinguish a finite 0 score from exclusion and a computed attention share from a learned parameter. Next, we will place attention inside a complete transformer block, following the other transformations and connections that act on its output.

Main Point

Normalize the supplied allowed-position scores, multiply the corresponding value vectors by those shares, and add their coordinates. A mask changes the allowed set and its normalization.

Key Points

1. Record the inputs

This is a separate classroom construction, not a measured map from the previous package-prefix example.

Source positionAlready-scaled scoreSupplied value
A2(2,0)
B1(0,2)
C0(1,1)

Initially all 3 positions are allowed. The value vectors have 2 coordinates. That does not specify a query/key dimension or justify scaling the scores again.

2. Compute the normalized shares

Softmax uses `exp(score) / sum(exp(allowed scores))`. The constant e is approximately 2.718. Displaying exponentials to 3 decimal places gives:

PositionExponential, approximatelyShare, to 3 decimal places
A7.3890.665
B2.7180.245
C1.0000.090

The displayed denominator is about `7.389 + 2.718 + 1 = 11.107`. The shares sum to 1 under this rounding. Score 0 remains a finite allowed score, so C has a positive share.

3. Mix the values using a declared rounding convention

For the classroom working path, round the weights first to (0.665,0.245,0.090), then compute:

`coordinate 1 = 0.665×2 + 0.245×0 + 0.090×1 = 1.420`

`coordinate 2 = 0.665×0 + 0.245×2 + 0.090×1 = 0.580`

The displayed output is approximately `(1.420,0.580)` under that convention. Several positions contributed. The shares sum to 1; the resulting coordinates need not. Coordinate 1 exceeds 1 here, which is valid for this representation.

Optional precision note

Using the exponential expressions without rounding the weights first gives approximate shares `(0.6652409558,0.2447284711,0.0900305732)` and output `(1.4205124847,0.5794875153)`. Rounding that final output alone to 3 decimal places gives `(1.421,0.579)`. This differs slightly from rounding the shares before multiplication. Keep the classroom convention explicit rather than mixing the 2 paths.

4. Exclude C and recompute

Keep the original scores and values fixed but exclude C through the defined mask. C receives share 0. The allowed denominator becomes `exp(2)+exp(1)`, approximately 10.107. The new shares are approximately `(0.731,0.269,0)` when displayed to 3 decimal places.

Using those rounded shares, the output is:

`(0.731×2, 0.269×2) = (1.462,0.538)`.

The higher-precision output is approximately `(1.4621171573,0.5378828427)`. Deleting C's old contribution without renormalizing would implement a different operation. Here removing the balanced value (1,1) changes both output coordinates.

5. Keep the scale and meaning clear

Multiplying all supplied scores by 10 gives (20,10,0), concentrating the shares much more strongly on A. The historical Transformer scales query–key dot products before the normalization we have inspected. Our scores already include that step.

This operation combines source-position values. A later vocabulary distribution and token-selection rule have a different object and purpose. A computed attention share is also distinct from a learned parameter value saved in a checkpoint.

6. Closing

You can now reproduce normalization and coordinate mixing, identify rounding choices, and recompute a masked case. The result depends on the specified inputs and allowed set. The next lesson places this operation inside a transformer block with additional transformations and connections.

7. Learning Checks

Attempt the calculations before opening the explanations; use the separate worksheet for working space.

M14-L06-Q01

All 3 source positions A, B and C are allowed, with scores 0, 0 and 0. Their values remain A=(2,0), B=(0,2), C=(1,1). What are the attention shares and combined output? Does score 0 exclude a position?

Your shares, output and explanation: ____________________

Show explanation after your attempt

Each exponential is 1, so the shares are 1/3, 1/3 and 1/3. Coordinate addition gives (1,1): ((2+0+1)/3, (0+2+1)/3). All 3 positions contributed, although the output coincides with C’s value. A finite score of 0 did not exclude a position; the defined mask determines exclusion.

M14-L06-P01

In a separate example, positions D and E are allowed with scores 0 and 0. Their values are D=(4,0) and E=(0,2). Calculate the mixture. Then exclude E while keeping D allowed. What are the new shares and output?

Your 2 calculations: ____________________

Show explanation after your attempt

Initially the equal shares are (0.5,0.5), giving 0.5×(4,0)+0.5×(0,2)=(2,1). After excluding E, the allowed set contains only D: the shares are (1,0), and the output is (4,0). The output coordinates are representation values; normalization applies to the shares over source positions.

Sources

Learner Materials

Download Lesson 6 Learner Materials

Use the supplied fictional cases and templates for practice.

Practice

Write your answer, compare it with the model answer and decide what to revisit. These written checkpoints are self-assessed.

All 3 source positions A, B and C are allowed, with scores 0, 0 and 0. Their values remain A=(2,0), B=(0,2), C=(1,1). What are the attention shares and combined output? Does score 0 exclude a position? Your shares, output and explanation: ____________________

Show The Model Answer

Each exponential is 1, so the shares are 1/3, 1/3 and 1/3. Coordinate addition gives (1,1): ((2+0+1)/3, (0+2+1)/3). All 3 positions contributed, although the output coincides with C’s value. A finite score of 0 did not exclude a position; the defined mask determines exclusion.

Compare your reasoning with the model answer. These written checks are self-assessed.

In a separate example, positions D and E are allowed with scores 0 and 0. Their values are D=(4,0) and E=(0,2). Calculate the mixture. Then exclude E while keeping D allowed. What are the new shares and output? Your 2 calculations: ____________________

Show The Model Answer

Initially the equal shares are (0.5,0.5), giving 0.5×(4,0)+0.5×(0,2)=(2,1). After excluding E, the allowed set contains only D: the shares are (1,0), and the output is (4,0). The output coordinates are representation values; normalization applies to the shares over source positions.

Compare your reasoning with the model answer. These written checks are self-assessed.

Open Practice And Save Your Work