-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
67 lines (49 loc) · 1.91 KB
/
utils.py
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
60
61
62
63
64
65
66
import torch
import torch.nn.functional as F
from datetime import datetime
def process_arg(args, arg):
if arg in ['gpu', 'eval_sharpness', 'log', 'rewrite']:
return ''
if arg == 'adaptive':
return ''
if arg != 'model_path':
return str(getattr(args, arg))
# return args.model_path.split('/')[-1][:24].replace(' ', '_')
return ''
def get_path(args, log_folder):
name = '-'.join([process_arg(args, arg) for arg in list(filter(lambda x: x not in ['adaptive'], vars(args)))])
name = str(datetime.now())[:-3].replace(' ', '_') + name
if getattr(args, 'adaptive'):
name += '-adaptive'
path = f'{log_folder}/{name}.json'
return path
def zero_grad(model):
for p in model.parameters():
if p.grad is not None:
p.grad.zero_()
def compute_err(batches, model, loss_f=F.cross_entropy, n_batches=-1):
n_wrong_classified, train_loss_sum, n_ex = 0, 0.0, 0
with torch.no_grad():
for i, (X, _, y, _, ln) in enumerate(batches):
if n_batches != -1 and i > n_batches: # limit to only n_batches
break
X, y = X.cuda(), y.cuda()
# print(X, X.shape)
output = model(X)
loss = loss_f(output, y)
n_wrong_classified += (output.max(1)[1] != y).sum().item()
train_loss_sum += loss.item() * y.size(0)
n_ex += y.size(0)
err = n_wrong_classified / n_ex
avg_loss = train_loss_sum / n_ex
return err, avg_loss
def estimate_loss_err(model, batches, loss_f):
err = 0
loss = 0
with torch.no_grad():
for i_batch, (x, _, y, _, _) in enumerate(batches):
x, y = x.cuda(), y.cuda()
curr_y = model(x)
loss += loss_f(curr_y, y)
err += (curr_y.max(1)[1] != y).float().mean().item()
return loss.item() / len(batches), err / len(batches)