PyTorch torch.nn.Dropout2d Function
PyTorch torch.nn Reference Manual
torch.nn.Dropout2dis a two-dimensional Dropout module in PyTorch.
It randomly zeros entire channels, suitable for feature maps of convolutional layers.
Function Definition
torch.nn.Dropout2d(p=0.5, inplace=False)
Features
- Randomly zero out along the channel dimension
- All channels of each sample maintain a consistent dropout pattern
Usage Examples
Example 1: Basic Usage
Example
import torch
import torch.nn as nn
dropout2d = nn.Dropout2d(p=0.5)
dropout2d.train()
# Input: batch=4, channels=8, height=16, width=16
x = torch.ones(4, 8, 16, 16)
output = dropout2d(x)
# Count the number of dropped channels
non_zero_channels = (output.sum(dim=(2, 3)) != 0).float()
print("Non-zero channel ratio:", non_zero_channels.mean().item())
print("Expected about 0.5 of channels to be retained")
import torch.nn as nn
dropout2d = nn.Dropout2d(p=0.5)
dropout2d.train()
# Input: batch=4, channels=8, height=16, width=16
x = torch.ones(4, 8, 16, 16)
output = dropout2d(x)
# Count the number of dropped channels
non_zero_channels = (output.sum(dim=(2, 3)) != 0).float()
print("Non-zero channel ratio:", non_zero_channels.mean().item())
print("Expected about 0.5 of channels to be retained")
Example 2: Comparing Dropout
Example
import torch
import torch.nn as nn
dropout1d = nn.Dropout(0.5)
dropout2d = nn.Dropout2d(0.5)
# Input
x = torch.randn(2, 4, 8, 8)
# Dropout: randomly zeros individual elements
out1 = dropout1d(x)
# Dropout2d: zeros by channel
out2 = dropout2d(x)
print("Dropout shape:", out1.shape)
print("Dropout2d shape:", out2.shape)
import torch.nn as nn
dropout1d = nn.Dropout(0.5)
dropout2d = nn.Dropout2d(0.5)
# Input
x = torch.randn(2, 4, 8, 8)
# Dropout: randomly zeros individual elements
out1 = dropout1d(x)
# Dropout2d: zeros by channel
out2 = dropout2d(x)
print("Dropout shape:", out1.shape)
print("Dropout2d shape:", out2.shape)
Example 3: Using in CNN
Example
import torch
import torch.nn as nn
# CNN with Dropout2d
model = nn.Sequential(
nn.Conv2d(3, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Dropout2d(0.3), # Dropout at feature map level
nn.Conv2d(64, 128, 3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(128, 10)
)
x = torch.randn(4, 3, 32, 32)
output = model(x)
print("Input:", x.shape, "-> Output:", output.shape)
import torch.nn as nn
# CNN with Dropout2d
model = nn.Sequential(
nn.Conv2d(3, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Dropout2d(0.3), # Dropout at feature map level
nn.Conv2d(64, 128, 3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(128, 10)
)
x = torch.randn(4, 3, 32, 32)
output = model(x)
print("Input:", x.shape, "-> Output:", output.shape)
Use Cases
- Convolutional networks: Feature map level regularization
- Reduce channel dependency
Note: Dropout2d drops channels during training and has no effect during evaluation.
Other Extensions