-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_GatCNNPPIS.py
367 lines (283 loc) · 12.3 KB
/
model_GatCNNPPIS.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
"""
@Time:Created on 2022/11/09 22:00
@author: Minjie Mou
@Filename: model.py
@Software: PyCharm
"""
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import math
import numpy as np
from sklearn.metrics import roc_auc_score, precision_score, recall_score,precision_recall_curve, auc
from Radam import *
from lookahead import Lookahead
import timeit
class SelfAttention2(nn.Module):
def __init__(self, hid_dim, n_heads, dropout, device):
super().__init__()
self.hid_dim = hid_dim
self.n_heads = n_heads
assert hid_dim % n_heads == 0
self.w_q = nn.Linear(hid_dim, hid_dim)
self.w_k = nn.Linear(hid_dim, hid_dim)
self.w_v = nn.Linear(hid_dim, hid_dim)
self.fc = nn.Linear(hid_dim, hid_dim)
self.do = nn.Dropout(dropout)
self.scale = torch.sqrt(torch.FloatTensor([hid_dim // n_heads])).to(device)
def forward(self, query, key, value, mask=None):
bsz = query.shape[0]
# query = key = value [batch size, sent len, hid dim]
Q = self.w_q(query)
K = self.w_k(key)
V = self.w_v(value)
# Q, K, V = [batch size, sent len, hid dim]
Q = Q.view(bsz, -1, self.n_heads, self.hid_dim // self.n_heads).permute(0, 2, 1, 3)
K = K.view(bsz, -1, self.n_heads, self.hid_dim // self.n_heads).permute(0, 2, 1, 3)
V = V.view(bsz, -1, self.n_heads, self.hid_dim // self.n_heads).permute(0, 2, 1, 3)
# K, V = [batch size, n heads, sent len_K, hid dim // n heads]
# Q = [batch size, n heads, sent len_q, hid dim // n heads]
energy = torch.matmul(Q, K.permute(0, 1, 3, 2)) / self.scale
# energy = [batch size, n heads, sent len_Q, sent len_K]
if mask is not None:
energy = energy.masked_fill(mask == 0, -1e10)
attention = self.do(F.softmax(energy, dim=-1))
# attention = [batch size, n heads, sent len_Q, sent len_K]
x = torch.matmul(attention, V)
# x = [batch size, n heads, sent len_Q, hid dim // n heads]
x = x.permute(0, 2, 1, 3).contiguous()
# x = [batch size, sent len_Q, n heads, hid dim // n heads]
x = x.view(bsz, -1, self.n_heads * (self.hid_dim // self.n_heads))
# x = [batch size, src sent len_Q, hid dim]
x = self.fc(x)
# x = [batch size, sent len_Q, hid dim]
return x, attention
class Encoder2(nn.Module):
"""protein feature extraction."""
def __init__(self, protein_dim, hid_dim, n_layers, kernel_size, dropout, device):
super().__init__()
assert kernel_size % 2 == 1, "Kernel size must be odd (for now)"
self.input_dim = protein_dim
self.hid_dim = hid_dim
self.kernel_size = kernel_size
self.dropout = dropout
self.n_layers = n_layers
self.device = device
#self.pos_embedding = nn.Embedding(1000, hid_dim)
self.scale = torch.sqrt(torch.FloatTensor([0.5])).to(device)
self.convs = nn.ModuleList([nn.Conv1d(hid_dim, 2*hid_dim, kernel_size, padding=(kernel_size-1)//2) for _ in range(self.n_layers)]) # convolutional layers
self.dropout = nn.Dropout(dropout)
self.fc = nn.Linear(self.input_dim, self.hid_dim)
self.gn = nn.GroupNorm(8, hid_dim * 2)
self.ln = nn.LayerNorm(hid_dim)
def forward(self, protein):
conv_input = self.fc(protein)
# conv_input=[batch size,protein len,hid dim]
#permute for convolutional layer
conv_input = conv_input.permute(0, 2, 1)
#conv_input = [batch size, hid dim, protein len]
for i, conv in enumerate(self.convs):
#pass through convolutional layer
conved = conv(self.dropout(conv_input))
#conved = [batch size, 2*hid dim, protein len]
#pass through GLU activation function
conved = F.glu(conved, dim=1)
#conved = [batch size, hid dim, protein len]
#apply residual connection / high way
conved = (conved + conv_input) * self.scale
#conved = [batch size, hid dim, protein len]
#set conv_input to conved for next loop iteration
conv_input = conved
conved = conved.permute(0, 2, 1)
# conved = [batch size,protein len,hid dim]
conved = self.ln(conved)
return conved
class PositionwiseFeedforward2(nn.Module):
def __init__(self, hid_dim, pf_dim, dropout):
super().__init__()
self.hid_dim = hid_dim
self.pf_dim = pf_dim
self.fc_1 = nn.Conv1d(hid_dim, pf_dim, 1) # convolution neural units
self.fc_2 = nn.Conv1d(pf_dim, hid_dim, 1) # convolution neural units
self.do = nn.Dropout(dropout)
def forward(self, x):
# x = [batch size, sent len, hid dim]
x = x.permute(0, 2, 1)
# x = [batch size, hid dim, sent len]
x = self.do(F.relu(self.fc_1(x)))
# x = [batch size, pf dim, sent len]
x = self.fc_2(x)
# x = [batch size, hid dim, sent len]
x = x.permute(0, 2, 1)
# x = [batch size, sent len, hid dim]
return x
class DecoderLayer2(nn.Module):
def __init__(self, hid_dim, n_heads, pf_dim, self_attention, positionwise_feedforward, dropout, device):
super().__init__()
self.ln = nn.LayerNorm(hid_dim)
self.sa = self_attention(hid_dim, n_heads, dropout, device)
self.ea = self_attention(hid_dim, n_heads, dropout, device)
self.pf = positionwise_feedforward(hid_dim, pf_dim, dropout)
self.do = nn.Dropout(dropout)
def forward(self, trg, src, trg_mask=None, src_mask=None):
# trg = [batch_size, local len, local_dim]
# src = [batch_size, protein len, hid_dim] # encoder output
# trg_mask = [batch size, local sent len]
# src_mask = [batch size, protein len]
trg_1 = trg
trg, k = self.sa(trg, trg, trg, trg_mask)
trg = self.ln(trg_1 + self.do(trg))
trg_2 = trg
trg, attention = self.ea(trg, src, src, src_mask)
trg = self.ln(trg_2 + self.do(trg))
trg_3 = trg
trg = self.ln(trg_3 + self.do(self.pf(trg)))
return trg,attention
class Decoder2(nn.Module):
def __init__(self, local_dim, hid_dim, n_layers, n_heads, pf_dim, decoder_layer, self_attention,
positionwise_feedforward, dropout, device):
super().__init__()
self.ln = nn.LayerNorm(hid_dim)
self.output_dim = local_dim
self.hid_dim = hid_dim
self.n_layers = n_layers
self.n_heads = n_heads
self.pf_dim = pf_dim
self.decoder_layer = decoder_layer
self.self_attention = self_attention
self.positionwise_feedforward = positionwise_feedforward
self.dropout = dropout
self.device = device
self.sa = self_attention(hid_dim, n_heads, dropout, device)
self.layers = nn.ModuleList(
[decoder_layer(hid_dim, n_heads, pf_dim, self_attention, positionwise_feedforward, dropout, device)
for _ in range(n_layers)])
self.ft = nn.Linear(local_dim, hid_dim)
self.do = nn.Dropout(dropout)
self.fc_1 = nn.Linear(hid_dim, 256)
self.fc_2 = nn.Linear(256, 64)
self.fc_3 = nn.Linear(64, 2)
self.gn = nn.GroupNorm(8, 256)
def forward(self, trg, index):
"""Use norm to determine which atom is significant. """
norm = torch.norm(trg, dim=2)
# norm = [batch size,local len]
norm = F.softmax(norm, dim=1)
# norm = [batch size,local len]
# trg = torch.squeeze(trg,dim=0)
# norm = torch.squeeze(norm,dim=0)
sum = torch.zeros((trg.shape[0], self.hid_dim)).to(self.device)
for i in range(norm.shape[0]):
v = trg[i, index[i], ]
v = v * norm[i, index[i]]
sum[i, ] += v
label = F.relu(self.fc_1(sum))
label = self.do(label)
label = F.relu(self.fc_2(label))
label = self.fc_3(label)
return sum, label
class Predictor2(nn.Module):
def __init__(self, encoder, decoder, device):
super().__init__()
self.encoder = encoder
self.decoder = decoder
self.device = device
def forward(self, protein, index):
enc_src = self.encoder(protein)
# enc_src = [batch size, protein len, hid dim]
sum, out = self.decoder.forward(enc_src, index)
return sum, out
def __call__(self, data, train=True):
Loss = nn.CrossEntropyLoss(weight=torch.from_numpy(np.array([1, 5])).float().to(self.device))
if train:
protein, index, correct_interaction = data
sum, predicted_interaction = self.forward(protein, index)
loss2 = Loss(predicted_interaction, correct_interaction)
return loss2
else:
protein, index = data
sum, predicted_interaction = self.forward(protein, index)
ys = F.softmax(predicted_interaction, 1).to('cpu').data.numpy()
predicted_labels = np.argmax(ys, axis=1)
predicted_scores = ys[:, 1]
return predicted_labels, predicted_scores
def todevice(proteins, index, labels, device):
# locals_new = torch.Tensor(locals).to(device)
proteins_new = torch.Tensor(proteins).to(device)
labels_new = torch.from_numpy(labels).to(device)
index = index
return (proteins_new, index, labels_new)
class Trainer2(object):
def __init__(self, model, lr, weight_decay):
self.model = model
# w - L2 regularization ; b - not L2 regularization
weight_p, bias_p = [], []
for p in self.model.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
for name, p in self.model.named_parameters():
if 'bias' in name:
bias_p += [p]
else:
weight_p += [p]
# self.optimizer = optim.Adam([{'params': weight_p, 'weight_decay': weight_decay}, {'params': bias_p, 'weight_decay': 0}], lr=lr)
self.optimizer_inner = RAdam(
[{'params': weight_p, 'weight_decay': weight_decay}, {'params': bias_p, 'weight_decay': 0}], lr=lr)
self.optimizer = Lookahead(self.optimizer_inner, k=5, alpha=0.5)
def train(self, dataloader, device):
self.model.train()
#np.random.shuffle(dataset)
loss_total = 0
i = 0
self.optimizer.zero_grad()
for batch_idx, (protein, index, label) in enumerate(dataloader):
print(batch_idx)
# print(protein.shape)
# print(label)
data_pack = todevice(protein, index, label, device)
loss = self.model(data_pack)
# print(loss)
# loss = loss1 + loss2
loss.backward()
self.optimizer.step()
self.optimizer.zero_grad()
loss_total += loss.item()
return loss_total
class Tester2(object):
def __init__(self, model):
self.model = model
def test(self, dataloader, device):
self.model.eval()
T, Y, S = [], [], []
with torch.no_grad():
for batch_idx, (protein, index, label) in enumerate(dataloader):
print(batch_idx)
data_pack = todevice(protein, index, label, device)
correct_labels, predicted_labels, predicted_scores = self.model(data_pack, train=False)
T.extend(correct_labels)
Y.extend(predicted_labels)
S.extend(predicted_scores)
return T, Y, S
def save_AUCs(self, AUCs, filename):
with open(filename, 'a') as f:
f.write('\t'.join(map(str, AUCs)) + '\n')
def save_model(self, model, filename):
torch.save(model.module.state_dict(), filename)
class Predictor_test2(object):
def __init__(self, model):
self.model = model
def test(self, dataloader, device):
self.model.eval()
Y, S = [], []
with torch.no_grad():
for batch_idx, (protein, index) in enumerate(dataloader):
print(batch_idx)
proteins_new = torch.Tensor(protein).to(device)
index = index
data_pack = (proteins_new, index)
predicted_labels, predicted_scores = self.model(data_pack, train=False)
Y.extend(predicted_labels)
S.extend(predicted_scores)
return Y, S