Implementing dropout from scratch
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Dropout is a regularization technique that randomly sets a fraction of neuron outputs to zero during training, forcing the network to learn redundant representations and reducing overfitting. During inference, all neurons are active but their outputs are scaled down. Implementing dropout from scratch requires generating a random binary mask during the forward pass, applying it to the activations, and scaling the output so that expected values remain consistent between training and inference.
How Dropout Works
During training, each neuron's output is independently set to zero with probability p (the dropout rate). During inference, no neurons are dropped, but outputs are multiplied by (1 - p) to compensate for the missing neurons during training:
The most common implementation is "inverted dropout," which scales during training (dividing by 1 - p) so that no scaling is needed at inference time.
NumPy Implementation
Usage in a simple network:
PyTorch Implementation
Full Neural Network with Dropout
Standard vs Inverted Dropout
Inverted dropout is preferred because inference requires no modification — the model outputs the correct values without any scaling step.
Dropout Variants
Common Pitfalls
- Forgetting to disable dropout during inference: If dropout stays active during evaluation, outputs are noisy and accuracy drops. Always call
model.eval()in PyTorch or passtraining=Falsein custom implementations before running inference or validation. - Applying dropout before the activation function: Dropout should be applied after the activation (e.g., after ReLU), not before. Applying it before means the activation function sees scaled inputs, which changes the nonlinearity's behavior.
- Using too high a dropout rate: Dropout rate above 0.5 drops more than half the neurons, causing severe underfitting. Common rates are 0.2-0.3 for input layers and 0.5 for hidden layers. Start with 0.5 and reduce if training loss is too high.
- Not using inverted dropout: Standard dropout requires multiplying outputs by
(1 - p)at test time. Forgetting this scaling step means test predictions are systematically too large. Inverted dropout avoids this by scaling during training. - Applying dropout to the output layer: Dropping neurons in the final layer randomly zeroes out class predictions, degrading performance. Dropout belongs in hidden layers only — never on the input or output layer.
Summary
- Dropout randomly zeroes neuron outputs during training to prevent overfitting
- Inverted dropout scales by
1/(1-p)during training so inference needs no modification - The backward pass blocks gradients through dropped neurons (gradient * mask)
- Use dropout rates of 0.2-0.5 for hidden layers, never on the output layer
- Always disable dropout during inference (
model.eval()ortraining=False) - Spatial dropout drops entire feature maps in CNNs; DropConnect drops weights instead of activations
Related reading
- Implementing Feedback Alignment in Tensorflow
- Implementing im2col in TensorFlow
- Implementing im2col in TensorFlow
- Implementing sparse connections in neural network
- Implementing Gradient Boosted Regression Trees in production - mathematically describing the learned model
- Implementing Gradient Descent In Python and receiving an overflow error
- Import Keras on Jupyter Notebook
- Import ResNeXt into Keras
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.