Concepts / Dropout in Deep Learning

Dropout in Deep Learning

Dropout is a training technique in which randomly selected hidden layer units are ignored, which helps prevent overfitting by stopping the model from relying too heavily on any specific set of units.

  • Programming
Interactive lab

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

  1. For each unit draw u from the seeded generator; keep it when u >= rate, otherwise output 0.
  2. Inverted dropout (Keras): multiply kept units by 1 / (1 - rate). Classic dropout: leave them unchanged.
  3. At inference nothing is dropped: inverted dropout is a no-op, classic dropout multiplies by (1 - rate).
  4. 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.

Educational simulation

Loading the simulation…

A Model That Leans on a Crutch

Imagine a training process where 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 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.

Dropout is a training technique in which randomly selected hidden layer units are ignored. By ignoring units at random, it helps prevent overfitting, because the model cannot rely too heavily on any specific set of units. This article first looks at dropout as it appears in ordinary deep learning, then moves 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.

Why Dropout Exists During Training

The key word in the definition is selection. 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.

This is why random ignoring is useful while the model is learning. Because the network never knows which units will be present on the next step, the weights are shaped so that they do not depend on fragile combinations of units. The network is pushed toward a broad, robust pattern instead of a narrow set of pathways. Dropout is therefore a regularization device that works through the learning process itself.

Dropout Switches Off at Inference

Dropout is a training-time device. Once training is finished and the model is used to produce outputs, dropout is disabled, so it does not affect the model once it is in use. The network then works with all of its units available. The randomness that helped shape the weights during learning has no role when the model answers new inputs.

The diagram below contrasts the two states. The same layer is involved in both, but the mask that decides which units are ignored behaves very differently. During training it is sampled fresh on each step. During use it is switched off.

Hidden unitssome ignored on each stepDropout maskfreshly sampled each stepHidden unitsall units availableDropout maskdisabledmaskednot applied
How does the same layer behave differently in training mode, where units are randomly masked, and in inference mode, where all units are kept?

Where Attention Can Accept Dropout

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 attention 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 one 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 flowchart below follows the attention computation from raw scores to output and marks both candidate points. The first candidate is the one this course follows.

Attention scoresSoftmaxAttention weightsDropout option Aapplied after weights; usedin this courseWeighted sum overvaluesDropout option Bapplied after weights meetvaluesAttention outputnormalizeproducesmask weightsmasked weightsmask resultresult
Where exactly in the attention computation can dropout be inserted, and what does the data flow look like at each option?

Read the flowchart as a sequence of choices rather than a sequence that always uses both options. In a given design, dropout can sit at option A, at option B, or in the relevant spot for that design. The source describes both as valid times to apply dropout in attention, so seeing them side by side makes the choice easier to understand.

Tracing a Mask Through Attention Weights

Consider what happens when dropout sits at option A, directly after the attention weights are calculated. Each attention weight says how much one position attends to another. A dropout mask then decides, entry by entry, which weights are ignored. The example below uses made-up numbers for four attention weights and four value vectors. The numbers are illustrative only and are not taken from the source.

Worked example

A dropout mask applied to attention weights

Four attention weights are 0.5, 0.2, 0.2, and 0.1. The associated value vectors, reduced here to single numbers for clarity, are 10, 20, 30, and 40. Suppose the dropout mask ignores the second and fourth weights on this training step. What does the weighted sum over the values look like before and after masking?

  1. 1
    Compute the unmasked weighted sum

    Multiply each weight by its value and add: 0.5 times 10 is 5, 0.2 times 20 is 4, 0.2 times 30 is 6, and 0.1 times 40 is 4. The total is 19.

  2. 2
    Apply the mask to the weights

    The mask ignores the second and fourth weights. Those entries no longer contribute, so the weights used become 0.5, 0, 0.2, and 0.

  3. 3
    Compute the masked weighted sum

    Multiply the masked weights by the values: 5, 0, 6, and 0. The total is 11.

  4. 4
    Compare the two results

    Masking changed the output from 19 to 11 because two positions stopped contributing on this step. On a different training step, a different subset would be masked.

Result

With this mask, the weighted sum over the value vectors falls from 19 to 11. The change comes from the masked weights, which are the quantity dropout acts on at option A.

Weight 10.5Weight 10.5Weight 20.2Weight 20Weight 30.2Weight 30.2Weight 40.1Weight 40maskedmasked
What happens to each attention weight when the mask ignores some entries, and how does the weighted sum over value vectors change as a result?

The diagram shows the state change for this one step. Weights 2 and 4 are the ones the mask ignores, so they are the entries that change. Weights 1 and 3 pass through untouched. Because the weighted sum over the value vectors is built from these weights, the output of attention shifts as well. A new mask on the next step would choose a different subset, which is why no single position can be relied on across training.

Why This Course Masks After the Weights

This course applies the dropout mask after computing the attention weights. The source describes that placement as the more common variant in practice, which is the reason this course follows it. The walkthrough above is therefore the placement the course uses: the mask acts on the weights that decide how much each position attends to others, before those weights are applied to the value vectors.

The other placement, option B, masks the result after the weights have already been combined with the value vectors. Both are valid according to the source. The practical reason the course prefers option A is that the source describes it as the more common choice, and the course adopts the same convention so that learners see the variant most often encountered in practice.

Mistakes When Reading Dropout Placement

  • Common mistake

    Believing dropout stays active when the model answers new inputs

    Why it fails

    Dropout is a training-time device and is disabled afterward, so it does not affect the model once it is in use.

    Do this instead

    Think of the mask as present only while weights are being learned, and fully available during inference.

  • Common mistake

    Assuming the same units are always ignored

    Why it fails

    Each training step draws a fresh random selection, so over many steps every unit is sometimes present and sometimes absent.

    Do this instead

    Treat the mask as re-sampled on every step rather than as a fixed set of removed units.

  • Common mistake

    Treating the two attention placements as a single fixed location

    Why it fails

    The source names two valid moments, after the attention weights are calculated or after they are applied to the value vectors.

    Do this instead

    Ask which quantity is being masked, the weights or the combined result, and then follow the placement a given design uses.

  • Common mistake

    Confusing dropout with a permanent removal of units

    Why it fails

    A unit ignored on one step can be active on the next.

    Do this instead

    Describe dropout as random ignoring during training, not as deleting parts of the network.

Practice: Predict the Masked Sum

What do you think happens?

Using the same weights 0.5, 0.2, 0.2, 0.1 and values 10, 20, 30, 40, suppose a different mask ignores only the third weight. Before reading the answer, predict the weighted sum after masking.

Reveal answer
Answer:

The weighted sum is 13.

With the third weight removed, the weights used are 0.5, 0.2, 0, and 0.1. Multiplying by the values gives 5, 4, 0, and 4, which total 13.

MEDIUM

A training run uses dropout at option A, after the attention weights are calculated. On a given step the mask ignores the first and third weights. Identify which weights change and state what happens to the weighted sum over value vectors. Then explain why the next training step may ignore a different subset.

Hints
  • Dropout acts on the weights at option A, so only the entries the mask ignores change.
  • The weighted sum is built from the weights and value vectors, so a changed weight changes the sum.
  • Each training step draws a fresh random selection, so the next subset is not fixed.

Key Takeaways

  • Dropout is a training technique in which randomly selected hidden layer units are ignored, which helps prevent overfitting by stopping the model from relying on any specific set of units.
  • Dropout is used only during training and is disabled afterward, so the full network is available once the model produces outputs.
  • 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 the more common variant in practice.

Related concepts

More concepts starting with D