-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
288 lines (228 loc) · 11 KB
/
model.py
File metadata and controls
288 lines (228 loc) · 11 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from dnc import DNC
from layers import GraphConvolution
import math
from torch.nn.parameter import Parameter
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 MaskLinear(nn.Module):
def __init__(self, in_features, out_features, bias=True):
super(MaskLinear, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.FloatTensor(in_features, out_features))
if bias:
self.bias = Parameter(torch.FloatTensor(out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input, mask):
weight = torch.mul(self.weight, mask)
output = torch.mm(input, weight)
if self.bias is not None:
return output + self.bias
else:
return output
def __repr__(self):
return self.__class__.__name__ + ' (' \
+ str(self.in_features) + ' -> ' \
+ str(self.out_features) + ')'
class MolecularGraphNeuralNetwork(nn.Module):
def __init__(self, N_fingerprint, dim, layer_hidden, device):
super(MolecularGraphNeuralNetwork, self).__init__()
self.device = device
self.embed_fingerprint = nn.Embedding(N_fingerprint, dim).to(self.device)
self.W_fingerprint = nn.ModuleList([nn.Linear(dim, dim).to(self.device)
for _ in range(layer_hidden)])
self.layer_hidden = layer_hidden
def pad(self, matrices, pad_value):
"""Pad the list of matrices
with a pad_value (e.g., 0) for batch proc essing.
For example, given a list of matrices [A, B, C],
we obtain a new matrix [A00, 0B0, 00C],
where 0 is the zero (i.e., pad value) matrix.
"""
shapes = [m.shape for m in matrices]
M, N = sum([s[0] for s in shapes]), sum([s[1] for s in shapes])
zeros = torch.FloatTensor(np.zeros((M, N))).to(self.device)
pad_matrices = pad_value + zeros
i, j = 0, 0
for k, matrix in enumerate(matrices):
m, n = shapes[k]
pad_matrices[i:i+m, j:j+n] = matrix
i += m
j += n
return pad_matrices
def update(self, matrix, vectors, layer):
hidden_vectors = torch.relu(self.W_fingerprint[layer](vectors))
return hidden_vectors + torch.mm(matrix, hidden_vectors)
def sum(self, vectors, axis):
sum_vectors = [torch.sum(v, 0) for v in torch.split(vectors, axis)]
return torch.stack(sum_vectors)
def mean(self, vectors, axis):
mean_vectors = [torch.mean(v, 0) for v in torch.split(vectors, axis)]
return torch.stack(mean_vectors)
def forward(self, inputs):
"""Cat or pad each input data for batch processing."""
fingerprints, adjacencies, molecular_sizes = inputs
fingerprints = torch.cat(fingerprints)
adjacencies = self.pad(adjacencies, 0)
"""MPNN layer (update the fingerprint vectors)."""
fingerprint_vectors = self.embed_fingerprint(fingerprints)
for l in range(self.layer_hidden):
hs = self.update(adjacencies, fingerprint_vectors, l)
# fingerprint_vectors = F.normalize(hs, 2, 1) # normalize.
fingerprint_vectors = hs
"""Molecular vector by sum or mean of the fingerprint vectors."""
molecular_vectors = self.sum(fingerprint_vectors, molecular_sizes)
# molecular_vectors = self.mean(fingerprint_vectors, molecular_sizes)
return molecular_vectors
class GAMENetplusplus(nn.Module):
def __init__(self, vocab_size, ehr_adj, ddi_adj, ddi_mask_H, MPNNSet, N_fingerprints, average_projection, emb_dim=64, device=torch.device('cpu:0'), ddi_in_memory=True):
super(GAMENetplusplus, self).__init__()
K = len(vocab_size)
self.K = K
self.vocab_size = vocab_size
self.device = device
self.tensor_ddi_adj = torch.FloatTensor(ddi_adj).to(device)
self.ddi_in_memory = ddi_in_memory
self.embeddings = nn.ModuleList(
[nn.Embedding(vocab_size[i], emb_dim) for i in range(K-1)])
self.dropout = nn.Dropout(p=0.5)
self.encoders = nn.ModuleList([nn.GRU(emb_dim, emb_dim * 2, batch_first=True) for _ in range(K-1)])
self.query = nn.Sequential(
nn.ReLU(),
nn.Linear(emb_dim * 4, emb_dim),
)
self.query1 = nn.Sequential(
nn.ReLU(),
nn.Linear(2 * emb_dim, emb_dim)
)
# bipartite local embedding
self.bipartite_transform = nn.Sequential(
nn.Linear(emb_dim, ddi_mask_H.shape[1])
)
self.bipartite_output = MaskLinear(ddi_mask_H.shape[1], vocab_size[2], False)
# MPNN global embedding
self.MPNN_molecule_Set = list(zip(*MPNNSet))
self.MPNN_emb = MolecularGraphNeuralNetwork(N_fingerprints, emb_dim, layer_hidden=2, device=device).forward(self.MPNN_molecule_Set)
self.MPNN_emb = torch.mm(average_projection.to(device=self.device), self.MPNN_emb.to(device=self.device))
self.MPNN_emb.to(device=self.device)
# self.MPNN_emb = torch.tensor(self.MPNN_emb, requires_grad=True)
self.MPNN_output = nn.Linear(vocab_size[2], vocab_size[2])
self.MPNN_layernorm = nn.LayerNorm(vocab_size[2])
self.tensor_ddi_mask_H = torch.FloatTensor(ddi_mask_H).to(device)
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.output = nn.Sequential(
nn.ReLU(),
nn.Linear((emb_dim * 3)+112, emb_dim * 2),
nn.ReLU(),
nn.Linear((emb_dim * 2), vocab_size[2])
)
self.init_weights()
def forward(self, input):
# input (adm, 3, codes)
# generate medical embeddings and queries
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.embeddings[0](torch.LongTensor(adm[0]).unsqueeze(dim=0).to(self.device)))) # (1,1,dim)
i2 = mean_embedding(self.dropout(self.embeddings[1](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)
o1, h1 = self.encoders[0](
i1_seq
) # o1:(1, seq, dim*2) hi:(1,1,dim*2)
o2, h2 = self.encoders[1](
i2_seq
)
patient_representations = torch.cat([o1, o2], dim=-1).squeeze(dim=0) # (seq, dim*4)
queries = self.query(patient_representations) # (seq, dim)
# graph memory module
'''I:generate current input'''
query = queries[-1:] # (1,dim)
query1 = self.query(patient_representations)[-1:, :]
# MPNN embedding
MPNN_match = F.sigmoid(torch.mm(query1, self.MPNN_emb.t()))
MPNN_att = self.MPNN_layernorm(MPNN_match + self.MPNN_output(MPNN_match))
# local embedding
bipartite_emb = self.bipartite_output(F.sigmoid(self.bipartite_transform(query1)), self.tensor_ddi_mask_H.t())
result = torch.mul(bipartite_emb, MPNN_att)
'''G:generate graph memory bank and insert history information'''
if self.ddi_in_memory:
drug_memory = self.ehr_gcn() - self.ddi_gcn() * self.inter # (size, dim)
else:
drug_memory = self.ehr_gcn()
if len(input) > 1:
history_keys = queries[:(queries.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'''
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'''
# print(result)
output = self.output(torch.cat([query, fact1, fact2, result], dim=-1)) # (1, dim)
# output = torch.cat((output, result), dim=0).mean(dim=0)
# print(output)
if self.training:
neg_pred_prob = F.sigmoid(output)
neg_pred_prob = neg_pred_prob.t() * neg_pred_prob # (voc_size, voc_size)
batch_neg = neg_pred_prob.mul(self.tensor_ddi_adj).mean()
return output, batch_neg
else:
return output
def init_weights(self):
"""Initialize weights."""
initrange = 0.1
for item in self.embeddings:
item.weight.data.uniform_(-initrange, initrange)
self.inter.data.uniform_(-initrange, initrange)