Ensemble
- 앙상블 학습이란?
앙상블 학습(ensemble learning)은 여러 개의 분류기를 생성하고, 그 예측을 결합하는 것이다.
강력한 하나의 모델을 사용하는 대신 약한 모델 여러 개를 조합하여 정확한 예측할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import torch.nn as nn
import torch
class MyEnsemble(nn.Module):
def __init__(self, modelA, modelB, modelC, output):
super(MyEnsemble, self).__init__()
self.modelA = modelA
self.modelB = modelB
self.modelC = modelC
self.fc1 = nn.Linear(num_classes, output)
def forward(self,x):
out1 = self.modelA(x)
out2 = self.modelB(x)
out3 = self.modelC(x)
out = out1 + out2 + out3
x = torch.softmax(x, dim=1)
return x
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
import torchvision.models as models
densenet161 = models.densenet161(pretrained=True).to(device)
resnet101 = models.resnet101(pretrained=True).to(device)
googlenet = models.googlenet(pretrained=True).to(device)
densenet161.classifier = nn.Linear(in_features=densenet161.classifier.in_features, out_features=num_classes)
resnet101.fc = nn.Linear(in_features=resnet101.fc.in_features, out_features=num_classes)
googlenet.fc = nn.Linear(in_features=googlenet.fc.in_features, out_features=num_classes)
densenet161 = densenet161.to(device)
resnet101 = resnet101.to(device)
googlenet = googlenet.to(device)
densenet161, history, best_epoch = train_and_val(densenet161, criterion, optimizer, dataloader['train'], dataloader['valid'], 2)
densenet = [densenet161, history, best_epoch]
resnet101, history, best_epoch = train_and_val(resnet101, criterion, optimizer, dataloader['train'], dataloader['valid'], 2)
resnet = [resnet101, history, best_epoch]
googlenet, history, best_epoch = train_and_val(googlenet, criterion, optimizer, dataloader['train'], dataloader['valid'], 2)
googlenet = [googlenet, history, best_epoch]
ensemble = MyEnsemble(densenet[1], resnet[1], googlenet[1], 10)