과적합을 해결하는 방법으로 드롭아웃(dropout)이 있습니다.

    신경망 모델이 과적합되는 것을 피하기 위한 방법으로, 학습 과정 중 임의로 일부 노드들을 학습에서 제외시킵니다.

    ▲ 그림 4-15 일반적인 신경망과 드롭아웃이 적용된 신경망

    다음은 파이토치에서 드롭아웃을 구현하는 예시 코드입니다.

    class DropoutModel(torch.nn.Module):
        def __init__(self):
            super(DropoutModel, self).__init__()
            self.layer1 = torch.nn.Linear(784, 1200)
            self.dropout1 = torch.nn.Dropout(0.5) ------ 50%의 노드를 무작위로 선택하여 사용하지 않겠다는 의미
            self.layer2 = torch.nn.Linear(1200, 1200)
            self.dropout2 = torch.nn.Dropout(0.5)
            self.layer3 = torch.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)
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.