Dropout
Dropout
A regularization technique that randomly deactivates neurons during training. Prevents overfitting.
Dropout
A regularization technique that randomly deactivates neurons during training. Prevents overfitting.
Dropout is a regularization technique that randomly deactivates neurons (sets their output to 0) at each training iteration when training a neural network. Published by Geoffrey Hinton's team in 2012, this technique is one of the most effective and widely used methods for preventing overfitting in neural networks, and since AlexNet it has become a standard component of almost all deep learning models.
The core idea of Dropout was inspired by ensemble learning. By using a different combination of neurons at every training step, it has the effect of simultaneously training 2^n different "thinned" networks. This reduces excessive dependence on any single neuron and encourages the network to learn more robust feature representations.
The mechanism is simple. During training, each neuron is deactivated with probability p (usually 0.5 or 0.2). During inference, all neurons are used, and the expected value is matched either by multiplying the output by (1-p) (standard dropout) or by scaling the output by 1/(1-p) during training (inverted dropout). Most modern frameworks use inverted dropout, so no separate handling is needed at inference time.
In practice, Dropout is particularly effective in fully connected layers, and is mainly applied to the final FC layer of a CNN or the hidden layers of an RNN/LSTM. However, its effect may diminish when used together with BatchNorm, and in Transformer architectures it is generally applied after Attention. In modern CNNs since ResNet, BatchNorm provides sufficient regularization, so the use of Dropout has decreased, but it remains essential in FC layers and language models.
# PyTorch에서 Dropout 사용 예제
import torch
import torch.nn as nn
class MLPWithDropout(nn.Module):
"""Dropout이 적용된 MLP 분류기"""
def __init__(self, input_size, hidden_size, num_classes, dropout_rate=0.5):
super().__init__()
self.layer1 = nn.Linear(input_size, hidden_size)
self.layer2 = nn.Linear(hidden_size, hidden_size)
self.layer3 = nn.Linear(hidden_size, num_classes)
self.relu = nn.ReLU()
# Dropout 레이어 (학습 시에만 활성화)
self.dropout = nn.Dropout(p=dropout_rate)
def forward(self, x):
x = self.relu(self.layer1(x))
x = self.dropout(x) # 첫 번째 hidden layer 후 dropout
x = self.relu(self.layer2(x))
x = self.dropout(x) # 두 번째 hidden layer 후 dropout
x = self.layer3(x) # 출력층에는 dropout 적용 안 함
return x
# 모델 생성
model = MLPWithDropout(784, 256, 10, dropout_rate=0.5)
# 학습 모드 vs 평가 모드
model.train() # Dropout 활성화
output_train = model(torch.randn(32, 784))
model.eval() # Dropout 비활성화 (모든 뉴런 사용)
output_eval = model(torch.randn(32, 784))
# 2D Dropout (CNN용)
class CNNWithDropout(nn.Module):
def __init__(self):
super().__init__()
self.conv = nn.Conv2d(3, 64, 3, padding=1)
self.dropout2d = nn.Dropout2d(p=0.25) # 채널 단위 dropout
self.fc = nn.Linear(64 * 32 * 32, 10)
self.dropout = nn.Dropout(p=0.5)
def forward(self, x):
x = torch.relu(self.conv(x))
x = self.dropout2d(x) # Conv 후 2D dropout
x = x.view(x.size(0), -1)
x = self.dropout(x) # FC 전 dropout
return self.fc(x)
print("Dropout 모델 구성 완료!")
"If training accuracy is 98% but validation accuracy is 85%, that is severe overfitting. Add Dropout 0.5 to the FC layer and apply data augmentation as well. Dropout alone usually improves validation accuracy by 2-4%."
"The model.eval() call is missing here. At inference time, you must switch to eval mode so that Dropout is deactivated. Otherwise, you get different results on every inference, and accuracy drops as well."
"Inverted Dropout scales the output up by 1/(1-p) during training so that no separate handling is needed at inference time. With p=0.5, the output is multiplied by 2 during training and used as is during inference, so the expected value stays the same."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
If you do not call model.eval() in PyTorch, Dropout remains active even at inference time, resulting in inconsistent results and performance degradation. Use it together with torch.no_grad().
Applying Dropout immediately after a BatchNorm layer can degrade performance at inference time due to distribution mismatch. In general, using only Conv-BN-ReLU is recommended over the Conv-BN-ReLU-Dropout order.
Too high a Dropout rate (0.7 or above) interferes with training, and too low a value (0.1 or below) has a negligible regularization effect. Start with 0.5 for FC layers and 0.2-0.3 for Conv layers, and adjust from there.