-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel.py
237 lines (206 loc) · 9.57 KB
/
model.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
'''
code by Brandon Theodorou
Original GPT-2 Paper and repository here: https://github.com/openai/gpt-2
Original GPT-2 Pytorch Model: https://github.com/huggingface/pytorch-pretrained-BERT
GPT-2 Pytorch Model Derived From: https://github.com/graykode/gpt-2-Pytorch
'''
import copy
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
def gelu(x):
return 0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3))))
class LayerNorm(nn.Module):
def __init__(self, hidden_size, eps=1e-12):
"""Construct a layernorm module in the TF style (epsilon inside the square root)."""
super(LayerNorm, self).__init__()
self.weight = nn.Parameter(torch.ones(hidden_size))
self.bias = nn.Parameter(torch.zeros(hidden_size))
self.variance_epsilon = eps
def forward(self, x):
u = x.mean(-1, keepdim=True)
s = (x - u).pow(2).mean(-1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.variance_epsilon)
return self.weight * x + self.bias
class Conv1D(nn.Module):
def __init__(self, nf, nx):
super(Conv1D, self).__init__()
self.nf = nf
w = torch.empty(nx, nf)
nn.init.normal_(w, std=0.02)
self.weight = nn.Parameter(w)
self.bias = nn.Parameter(torch.zeros(nf))
def forward(self, x):
size_out = x.size()[:-1] + (self.nf,)
x = torch.addmm(self.bias, x.view(-1, x.size(-1)), self.weight)
x = x.view(*size_out)
return x
class Attention(nn.Module):
def __init__(self, nx, n_ctx, config, scale=False):
super(Attention, self).__init__()
n_state = nx # in Attention: n_state=768 (nx=n_embd)
assert n_state % config.n_head == 0
self.register_buffer("bias", torch.tril(torch.ones(n_ctx, n_ctx)).view(1, 1, n_ctx, n_ctx))
self.n_head = config.n_head
self.split_size = n_state
self.scale = scale
self.c_attn = Conv1D(n_state * 3, nx)
self.c_proj = Conv1D(n_state, nx)
def _attn(self, q, k, v):
w = torch.matmul(q, k)
if self.scale:
w = w / math.sqrt(v.size(-1))
nd, ns = w.size(-2), w.size(-1)
b = self.bias[:, :, ns-nd:ns, :ns]
w = w * b - 1e10 * (1 - b)
w = nn.Softmax(dim=-1)(w)
return torch.matmul(w, v)
def merge_heads(self, x):
x = x.permute(0, 2, 1, 3).contiguous()
new_x_shape = x.size()[:-2] + (x.size(-2) * x.size(-1),)
return x.view(*new_x_shape)
def split_heads(self, x, k=False):
new_x_shape = x.size()[:-1] + (self.n_head, x.size(-1) // self.n_head)
x = x.view(*new_x_shape)
if k:
return x.permute(0, 2, 3, 1) # (batch, head, head_features, seq_length)
else:
return x.permute(0, 2, 1, 3) # (batch, head, seq_length, head_features)
def forward(self, x, layer_past=None):
x = self.c_attn(x)
query, key, value = x.split(self.split_size, dim=2)
query = self.split_heads(query)
key = self.split_heads(key, k=True)
value = self.split_heads(value)
if layer_past is not None:
past_key, past_value = layer_past[0].transpose(-2, -1), layer_past[1] # transpose back cf below
key = torch.cat((past_key, key), dim=-1)
value = torch.cat((past_value, value), dim=-2)
present = torch.stack((key.transpose(-2, -1), value)) # transpose to have same shapes for stacking
a = self._attn(query, key, value)
a = self.merge_heads(a)
a = self.c_proj(a)
return a, present
class MLP(nn.Module):
def __init__(self, n_state, config): # in MLP: n_state=3072 (4 * n_embd)
super(MLP, self).__init__()
nx = config.n_embd
self.c_fc = Conv1D(n_state, nx)
self.c_proj = Conv1D(nx, n_state)
self.act = gelu
def forward(self, x):
h = self.act(self.c_fc(x))
h2 = self.c_proj(h)
return h2
class Block(nn.Module):
def __init__(self, n_ctx, config, scale=False):
super(Block, self).__init__()
nx = config.n_embd
self.ln_1 = LayerNorm(nx, eps=config.layer_norm_epsilon)
self.attn = Attention(nx, n_ctx, config, scale)
self.ln_2 = LayerNorm(nx, eps=config.layer_norm_epsilon)
self.mlp = MLP(4 * nx, config)
def forward(self, x, layer_past=None):
a, present = self.attn(self.ln_1(x), layer_past=layer_past)
x = x + a
m = self.mlp(self.ln_2(x))
x = x + m
return x, present
class CoarseTransformerModel(nn.Module):
def __init__(self, config):
super(CoarseTransformerModel, self).__init__()
self.n_layer = config.n_layer
self.n_embd = config.n_embd
self.n_vocab = config.total_vocab_size
self.vis_embed_mat = nn.Linear(config.total_vocab_size, config.n_embd, bias=False)
self.pos_embed_mat = nn.Embedding(config.n_positions, config.n_embd)
block = Block(config.n_ctx, config, scale=True)
self.h = nn.ModuleList([copy.deepcopy(block) for _ in range(config.n_layer)])
self.ln_f = LayerNorm(config.n_embd, eps=config.layer_norm_epsilon)
def forward(self, input_visits, position_ids=None, past=None):
if past is None:
past_length = 0
past = [None] * len(self.h)
else:
past_length = past[0][0].size(-2)
if position_ids is None:
position_ids = torch.arange(past_length, input_visits.size(1) + past_length, dtype=torch.long,
device=input_visits.device)
position_ids = position_ids.unsqueeze(0).expand(input_visits.size(0), input_visits.size(1))
inputs_embeds = self.vis_embed_mat(input_visits)
position_embeds = self.pos_embed_mat(position_ids)
hidden_states = inputs_embeds + position_embeds
for block, layer_past in zip(self.h, past):
hidden_states, _ = block(hidden_states, layer_past)
hidden_states = self.ln_f(hidden_states)
return hidden_states
class AutoregressiveLinear(nn.Linear):
""" same as Linear except has a configurable mask on the weights """
def __init__(self, in_features, out_features, bias=True):
super().__init__(in_features, out_features, bias)
self.register_buffer('mask', torch.tril(torch.ones(in_features, out_features)).int())
def forward(self, input):
return F.linear(input, self.mask * self.weight, self.bias)
class FineAutoregressiveHead(nn.Module):
def __init__(self, config):
super(FineAutoregressiveHead, self).__init__()
self.auto1 = AutoregressiveLinear(config.n_embd + config.total_vocab_size, config.n_embd + config.total_vocab_size)
self.auto2 = AutoregressiveLinear(config.n_embd + config.total_vocab_size, config.n_embd + config.total_vocab_size)
self.n_embd = config.n_embd
self.tot_vocab = config.total_vocab_size
def forward(self, history, input_visits):
history = history[:,:-1,:]
input_visits = input_visits[:,1:,:]
code_logits = self.auto2(torch.relu(self.auto1(torch.cat((history, input_visits), dim=2))))[:,:,self.n_embd-1:-1]
return code_logits
def sample(self, history, input_visits):
history = history[:,:-1,:]
input_visits = input_visits[:,1:,:]
currVisit = torch.cat((history, input_visits), dim=2)[:,-1,:].unsqueeze(1)
code_logits = self.auto2(torch.relu(self.auto1(currVisit)))[:,:,self.n_embd-1:-1]
return code_logits
class HALOModel(nn.Module):
def __init__(self, config):
super(HALOModel, self).__init__()
self.transformer = CoarseTransformerModel(config)
self.ehr_head = FineAutoregressiveHead(config)
def forward(self, input_visits, position_ids=None, ehr_labels=None, ehr_masks=None, past=None, pos_loss_weight=None):
hidden_states = self.transformer(input_visits, position_ids, past)
code_logits = self.ehr_head(hidden_states, input_visits)
sig = nn.Sigmoid()
code_probs = sig(code_logits)
if ehr_labels is not None:
shift_labels = ehr_labels[..., 1:, :].contiguous()
loss_weights = None
if pos_loss_weight is not None:
loss_weights = torch.ones(code_probs.shape, device=code_probs.device)
loss_weights = loss_weights + (pos_loss_weight-1) * shift_labels
if ehr_masks is not None:
code_probs = code_probs * ehr_masks
shift_labels = shift_labels * ehr_masks
if pos_loss_weight is not None:
loss_weights = loss_weights * ehr_masks
bce = nn.BCELoss(weight=loss_weights)
loss = bce(code_probs, shift_labels)
return loss, code_probs, shift_labels
return code_probs
def sample(self, input_visits, random=True):
sig = nn.Sigmoid()
hidden_states = self.transformer(input_visits)
i = 0
while i < self.ehr_head.tot_vocab:
next_logits = self.ehr_head.sample(hidden_states, input_visits)
next_probs = sig(next_logits)
if random:
visit = torch.bernoulli(next_probs)
else:
visit = torch.round(next_probs)
remaining_visit = visit[:,0,i:]
nonzero = torch.nonzero(remaining_visit, as_tuple=True)[1]
if nonzero.numel() == 0:
break
first_nonzero = nonzero.min()
input_visits[:,-1,i + first_nonzero] = visit[:,0,i + first_nonzero]
i = i + first_nonzero + 1
return input_visits