Dropout and weight decay
Quick answer
Dropout randomly masks activations during training and compensates for the expected scale. Weight decay shrinks parameters during optimization. Both can regularize a model, but neither guarantees better validation performance. Examples are static Python snippets tested locally on PyTorch CPU. ByteMind execution is unverified.
Dropout expectation and evaluation
import torchtorch.manual_seed(18)d=torch.nn.Dropout(.5); x=torch.ones(20000); y=d(x)print(sorted(y.unique().tolist()),abs(y.mean().item()-1)<.04)d.eval(); print(torch.equal(d(x),x))Expected output (locally tested): [0.0, 2.0] True True. The empirical mean is close to one; evaluation returns the input.
How it works
In inverted dropout, with probability p of dropping and independent Bernoulli keep mask m, output is mx/(1-p) for 0<=p<1. Its conditional expectation is x; an individual pass differs. PyTorch Dropout becomes identity in eval mode. Do not confuse dropout masks with permanent pruning. L2 penalty lambda||w||²/2 contributes lambdaw to the gradient. In plain SGD, coupled L2 matches multiplicative decay w <- (1-etalambda)w-etag; adaptive optimizers and momentum can break this equivalence. AdamW decouples the shrinkage. Parameter groups can exclude biases and normalization affine parameters from decay as a modeling choice. Regularization strength must be selected using validation; report the exact policy, and measure ordinary predictive loss separately from penalty terms.
Decoupled decay without task gradient
import torchw=torch.nn.Parameter(torch.tensor([2.,-4.],dtype=torch.float64)); w.grad=torch.zeros_like(w)o=torch.optim.AdamW([w],lr=.25,weight_decay=.2); o.step()print([round(v,4) for v in w.tolist()])Expected output (locally tested): [1.9, -3.8]. The explicit zero gradient leaves only multiplicative decay in this first update.
Common mistakes
Do not leave dropout active for ordinary deterministic evaluation. Do not add an explicit L2 penalty and decay unintentionally. A training loss with dropout active is not directly comparable to evaluation-mode loss.
Practice and next steps
Compare a fixed dropout mask with the expected activation scale. In the milestone compare regularization on/off for each optimizer, holding batch order and initialization fixed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dropout and weight decay — knowledge check
Apply dropout and weight decay to small examples and debugging scenarios.
What happens to retained activations during inverted dropout at p=0.5?
What does ordinary PyTorch Dropout do in eval mode?
What is the gradient of lambda*w²/2?
When is coupled L2 equivalent to ordinary multiplicative decay?
How should decay strength be chosen?