-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
executable file
·61 lines (43 loc) · 2 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
# Max-Heinrich Laves
# Institute of Mechatronic Systems
# Leibniz Universität Hannover, Germany
# 2019
import torch
__all__ = ['leaky_relu1',
'normal_init',
'xavier_normal_init',
'kaiming_normal_init', 'kaiming_uniform_init',
'nll_criterion_gaussian', 'nll_criterion_laplacian',
'save_current_snapshot']
def leaky_relu1(x, slope=0.1, a=1):
x = torch.nn.functional.leaky_relu(x, negative_slope=slope)
x = -torch.nn.functional.leaky_relu(-x+a, negative_slope=slope)+a
return x
def normal_init(m):
if isinstance(m, torch.nn.Conv2d) or isinstance(m, torch.nn.Linear):
torch.nn.init.normal_(m.weight.data)
def xavier_normal_init(m):
if isinstance(m, torch.nn.Conv2d) or isinstance(m, torch.nn.Linear):
torch.nn.init.xavier_normal_(m.weight.data)
def kaiming_normal_init(m):
if isinstance(m, torch.nn.Conv2d) or isinstance(m, torch.nn.Linear):
torch.nn.init.kaiming_normal_(m.weight.data)
def kaiming_uniform_init(m):
if isinstance(m, torch.nn.Conv2d) or isinstance(m, torch.nn.Linear):
torch.nn.init.kaiming_uniform_(m.weight.data)
def nll_criterion_gaussian(mu, logvar, target, reduction='mean'):
loss = torch.exp(-logvar) * torch.pow(target-mu, 2).mean(dim=1, keepdim=True) + logvar
return loss.mean() if reduction == 'mean' else loss.sum()
def nll_criterion_laplacian(mu, logsigma, target, reduction='mean'):
loss = torch.exp(-logsigma) * torch.abs(target-mu).mean(dim=1, keepdim=True) + logsigma
return loss.mean() if reduction == 'mean' else loss.sum()
def save_current_snapshot(base_model, likelihood, dataset, e, model, optimizer_net, train_losses, valid_losses):
filename = f"./snapshots/{base_model}_{likelihood}_{dataset}_{e}.pth.tar"
print(f"Saving at epoch: {e}")
torch.save({
'epoch': e,
'state_dict': model.state_dict(),
'optimizer': optimizer_net.state_dict(),
'train_losses': train_losses,
'val_losses': valid_losses,
}, filename)