π Dropout Layer
Descriptionβ
< What is dropout? >β
Dropout randomly sets some hidden activations to zero during training. It is a regularization technique for neural networksβnot an optimizer or an initialization method.
< Drop probability and keep_prob >β
If the dropout probability is , each activation is dropped with probability and kept with probability :
For example, p = 0.2 drops 20% of activations; equivalently, keep_prob = 0.8 keeps 80%. Tutorials often use
keep_prob, while PyTorch's Dropout(p=0.2) and TensorFlow's rate=0.2 use the probability of dropping a unit.
Key pointsβ
< Training, inference, backpropagation >β
For an input vector , dropout samples a binary mask for every forward pass. Each element is 1 with probability and 0 with probability :
With inverted dropout, the forward and backward rules are:
< What the backward pass receives and returns >β
| Object | What it is | Where it comes from |
|---|---|---|
| How the loss changes with dropout's output | Received from the next layer (grad_output) | |
| Dropout's local Jacobian | Computed from the cached mask | |
| How the loss changes with dropout's input | Computed here and sent to the previous layer (grad_input) |
The chain rule wires them together:
Every layer's backward pass is a small function that receives the gradient with respect to its output and returns the gradient with respect to its input:
forward: h ββ[linear]βββΆ x ββ[dropout]βββΆ y ββ[next layer]βββΆ β¦ βββΆ L
backward: βL/βh βββ[linear]ββ βL/βx βββ[dropout]ββ βL/βy βββ β¦ βββ 1
The same mask is reused for its matching backward pass, so an activation dropped in the forward pass receives no gradient for that pass. Since , scaling by makes ; evaluation can therefore use every unit without additional scaling.
< A small example >β
Suppose a layer produces:
With , one possible mask is . In that training pass, inverted dropout produces:
import torch.nn as nn
model = nn.Sequential(
nn.Linear(512, 512),
nn.ReLU(),
nn.Dropout(p=0.2), # p is the probability of dropping a unit
)
model.train() # dropout is active
model.eval() # dropout is disabled
< Why it regularizes >β
- It prevents co-adaptation: a unit must learn features that remain useful with many different subsets of other units.
- Each random mask trains a different thinned subnetwork, acting like a lightweight ensemble whose parameters are shared.
- The temporarily reduced capacity makes memorizing the training set harder and can reduce overfitting.
- Dropout does not delete neurons or parameters; it only masks their outputs for an individual training pass.
< Practical guidance >β
- Add dropout to hidden representations when training performance is much better than validation performance.
- Start modestly:
p = 0.1is common in transformer blocks;p = 0.5is more typical for wide fully connected layers. - Do not use dropout to fix underfitting, and do not forget
model.eval()for evaluation or inference.