PyTorch
nn.Dropout 으로 dropout 레이어 넣기
쉽게가자
2020. 5. 23. 20:36
dropout은 신경망의 일반화 성능을 높이기 위해 자주 쓰이는 테크닉 중 하나이다.
신경망 구조 학습시, 레이어간 연결 중 일부를 랜덤하게 삭제하면, 여러개의 네트워크를 앙상블 하는 효과를 낼 수 있고, 이로 인해 일반화 성능이 높아진다고 한다.
파이토치에서는 nn.Dropout 클래스를 통해 간단히 dropout 레이어를 추가할 수 있다.
nn.Dropout(p=0.5)
그런데 신경망의 어떤 부분에 dropout을 적용시키면 되는걸까?
Hinton교수의 논문을 보니, 다음과 같이 hidden unit에 적용하면 된다고 한다.
참고: Hinton et. al. "Improving neural networks by preventing co-adaptation of feature detectors". 2012
코드로는 이런식으로 쓰면 된다.
class DropoutModel(nn.Module):
def __init__(self):
super(DropoutModel, self).__init__()
self.layer1 = nn.Linear(784, 1200)
self.dropout1 = nn.Dropout(0.5)
self.layer2 = nn.Linear(1200, 1200)
self.dropout2 = nn.Dropout(0.5)
self.layer3 = nn.Linear(1200, 10)
def forward(self, x):
x = F.relu(self.layer1(x))
x = self.dropout1(x)
x = F.relu(self.layer2(x))
x = self.dropout2(x)
return self.layer3(x)
위 모델을 MNIST로 훈련시키고, 정확도가 정말 개선되는지 확인해봤다.
- droupout 적용전 error rate: 2.41%
- droupout 적용후 error rate: 1.96%
당연하지만, 정말 정확도가 개선되었다!...