-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
47 lines (35 loc) · 1.51 KB
/
helper.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
import torch
def accuracy(output, target, topk=(1,)):
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res[0].item() if len(res) == 1 else [r.item() for r in res]
def save_ckpt(episode, metalearner, optim, save):
if not os.path.exists(os.path.join(save, 'ckpts')):
os.mkdir(os.path.join(save, 'ckpts'))
torch.save({
'episode': episode,
'metalearner': metalearner.state_dict(),
'optim': optim.state_dict()
}, os.path.join(save, 'ckpts', 'meta-learner-{}.pth.tar'.format(episode)))
def resume_ckpt(metalearner, optim, resume, device):
ckpt = torch.load(resume, map_location=device)
last_episode = ckpt['episode']
metalearner.load_state_dict(ckpt['metalearner'])
optim.load_state_dict(ckpt['optim'])
return last_episode, metalearner, optim
def preprocess_grad_loss(x):
p = 10
indicator = (x.abs() >= np.exp(-p)).to(torch.float32)
# preproc1
x_proc1 = indicator * torch.log(x.abs() + 1e-8) / p + (1 - indicator) * -1
# preproc2
x_proc2 = indicator * torch.sign(x) + (1 - indicator) * np.exp(p) * x
return torch.stack((x_proc1, x_proc2), 1)