-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
59 lines (51 loc) · 2.11 KB
/
utils.py
File metadata and controls
59 lines (51 loc) · 2.11 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
"""Utility functions used for the Domain Adaptation code."""
import time
import torch
import numpy as np
def train(dataloader, model, loss_fun, optimizer, num_epochs, device, params=None, report_every=1, report_acc=True):
size = len(dataloader.dataset)
t0 = time.perf_counter()
model.train()
for epoch in range(num_epochs):
# print(f"Epoch {epoch+1}\n-------------------------------")
for batch, (X, y) in enumerate(dataloader):
X, y = X.to(device), one_hot(y, model.num_classes).to(device)
# TODO: Is this necessary?
X = X.contiguous()
loss, params = step(optimizer, model, params, loss_fun, X, y)
if batch % report_every == 0:
loss, current = loss.item(), (batch + 1) * len(X)
print(f"loss: {loss:>7f} epoch:{epoch+1} [{current:>5d}/{size:>5d}]")
if report_acc is True:
print("Train dataset metrics:")
test(dataloader, model, loss_fun, device)
def test(dataloader, model, loss_fun, device):
size = len(dataloader.dataset)
num_batches = len(dataloader)
# model.eval()
test_loss, correct = 0, 0
with torch.no_grad():
for X, y in dataloader:
X, y = X.to(device), y.to(device)
pred = model(X)
test_loss += loss_fun(pred, y).item()
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
test_loss /= num_batches
correct /= size
print(f"Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
return test_loss, correct
# Convenient wrapper for stepping with first order AND custom second order methods
def step(optimizer, model, params, loss_fun, X, y):
opt = optimizer["method"]
# TODO: Check if optimizer is custom!
if optimizer["name"] == "SGD" or optimizer["name"] == "ADAM":
loss = loss_fun(model(X), y)
loss.backward()
opt.step()
opt.zero_grad()
return loss, None
else:
params, loss = opt.step(model, params, loss_fun, X, y)
return loss, params
def one_hot(x, k):
return torch.nn.functional.one_hot(x, k).float()