Macalinao StudioLearnAll enriched lessons ↗
High school path · 7 sectionsSwitch to college ↗

High school / LONGER LESSON 12

Attention: follow every shape

Compute a mixture, apply a causal mask, trace gradients and combine heads.

Preparation: Weighted averages; matrix multiplication for college.

Plan 20–35 minutes with the activities; this is a study estimate, not a timed requirement. Both paths have complete narration. Your notes and progress are kept separately for each level.

Section 01 / 07

Questions, labels and messages

High-school path. Compute a mixture, apply a causal mask, trace gradients and combine heads.

Preparation: Weighted averages; matrix multiplication for college. Work through one section at a time. Predict the experiment result before changing its controls, then explain what the example leaves out.

A query is the current question. Keys are labels used to find relevant messages, and values are the messages themselves. Attention compares the query with each key, turns the scores into shares, and mixes the values. The label and message have different jobs even when they come from the same input. A large weight tells you about this routing calculation; it is not automatically a complete explanation of the model's final answer.

Worked step 1. One query compares with three keys.

Worked step 2. Its three scores produce three weights.

Worked step 3. Those weights mix three values into one output.

Q[n,dₖ] Kᵀ[dₖ,m] → S[n,m]; A[n,m] V[m,dᵥ] → O[n,dᵥ]

Queries times transposed keys produce one score per query and key. Multiplying weights by values produces one output per query.

Try it yourself. Four queries attend to seven source positions. How many output positions result?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. Four. Every query makes one mixture. The seven sources determine what can be mixed.

Section 02 / 07

Calculate the mixture

Take already-scaled scores two, one and zero. Their softmax shares are approximately 0.665, 0.245 and 0.090. Let the messages be ten, twenty and thirty. Multiply each message by its share and add. You get about 14.25. The calculation did not select just one message; it combined all three. Try predicting whether the output will move upward or downward when you increase the score of the value thirty.

Worked step 1. First contribution: 0.665241×10 ≈ 6.6524.

Worked step 2. Second: 0.244728×20 ≈ 4.8946.

Worked step 3. Third: 0.090031×30 ≈ 2.7009; total ≈ 14.2479.

O = Σⱼ aⱼvⱼ

Add each attention weight multiplied by its value.

Try it yourself. Can this mixture exceed thirty before any later transformation?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. Nonnegative weights summing to one keep this scalar weighted average between ten and thirty.

Section 03 / 07

Exclude forbidden information

A position predicting future text must not read the future answer. A causal mask prevents that connection. Setting a forbidden score to zero does not block it: the exponential of zero is one, so it still gets a share. Instead, exclude it before normalization. In mathematical notation, add negative infinity to that score. Always leave at least one allowed source, because there is no meaningful weighted average of no available messages.

Worked step 1. Block the third entry in scores [2,1,0].

Worked step 2. The remaining probabilities become [0.7311,0.2689,0].

Worked step 3. The values [10,20,30] now mix to about 12.6894.

A = softmax(QKᵀ/√dₖ + M)

Add the mask before softmax. Allowed entries add zero; forbidden entries add negative infinity.

Try it yourself. Why is replacing the forbidden score with zero wrong?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. Zero is still a valid score. In the original example it receives about nine percent of the weight.

Section 04 / 07

Scale scores and protect arithmetic

A dot product adds one product per coordinate. More coordinates can make its typical size grow. Large score gaps can squeeze nearly all the share onto one source. Dividing by the square root of key width helps control that scale under common assumptions. This differs from subtracting the largest score: subtraction protects exponential arithmetic without changing probabilities, while scaling generally changes the shares.

Worked step 1. A key width of 64 has square root eight.

Worked step 2. A raw dot product of sixteen becomes a scaled score of two.

Worked step 3. Subtracting a common row maximum afterward leaves the intended probabilities unchanged.

S = QKᵀ / √dₖ

Divide the query-key dot product by the square root of key width.

Try it yourself. Does scaling guarantee variance one for every trained attention head?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. The argument assumes particular independence and variance conditions that learned features may not satisfy.

Section 05 / 07

Trace the backward path

A value influences the mixture directly through its weight. A score acts indirectly: changing one score changes several shares. For one scalar output, a score's sensitivity is its weight times its value minus the current mixture. Values above the current mixture pull upward when their scores rise; values below it pull downward. This connects the gradient to the weighted-average picture instead of treating it as a formula to memorize.

Worked step 1. For unit output sensitivity, the first score derivative is 0.665241×(10−14.2479)≈−2.8259.

Worked step 2. All three score derivatives sum to zero.

Worked step 3. A common score shift therefore cannot change the output.

∂O/∂sⱼ = aⱼ(vⱼ−O)

For a scalar mixture, the score derivative is the weight times the difference between its value and the output.

Try it yourself. Why can the three weights not be differentiated as independent quantities?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. They share one normalization denominator. Changing one score changes every normalized weight.

Section 06 / 07

Combine heads and verify

Several heads can ask different questions about the same positions. Each makes its own mixture, and their features are placed side by side. A learned output rule then returns the result to the model's working width. The heads are not guaranteed to match human categories. In Workbench, change query scale and mask separately: one changes the question's preferences, while the other changes which information is available.

Worked step 1. Four heads with eight features each give 32 features per position.

Worked step 2. A 32×24 output matrix returns 24 features.

Worked step 3. Ten input query positions remain ten output positions.

Try it yourself. What is wrong with turning four heads over ten queries into forty output positions?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. That concatenates the sequence axis instead of the feature axis. Standard multihead attention preserves query length.

Section 07 / 07

Mastery and transfer

Bring the pieces together. First answer the checkpoint without the worked solutions. Then explain the mechanism in your own words, using one numerical example from this lesson.

To block a future source, you… Option 1: Set its score to zero. Option 2: Exclude it before normalization. Option 3: Increase its value.

Pause here and choose an answer before continuing. The checkpoint explanation follows.

Reveal worked solution

The correct choice is option 2: Exclude it before normalization. Zero still receives a positive softmax weight.

Transfer task. Change one assumption in the worked example. State your prediction, calculate or test it in Workbench, then explain whether the result supports your prediction.

Reveal worked solution

Self-check rubric. Name the changed assumption, keep the other inputs fixed, show at least one intermediate calculation, and distinguish what the toy example guarantees from what would require a trained model or a real measurement.

LONGER TRANSFORMER SYSTEMS LESSONS

Follow the mechanism in depth

Worked examples, local narration and connected learning modes at your level.

Sources and scope

Original worked examples and teaching text, grounded in these primary references. Numerical fixtures explain mechanisms; model quality and hardware performance require separate measurements. Both levels share key examples, with different explanations and checkpoints.

Reset this high school path?

This removes only this path’s notes, progress and experiments from this browser.