-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathutils.py
66 lines (55 loc) · 2.18 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 as nn
import pdb
class CumulativeProbabilityLayer(nn.Module):
"""
The cumulative layer which defines the monotonically increasing risk scores.
"""
def __init__(self, num_features, max_followup, args):
super(CumulativeProbabilityLayer, self).__init__()
self.args = args
self.hazard_fc = nn.Linear(num_features, max_followup)
self.base_hazard_fc = nn.Linear(num_features, 1)
self.relu = nn.ReLU(inplace=True)
mask = torch.ones([max_followup, max_followup])
mask = torch.tril(mask, diagonal=0)
mask = torch.nn.Parameter(torch.t(mask), requires_grad=False)
self.register_parameter('upper_triagular_mask', mask)
def hazards(self, x):
raw_hazard = self.hazard_fc(x)
pos_hazard = self.relu(raw_hazard)
return pos_hazard
def forward(self, x):
hazards = self.hazards(x)
B, T = hazards.size() # hazards is (B, T)
expanded_hazards = hazards.unsqueeze(-1).expand(B, T, T) # expanded_hazards is (B,T, T)
masked_hazards = expanded_hazards * self.upper_triagular_mask # masked_hazards now (B,T, T)
cum_prob = torch.sum(masked_hazards, dim=1) + self.base_hazard_fc(x)
return cum_prob
class OneHotLayer(nn.Module):
"""
One-hot embedding for categorical inputs.
"""
def __init__(self, num_classes, padding_idx):
super(OneHotLayer, self).__init__()
self.num_classes = num_classes
self.embed = nn.Embedding(num_classes, num_classes, padding_idx=padding_idx)
self.embed.weight.data = torch.eye(num_classes)
self.embed.weight.requires_grad_(False)
def forward(self, x):
return self.embed(x)
class AttributionModel(nn.Module):
"""
Model wrapper for posthoc attribution analyses.
"""
def __init__(self, model, args):
super().__init__()
if isinstance(model, dict):
self.model = model[args.model_name]
else:
self.model = model
def forward(self, x, age_seq, time_seq, batch):
batch['age_seq'] = age_seq
batch['time_seq'] = time_seq
y = self.model(x, batch=batch)
return y