-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
129 lines (104 loc) · 4.95 KB
/
models.py
File metadata and controls
129 lines (104 loc) · 4.95 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from layers import GraphConvolution, Fastformer
class GCN(nn.Module):
def __init__(self, voc_size, emb_dim, adj, device=torch.device('cpu:0')):
super(GCN, self).__init__()
self.voc_size = voc_size
self.emb_dim = emb_dim
self.device = device
adj = self.normalize(adj + np.eye(adj.shape[0]))
self.adj = torch.FloatTensor(adj).to(device)
self.x = torch.eye(voc_size).to(device)
self.gcn1 = GraphConvolution(voc_size, emb_dim)
self.dropout = nn.Dropout(p=0.3)
self.gcn2 = GraphConvolution(emb_dim, emb_dim)
def forward(self):
node_embedding = self.gcn1(self.x, self.adj)
node_embedding = F.relu(node_embedding)
node_embedding = self.dropout(node_embedding)
node_embedding = self.gcn2(node_embedding, self.adj)
return node_embedding
def normalize(self, mx):
"""Row-normalize sparse matrix"""
rowsum = np.array(mx.sum(1))
r_inv = np.power(rowsum, -1).flatten()
r_inv[np.isinf(r_inv)] = 0.
r_mat_inv = np.diagflat(r_inv)
mx = r_mat_inv.dot(mx)
return mx
class FastRx(nn.Module):
def __init__(self, vocab_size, ehr_adj, ddi_adj, emb_dim=256, device=torch.device('cpu:0')):
super(FastRx, self).__init__()
self.device = device
self.emb_dim = emb_dim
self.vocab_size = vocab_size
self.emb_dim_ff = 128
self.fastformer = Fastformer(dim = 2*self.emb_dim_ff, decode_dim = self.emb_dim)
self.dropout = nn.Dropout(p=0.2)
self.ehr_gcn = GCN(voc_size=vocab_size[2], emb_dim=emb_dim, adj=ehr_adj, device=device)
self.ddi_gcn = GCN(voc_size=vocab_size[2], emb_dim=emb_dim, adj=ddi_adj, device=device)
self.inter = nn.Parameter(torch.FloatTensor(1))
self.embedding = nn.Embedding(vocab_size[0] + vocab_size[1] + 2, self.emb_dim_ff)
# graphs, bipartite matrix
self.tensor_ddi_adj = torch.FloatTensor(ddi_adj).to(device)
self.cnn1d = nn.Sequential(
nn.Conv1d(1, 1, kernel_size=3, padding='same', stride=1),
nn.ReLU(),
nn.Dropout(0.2)
)
self.output = nn.Sequential(
nn.Linear(emb_dim * 3, emb_dim * 2),
nn.ReLU(),
nn.Linear(emb_dim * 2, vocab_size[2])
)
def forward(self, input):
# patient health representation
i1_seq, i2_seq = [], []
def mean_embedding(embedding):
return embedding.mean(dim=1).unsqueeze(dim=0) # (1,1,dim)
for adm in input:
i1 = mean_embedding(self.dropout(self.embedding(torch.LongTensor(adm[0]).unsqueeze(dim=0).to(self.device))))
i2 = mean_embedding(self.dropout(self.embedding(torch.LongTensor(adm[1]).unsqueeze(dim=0).to(self.device))))
i1_seq.append(i1)
i2_seq.append(i2)
i1_seq = torch.cat(i1_seq, dim=1) #(1,seq,dim)
i2_seq = torch.cat(i2_seq, dim=1) #(1,seq,dim)
i1_seq = self.cnn1d(i1_seq.permute(1, 0, 2))
i2_seq = self.cnn1d(i2_seq.permute(1, 0, 2))
i1_seq = i1_seq.permute(1, 0, 2)
i2_seq = i2_seq.permute(1, 0, 2)
h = torch.cat([i1_seq, i2_seq], dim=-1) # (seq, dim*2)
mask = torch.ones(1, self.emb_dim).to(torch.bool).to(self.device)
feat = self.fastformer(h, mask).squeeze(0)
# graph memory module
'''I:generate current input'''
query = feat[-1:] # (1,dim)
'''G:generate graph memory bank and insert history information'''
drug_memory = self.ehr_gcn() - self.ddi_gcn() * self.inter # (size, dim)
if len(input) > 1:
history_keys = feat[:(feat.size(0)-1)] # (seq-1, dim)
history_values = np.zeros((len(input)-1, self.vocab_size[2]))
for idx, adm in enumerate(input):
if idx == len(input)-1:
break
history_values[idx, adm[2]] = 1
history_values = torch.FloatTensor(history_values).to(self.device) # (seq-1, size)
'''O:read from global memory bank and dynamic memory bank'''
# print(query.shape, drug_memory.t().shape)
key_weights1 = F.softmax(torch.mm(query, drug_memory.t()), dim=-1) # (1, size)
fact1 = torch.mm(key_weights1, drug_memory) # (1, dim)
if len(input) > 1:
visit_weight = F.softmax(torch.mm(query, history_keys.t())) # (1, seq-1)
weighted_values = visit_weight.mm(history_values) # (1, size)
fact2 = torch.mm(weighted_values, drug_memory) # (1, dim)
else:
fact2 = fact1
'''R:convert O and predict'''
result = self.output(torch.cat([query, fact1, fact2], dim=-1)) # (1, dim)
neg_pred_prob = F.sigmoid(result)
neg_pred_prob = neg_pred_prob.t() * neg_pred_prob # (voc_size, voc_size)
batch_neg = 0.0005 * neg_pred_prob.mul(self.tensor_ddi_adj).sum()
return result, batch_neg