Official Docs: https://pytorch.org/docs/stable/generated/torch.nn.Dropout.html
Supported Version: PyTorch 2.11 (based on official docs)
1. What it does
torch.nn.Dropout is a regularization layer that prevents overfitting by randomly disabling (setting to zero) neurons during training. It sets each element to zero with a specified probability p and scales the remaining elements by 1/(1-p).
2. When to use it
When you want to prevent overfitting, where accuracy is high on training data but low on test data
When you want to add regularization to the hidden layers of an MLP or Transformer
When the model is large and has many parameters
3. How to use it (minimal code)
import torch
import torch.nn as nn
✅ Dropout(確率p):p の割合でランダムに0にする
dropout = nn.Dropout(p=0.5)
x = torch.ones(4, 8)
y = dropout(x) # 学習時:約50%が0になる
print(y)
MLP での使い方
model = nn.Sequential(
nn.Linear(4, 64),
nn.ReLU(),
nn.Dropout(p=0.3), # ← ReLUの後
nn.Linear(64, 32),
nn.ReLU(),
nn.Dropout(p=0.3),
nn.Linear(32, 2) # 最終層にはDropoutを入れない
)学習時:p=0.5 → 約50%の要素が0になり、残りは 1/(1-0.5)=2倍 にスケーリング 推論時:Dropoutは無効化(全要素がそのまま通る)
4. Common pitfalls
1. If you forget model.eval(), Dropout will remain active during inference
Calling model.eval() during inference automatically disables Dropout. If you forget, the results will change randomly every time.
model.eval() # ← 推論前に必ず呼ぶ
with torch.no_grad():
pred = model(x)2. Guidelines for choosing p
Small data/prone to overfitting: 0.3 to 0.5
Large data/less prone to overfitting: 0.1 to 0.2
After Transformer Attention: around 0.1
3. Do not use Dropout on the final layer (output layer)
Since the output would be randomly set to zero, it is not used immediately before the final layer.
4. Don't forget to switch back to model.train()
If you resume training after evaluation without calling model.train(), training will proceed with Dropout disabled.
# 学習ループのパターン
for epoch in range(epochs):
model.train() # ← 学習モード
for X, y in train_loader:
...
model.eval() # ← 評価モード
with torch.no_grad():
for X, y in val_loader:
...5. Common errors and solutions
Error Example(Not an error, but a behavioral issue) Results change every time you perform inference
Cause:
model.eval() was not called. Dropout remains active during inference.
Solution:
Call model.eval() before inference.
(Not an error, but a behavioral issue) Loss stopped decreasing after adding Dropout
Cause:
p is too high (e.g., 0.7 to 0.9). Too much information is lost, making learning impossible.
Solution:
Try smaller values for p, such as 0.1 to 0.3.
⑥ A Quick Tip
⑦ Related Functions
torch.nn.Dropout2d — Dropout for CNN channels
torch.nn.BatchNorm1d — Another approach to regularization
torch.nn.Module.train() — Switch to training mode
⑧ Version Notes
No major breaking changes. inplace=False (default). inplace=True saves memory, but can cause issues with autograd, so avoid using it until you are more experienced.
[For those who want to learn by doing]
Don't just read; I have published an article with an exercise notebook that you can actually run and verify (free).
▼ Exercise: Implementing MLP in PyTorch | Writing classification and regression with the same framework [Basics ①, Free Notebook]
https://note.com/technosend/n/nb3888e2dd2e1
[About this series]
"Learn PyTorch in 3 Minutes" is a series that explains PyTorch APIs one by one in a format that can be read in 3 minutes. It is updated on Mondays, Wednesdays, and Fridays, with a total of 32 articles planned.
▼ Follow the magazine to receive new updates
Learn PyTorch in 3 Minutes
https://note.com/technosend/m/m84d841b6d067