PyTorch torch.nn.Dropout function
PyTorch torch.nn Reference Manual
torch.nn.Dropout is the module used for regularization in PyTorch.
It reduces co-adaptation between neurons by randomly zeroing out input elements, thereby preventing overfitting.
Function definition
torch.nn.Dropout(p=0.5, inplace=False)
Parameter Description:
p(float): The probability of each element being zeroed. Defaults to 0.5.inplace(bool): Whether to perform the operation in place. Defaults to False.
Usage examples
Example 1: Basic usage
Create and use a Dropout layer:
Example
import
torch
import
torch.
nn
as
nn
Create a Dropout layer with a drop probability of 0.5
dropout
=
nn.
Dropout
(
p
=
0.5
)
Training mode (Dropout is active)
dropout.
train
(
)
Create input
input_tensor
=
torch.
ones
(
1
,
10
)
(
"Input:"
,
input_tensor.
squeeze
(
)
.
tolist
(
)
)
Perform multiple forward passes to observe the randomness
for
i
in
range
(
3
)
:
output
=
dropout
(
input_tensor
)
(
f
"Output #{i+1}:"
,
output.
squeeze
(
)
.
tolist
(
)
)
As you can see, about half of the elements are randomly zeroed on each call.
Example 2: Training vs. evaluation mode
Dropout behaves differently during training and evaluation:
Example
import
torch
import
torch.
nn
as
nn
dropout
=
nn.
Dropout
(
p
=
0.5
)
Training mode
dropout.
train
(
)
train_output
=
dropout
(
torch.
ones
(
4
,
10
)
)
(
"Training mode - activation ratio:"
,
(
train_output
!=
0
)
.
float
(
)
.
mean
(
)
.
item
(
)
)
Evaluation mode
dropout.
eval
(
)
eval_output
=
dropout
(
torch.
ones
(
4
,
10
)
)
(
"Evaluation mode - activation ratio:"
,
(
eval_output
!=
0
)
.
float
(
)
.
mean
(
)
.
item
(
)
)
(
"Evaluation mode output:"
,
eval_output
[
0
]
.
tolist
(
)
)
During evaluation, Dropout has no effect and the output remains unchanged.
Example 3: Use in a neural network
A typical fully connected network with Dropout:
Example
import
torch
import
torch.
nn
as
nn
class
DropoutNet
(
nn.
Module
)
:
def
init
(
self
,
input_dim
=
784
,
hidden_dim
=
256
,
output_dim
=
10
,
dropout_rate
=
0.5
)
:
super
(
DropoutNet
,
self
)
.
init
(
)
self
.
fc1
=
nn.
Linear
(
input_dim
,
hidden_dim
)
self
.
dropout1
=
nn.
Dropout
(
p
=
dropout_rate
)
self
.
fc2
=
nn.
Linear
(
hidden_dim
,
hidden_dim
)
self
.
dropout2
=
nn.
Dropout
(
p
=
dropout_rate
)
self
.
fc3
=
nn.
Linear
(
hidden_dim
,
output_dim
)
self
.
relu
=
nn.
ReLU
(
)
def
forward
(
self
,
x
)
:
x
=
self
.
relu
(
self
.
fc1
(
x
)
)
x
=
self
.
dropout1
(
x
)
First Dropout
x
=
self
.
relu
(
self
.
fc2
(
x
)
)
x
=
self
.
dropout2
(
x
)
Second Dropout
x
=
self
.
fc3
(
x
)
return
x
model
=
DropoutNet
(
)
Training mode
model.
train
(
)
input_data
=
torch.
randn
(
32
,
784
)
output
=
model
(
input_data
)
(
"Training mode output shape:"
,
output.
shape
)
Evaluation mode
model.
eval
(
)
output
=
model
(
input_data
)
(
"Output shape in eval mode:"
,
output.
shape
)
Example 4: Using Dropout2d in a CNN
nn.Dropout2d Dropping entire feature maps channel by channel:
Example
import
torch
import
torch.
nn
as
nn
Dropout2d drops by channel
dropout2d
=
nn.
Dropout2d
(
p
=
0.5
)
Input: batch=1, channels=4, height=4, width=4
input_tensor
=
torch.
ones
(
1
,
4
,
4
,
4
)
dropout2d.
train
(
)
output
=
dropout2d
(
input_tensor
)
(
"Dropout2d output shape:"
,
output.
shape
)
(
"Number of non-zero channels:"
,
(
output.
sum
(
dim
=
(
2
,
3
)
)
!=
0
)
.
sum
(
)
.
item
(
)
)
Example 5: Effect of different dropout rates
Effect of dropout rate on the network:
Example
import
torch
import
torch.
nn
as
nn
for
p
in
[
0.1
,
0.3
,
0.5
,
0.7
]
:
dropout
=
nn.
Dropout
(
p
=
p
)
dropout.
train
(
)
Average over multiple runs
total_active
=
0
for
_
in
range
(
100
)
:
output
=
dropout
(
torch.
ones
(
1000
)
)
total_active +
=
(
output
!=
0
)
.
float
(
)
.
sum
(
)
.
item
(
)
avg_active
=
total_active /
100
/
1000
(
f
"p={p} - average activation ratio: {avg_active:.2%} (expected: {1-p:.2%})"
)
Comparison of Dropout types
| Type | Dropping method | Applicable scenarios |
|---|---|---|
nn.Dropout |
Randomly zero out individual elements | Fully connected layers, feature vectors |
nn.Dropout2d |
Randomly zero out entire channels | Convolutional layer feature maps |
nn.Dropout3d |
Randomly zero out entire 3D channels | 3D convolution features |
Common questions
Q1: How to choose the Dropout rate?
- 0.1-0.3: lighter regularization, suitable for large datasets
- 0.4-0.5: common default value
- 0.5+: stronger regularization, suitable for small datasets
Q2: Where to place Dropout?
Usually placed after fully connected layers and after activation functions. It can also be placed before activation functions.
Q3: Should Dropout be disabled during evaluation?
Yes, use eval mode during evaluation to model.eval() automatically disable Dropout.
Use Cases
nn.Dropout Main application scenarios include:
- Prevent overfitting: reduces dependency between neurons
- Model Ensembling: approximates the effect of multiple networks
- Fully connected layer: most commonly used in FC layers
- Feature dropout: improves model robustness
Note: Dropout is enabled during training; you must switch to eval mode during evaluation, otherwise the output will be unstable.