-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
53 lines (41 loc) · 1.5 KB
/
utils.py
File metadata and controls
53 lines (41 loc) · 1.5 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
import torch
import numpy as np
import torch.nn as nn
from torch import Tensor
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
torch.manual_seed(0)
def epoch(loader, model, opt=None, lr_scheduler=None):
total_loss, total_err = 0.,0.
model.eval() if opt is None else model.train()
for X,y in loader:
X,y = X.to(device), y.to(device)
yp = model(X)
loss = nn.CrossEntropyLoss()(yp,y)
if opt:
opt.zero_grad()
loss.backward()
opt.step()
lr_scheduler.step()
total_err += (yp.max(dim=1)[1] != y).sum().item()
total_loss += loss.item() * X.shape[0]
total_err = total_err / len(loader.dataset)
total_loss = total_loss / len(loader.dataset)
return 100.*total_err, 100.*total_loss
def epoch_eval(loader, model, opt=None, lr_scheduler=None):
total_loss, total_err = 0.,0.
with torch.no_grad():
for X,y in loader:
X,y = X.to(device), y.to(device)
yp = model(X)
loss = nn.CrossEntropyLoss()(yp,y)
total_err += (yp.max(dim=1)[1] != y).sum().item()
total_loss += loss.item() * X.shape[0]
total_err = total_err / len(loader.dataset)
total_loss = total_loss / len(loader.dataset)
return 100.*total_err, 100.*total_loss
def cuda(tensor,device=device):
if torch.cuda.is_available():
return tensor.to(device)
else:
return tensor
#device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")