elevne's Study Note
딥러닝 파이토치 교과서 (6-3: VGGNet (2)) 본문
저번 시간에는 모델 정의 및 데이터를 불러오는 파트까지 실습을 진행해보았다. 그 후, 모델에 사용할 최적화 함수와 손실 함수를 정의해야 했다.
optimizer = optim.Adam(model.parameters(), lr=1e-7)
criterion = nn.CrossEntropyLoss()
model = model.to(device)
criterion = criterion.to(device)
그 후, 훈련을 진행하며 모델에 대한 정확도를 측정하기 위한 함수도 작성해둔다.
def calculate_accuracy(y_pred, y):
top_pred = y_pred.argmax(1, keepdim=True)
correct = top_pred.eq(y.view_as(top_pred)).sum()
acc = correct.float() / y.shape[0]
return acc
위에서 사용되는 eq 메서드는 equal 의 약자로 서로 같은지를 비교해주는 표현식이다. 또, view_as(other) 메서드가 사용되었다. 이는 other 의 텐서 크기를 사용하겠다는 의미로, view(other.size()) 와 같은 의미이다. 즉, 텐서 크기를 주어진 텐서의 크기로 변경하는 것이다.
이제 모델을 학습을 위한 함수를 정의한다.
def train(model, iterator, optimizer, criterion, device):
epoch_loss = 0
epoch_acc = 0
model.train()
for (x, y) in iterator:
x = x.to(device)
y = y.to(device)
optimizer.zero_grad()
y_pred, _ = model(x)
loss = criterion(y_pred, y)
acc = calculate_accuracy(y_pred, y)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator)
여기에 더해, 검증 및 테스트 데이터셋을 이용한 모델 성능을 측정하는 함수와 모델의 학습 시간을 측정하기 위한 함수도 작성한다.
def evaluate(model, iterator, criterion, device):
epoch_loss = 0
epoch_acc = 0
model.eval()
with torch.no_grad():
for (x, y) in iterator:
x = x.to(device)
y = y.to(device)
y_pred, _ = model(x)
loss = criterion(y_pred, y)
acc = calculate_accuracy(y_pred, y)
epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator)
def epoch_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
이제 모델을 학습시킬 준비가 전부 되었다. 아래 코드를 통해 학습을 진행한다.
EPOCHS = 5
best_valid_loss = float("inf")
for epoch in range(EPOCHS):
start_time = time.monotonic()
train_loss, train_acc = train(model, train_iterator, optimizer, criterion, device)
valid_loss, valid_acc = evaluate(model, valid_iterator, criterion, device)
if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(model.state_dict(), "../data/VGG_model.pt")
end_time = time.monotonic()
epoch_mins, epoch_secs = epoch_time(start_time, end_time)
print(f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s')
print(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')
print(f'\t Valid. Loss: {valid_loss:.3f} | Valid. Acc: {valid_acc*100:.2f}%')
위에서 학습시킨 모델을, 테스트 데이터셋을 사용하여 성능을 다시 한 번 측정해볼 수 있다.
test_loss, test_acc = evaluate(model, test_iterator, criterion, device)
print(f'Test Loss: {test_loss:.3f} | Test Acc: {test_acc*100:.2f}%')
Reference:
딥러닝 파이토치 교과서
'Machine Learning > Pytorch' 카테고리의 다른 글
딥러닝 파이토치 교과서 (6-4: ResNet(2)) (0) | 2023.03.28 |
---|---|
딥러닝 파이토치 교과서 (6-4: GoogLeNet, ResNet(1)) (0) | 2023.03.22 |
딥러닝 파이토치 교과서 (6-3: VGGNet (1)) (0) | 2023.03.20 |
딥러닝 파이토치 교과서 (6-2: AlexNet) (0) | 2023.03.16 |
딥러닝 파이토치 교과서 (6-1: LeNet) (1) | 2023.03.15 |