Element-wise regularization
Training dense or general neural-network layers
PyTorch · Class
torch.nn.Dropout randomly zeros input elements during training with probability p. It samples independently on each forward call and preserves the input shape.
Dropout is a module that independently samples input elements for removal on each forward pass while training. Removed values become zero, and the output keeps the input shape; evaluation mode disables the stochastic operation.
class torch.nn.Dropout(p=0.5, inplace=False)| Name | Type | Description |
|---|---|---|
p | float | Probability that an input element is zeroed; defaults to 0.5. |
inplace | bool | Whether to perform the operation in-place; defaults to False. |
A Tensor with the same shape as the input.
import torch
from torch import nn
layer = nn.Dropout(p=0.2)
x = torch.randn(20, 16)
layer.train()
y_train = layer(x)
layer.eval()
y_eval = layer(x)
print(y_train.shape, y_eval.shape)The same layer applies stochastic dropout in training mode and disables it in evaluation mode while preserving the input shape.
Training dense or general neural-network layers
Reducing reliance on individual activations
Applying stochastic masking to tensors of arbitrary shape
p control in nn.Dropout?p is the probability that an individual input element is zeroed during training. The default is 0.5, and the selection is sampled independently for each forward call.
Follow-up: How does the module behave for arbitrary tensor ranks? It accepts any input shape and returns a tensor with that same shape.
inplace?The inplace argument controls whether dropout writes directly into its input. It defaults to False; setting it to True performs the operation in-place.
Follow-up: What shape constraints does this class impose? None are documented: the input may have any shape.
nn.Dropout differ between training and evaluation?During training, selected elements are set to zero according to independent Bernoulli samples, while evaluation does not apply the training-time dropout operation. The returned tensor has the same shape as the input.
Follow-up: What is the documented return type? The forward pass returns a Tensor.
torch.nn.Dropout1dtorch.nn.Dropout2dtorch.nn.Dropout3dtorch.nn.AlphaDropouttorch.nn.FeatureAlphaDropout