
1) 활성화 함수 ReLU와 Dropout을 내장하는 커스텀 선형 계층을 만들고, 그것을 이용해서 MLP 작성
# 모듈 만들기 class CustomLinear(nn.Module): def __init__(self, in_features, out_features, bias=True, p=0.5): super().__init__() self.linear = nn.Linear(in_features, out_features, bias) self.relu = nn.ReLU() self.drop = nn.Dropout(p)
def forward(self, x): x = self.linear(x) x = self.relu(x) x = self.drop(x) return x |
# 모듈로 네트워크 구성 mlp = mm.Sequential( CustomLinear(64, 200), CustomLinear(200, 200), CustomLinear(200, 200), nn.Linear(200, 10) ) |
2) nn.Module을 상속한 클래스 이용
class MyMLP(nn.Module): def __init__(self, in_features, out_features): super().__init__() self.ln1 = CustomLinear(in_features, 200) self.ln2 = CustomLinear(200, 200) self.ln3 = CustomLinear(200, 200) self.ln4 = CustomLinear(200, out_features)
def forward(self, x): x = self.ln1(x) x = self.ln2(x) x = self.ln3(x) x = self.ln4(x) return x
mlp = MyMLP(64, 10) |
'인공지능 > 파이토치' 카테고리의 다른 글
파이토치 - 전이 학습 (0) | 2020.07.22 |
---|---|
파이토치 - CNN을 사용한 이미지 분류 (Fashion-MNIST) (0) | 2020.07.21 |
파이토치 - Dropout과 Batch Normalization (0) | 2020.07.20 |
파이토치 - Dataset과 DataLoader (0) | 2020.07.20 |
파이토치 - MLP 구축과 학습 (0) | 2020.07.20 |