Masking Additional Attention Weights with Dropout
The purpose of dropout is to make the model robust. A pattern that survives when random parts of the network are removed is more likely to be a broad pattern than a dependence on one small group of units.
Try it: Dropout at Training vs Inference
How dropout zeroes a random subset of a layer's outputs during training (with a visible seeded mask), how inverted dropout scales the survivors by 1 / (1 - rate) while inference is a no-op (or classic dropout scales at inference instead), and why the average training output matches the inference output.
How it works
- For each unit draw u from the seeded generator; keep it when u >= rate, otherwise output 0.
- Inverted dropout (Keras): multiply kept units by 1 / (1 - rate). Classic dropout: leave them unchanged.
- At inference nothing is dropped: inverted dropout is a no-op, classic dropout multiplies by (1 - rate).
- Average the training output over more and more masks: it converges to the inference output.
Default run (18 steps): A layer outputs 6 activations [0.8, 1.5, 0.2, 2, 1.1, 0.6]. Dropout rate 0.5, training mode, inverted dropout (as Keras does). … Average over 1000 masks (seeds 1..1000): [0.7936, 1.56, 0.198, 1.96, 1.1242, 0.5928]; largest gap to the inference output [0.8, 1.5, 0.2, 2, 1.1, 0.6] is 0.06. The average training output converges to the inference output: dropout preserves the expected activation.
Simplified: One activation vector with up to 10 units and a seeded teaching PRNG (mulberry32) instead of TensorFlow's generator.
Loading the simulation…
A Model That Leans on One Narrow Path
Imagine a training process in which a model keeps finding one small group of internal units that carries most of the useful signal. Those units become a crutch. The model performs well on the examples it has already seen, but it has learned to depend on a narrow set of pathways rather than on a broad, robust pattern. This is the situation that the technique called dropout is designed to discourage.
In this article we first look at dropout as it appears in ordinary deep learning, and then move into the attention mechanism of transformer architectures such as GPT. The goal is to understand not only what dropout does, but exactly where it sits inside attention and why one placement is chosen over another.
Random Ignoring as a Training Device
Dropout is a training technique in which randomly selected hidden layer units are ignored. It helps prevent overfitting by stopping the model from relying too heavily on any specific set of units.
The word selection matters. The units are not chosen by a human, and they are not always the same units. Each training step draws a fresh random selection, so over many steps every unit is sometimes present and sometimes absent. No single unit can be guaranteed to be available, and therefore no single unit can become indispensable to the model. Because the set of available units keeps changing, the weights are shaped so that they do not depend on fragile combinations of units.
The purpose of dropout is to make the model robust. A pattern that survives when random parts of the network are removed is more likely to be a broad pattern than a dependence on one small group of units.
Training Mode Versus Inference Mode
Dropout is a training-time device. Random ignoring is useful while the model is learning, because it shapes the weights. Once training is finished and the model is used to produce outputs, dropout is disabled. The network then works with all of its units available. The same layer therefore behaves differently in the two situations: it drops units while training and passes everything through when the model is in use.
Trace the diagram from top to bottom. The top half is the training life of the layer, where units are ignored on each pass. The bottom half is the use of the finished model, where nothing is ignored. Dropout is not a permanent part of the computation; it is a mechanism that exists to shape the weights during learning.
Where Dropout Fits in Attention
Dropout is not limited to fully connected hidden layers. In the transformer architecture, including models like GPT, the attention mechanism can also use dropout. The source identifies two specific moments when this happens: after the attention weights have been calculated, or after those weights have been applied to the value vectors. In both cases, dropout is a random masking step inserted into the flow of attention.
The two placements answer a simple question: which quantity should be randomly ignored? Either the weights that decide how much each position attends to others, or the result obtained after those weights have been combined with the value vectors. The flow below shows the attention steps in order and marks both insertion points.
Placement A sits between the weights and the value step, so the mask acts on the weights themselves. Placement B sits after the weights have been applied to the value vectors, so the mask acts on the combined result. The data passes through the same steps in both cases; only the point where the random masking happens is different.
Tracing a Dropout Mask on Attention Weights
Now we follow a dropout mask through placement A, where the mask is applied to the attention weights before they are applied to the value vectors. The example below uses invented numbers for four positions. It is a generated illustration of the masking step, not a figure taken from the source.
Masking Four Attention Weights
Four attention weights for one query position are 0.40, 0.10, 0.30, and 0.20. A dropout mask marks positions 2 and 4 as ignored during this training step. Trace what happens to the weights before they are applied to the value vectors.
- 1Start with the weights
The attention weights before masking are 0.40, 0.10, 0.30, and 0.20, one for each position.
- 2Draw the mask
The random selection for this training step ignores positions 2 and 4. Positions 1 and 3 are kept. On another step a different selection could be drawn.
- 3Apply the mask to the weights
Under placement A, the masked positions are set to zero, so position 2 becomes 0 and position 4 becomes 0. Positions 1 and 3 keep 0.40 and 0.30.
- 4Pass the masked weights forward
The masked weights are then applied to the value vectors. Positions 2 and 4 contribute nothing to this step, so the output depends only on the kept positions.
After masking, the weights are 0.40, 0, 0.30, and 0. The source pack describes the masking step but does not specify how the remaining values are scaled, so that detail is not covered here.
Notice what the mask does and does not do. It removes a random subset of the attention weights for this step, and the next training step may remove a different subset. Over many steps, every position is sometimes ignored, so no single attention pathway becomes indispensable. The mask is a training-time event: when the model is used for outputs, the masking is turned off.
Choosing Which Placement This Course Uses
The source names two valid times to apply dropout in attention and chooses one for its own walkthrough. Placing the two options side by side makes the choice easier to understand.
| Question | Placement A: after the attention weights | Placement B: after the weights are applied to values |
|---|---|---|
| What is randomly ignored? | The weights that decide how much each position attends to others | The result after the weights are combined with the value vectors |
| Where in the flow is the mask? | Between the weight calculation and the value step | After the value step, before the output |
| Status in this course | Used in this course | Described by the source as a valid option, not the one used here |
| Why it is chosen | The source describes it as the more common variant in practice | Valid, but less common in practice according to the source |
Comparing the two dropout placements in attention
Mistakes Learners Make With Dropout
Common mistake Thinking dropout is always active
Why it failsDropout is used only during training and is disabled afterward, so it does not affect the model once it is in use.
Do this insteadTreat dropout as a training-time device. In inference mode, all units are available.
Common mistake Believing the same units are always dropped
Why it failsEach training step draws a fresh random selection, so the units ignored change from step to step.
Do this insteadPicture the mask as a new random draw on every step, not a fixed set of removed units.
Common mistake Confusing the two placements in attention
Why it failsPlacement A masks the attention weights, while placement B masks the result after the weights are applied to value vectors.
Do this insteadAsk which quantity is being randomly ignored: the weights themselves, or the combined result.
Common mistake Assuming the source gives a rescaling rule for the remaining weights; the source pack does not describe one, so do not treat a specific scaling method as taught here
Practice Tracing a Mask
Four attention weights are 0.25, 0.25, 0.25, and 0.25. A dropout mask under placement A ignores positions 1 and 3 on one training step. Write the weights after masking, then say which weights would be ignored on the next training step if the random selection changed.
Hints
- Under placement A the mask acts on the attention weights, before they are applied to value vectors.
- Masked positions contribute nothing to this step, so their weights are zero in the masked version.
- Each training step draws a fresh random selection, so the next step can ignore a different set of positions.
Explain in two or three sentences why a model trained with dropout is less likely to depend on one narrow set of units, and why dropout is disabled once the model is used to produce outputs.
Hints
- Think about what happens when no single unit is guaranteed to be available.
- Think about the difference between training and using the finished model.
Key Takeaways
- Dropout is a training technique in which randomly selected hidden layer units are ignored, which discourages the model from relying on any specific set of units.
- Each training step draws a fresh random selection, so no single unit can become indispensable.
- Dropout is disabled after training, so the finished network works with all of its units available.
- In transformer attention, dropout can be applied after the attention weights are calculated or after they are applied to the value vectors.
- This course applies the dropout mask after computing the attention weights, the placement the source describes as more common in practice.