-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
83 lines (69 loc) · 2.61 KB
/
models.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import torch
from torch import nn
from torch.distributions.multivariate_normal import MultivariateNormal
import torch.nn.functional as F
import numpy as np
def init_weights(m):
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.01)
def FFLayer(input_size, output_size):
return nn.Sequential(*[
nn.Linear(input_size, output_size),
nn.ReLU()
])
def FFDropoutLayer(input_size, output_size, dropout_p=.2):
return nn.Sequential(*[
nn.Linear(input_size, output_size),
nn.Dropout(dropout_p),
nn.ReLU()
])
class AleatoricNN(nn.Module):
def __init__(self, input_size, output_size, hidden_size, hidden_count):
super(AleatoricNN, self).__init__()
self.mu = nn.Sequential(*[
FFLayer(input_size, hidden_size),
*[FFLayer(hidden_size, hidden_size) for i in range(hidden_count)],
nn.Linear(hidden_size, output_size)
])
self.log_sigma2 = nn.Sequential(*[
FFLayer(input_size, hidden_size),
*[FFLayer(hidden_size, hidden_size) for i in range(hidden_count)],
nn.Linear(hidden_size, 1)
])
def forward(self, x):
return self.mu(x), self.log_sigma2(x)
class AleatoricLoss(torch.nn.Module):
def __init__(self, class_count=10, T=25):
super(AleatoricLoss,self).__init__()
self.mvn = MultivariateNormal(torch.zeros(class_count), torch.eye(class_count))
self.T = T
self.class_count = class_count
def forward(self, mu, log_sigma2, y):
y_hat = []
for t in range(self.T):
y_hat.append( F.log_softmax(mu + torch.exp(log_sigma2)*self.mvn.sample((len(mu),)), dim=1) - np.log(self.T))
return F.nll_loss(torch.logsumexp(torch.stack(tuple(y_hat)), dim=0), y)
def EpistemicNN(input_size, output_size, hidden_size, hidden_count):
return nn.Sequential(*[
FFDropoutLayer(input_size, hidden_size),
*[FFDropoutLayer(hidden_size, hidden_size) for i in range(hidden_count)],
nn.Linear(hidden_size, output_size)
])
class CombinedNN(nn.Module):
def __init__(self, input_size, output_size, hidden_size, hidden_count):
super(CombinedNN, self).__init__()
self.backbone = nn.Sequential(*[
FFDropoutLayer(input_size, hidden_size),
*[FFDropoutLayer(hidden_size, hidden_size) for i in range(hidden_count)]
])
self.mu = nn.Linear(hidden_size, output_size)
self.log_sigma2 = nn.Linear(hidden_size, 1)
def forward(self, x):
embedded = self.backbone(x)
return self.mu(embedded), self.log_sigma2(embedded)
def accuracy_score(p, y):
_, predicted = torch.max(p.data, 1)
total = y.size(0)
correct = (predicted == y).sum().item()
return correct/total