-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
265 lines (173 loc) · 7.81 KB
/
layers.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
import torch
import torch.nn as nn
from torch.nn import functional as F
import math
class MLP(nn.Module):
def __init__(self, in_feat, hid_feat=None, out_feat=None,
dropout=0.):
super().__init__()
if not hid_feat:
hid_feat = in_feat
if not out_feat:
out_feat = in_feat
self.fc1 = nn.Linear(in_feat, hid_feat)
self.act = torch.nn.ReLU()
self.fc2 = nn.Linear(hid_feat,out_feat)
self.droprateout = nn.Dropout(dropout)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.fc2(x)
return self.droprateout(x)
class Attention_new(nn.Module):
def __init__(self, dim, heads, attention_dropout=0.):
super().__init__()
assert dim % heads == 0
self.heads = heads
self.scale = 1./dim**0.5
self.q = nn.Linear(dim, dim)
self.k = nn.Linear(dim, dim)
self.v = nn.Linear(dim, dim)
self.e = nn.Linear(dim, dim)
#self.attention_dropout = nn.Dropout(attention_dropout)
self.d_k = dim // heads
self.heads = heads
self.out_e = nn.Linear(dim,dim)
self.out_n = nn.Linear(dim, dim)
def forward(self, node, edge):
b, n, c = node.shape
q_embed = self.q(node).view(-1, n, self.heads, c//self.heads)
k_embed = self.k(node).view(-1, n, self.heads, c//self.heads)
v_embed = self.v(node).view(-1, n, self.heads, c//self.heads)
e_embed = self.e(edge).view(-1, n, n, self.heads, c//self.heads)
q_embed = q_embed.unsqueeze(2)
k_embed = k_embed.unsqueeze(1)
attn = q_embed * k_embed
attn = attn/ math.sqrt(self.d_k)
attn = attn * (e_embed + 1) * e_embed
edge = self.out_e(attn.flatten(3))
attn = F.softmax(attn, dim=2)
v_embed = v_embed.unsqueeze(1)
v_embed = attn * v_embed
v_embed = v_embed.sum(dim=2).flatten(2)
node = self.out_n(v_embed)
return node, edge
class Encoder_Block(nn.Module):
def __init__(self, dim, heads,act, mlp_ratio=4, drop_rate=0.):
super().__init__()
self.ln1 = nn.LayerNorm(dim)
self.attn = Attention_new(dim, heads, drop_rate)
self.ln3 = nn.LayerNorm(dim)
self.ln4 = nn.LayerNorm(dim)
self.mlp = MLP(dim, dim*mlp_ratio, dim, dropout=drop_rate)
self.mlp2 = MLP(dim, dim*mlp_ratio, dim, dropout=drop_rate)
self.ln5 = nn.LayerNorm(dim)
self.ln6 = nn.LayerNorm(dim)
def forward(self, x,y):
x1 = self.ln1(x)
x2,y1 = self.attn(x1,y)
x2 = x1 + x2
y2 = y1 + y
x2 = self.ln3(x2)
y2 = self.ln4(y2)
x = self.ln5(x2 + self.mlp(x2))
y = self.ln6(y2 + self.mlp2(y2))
return x, y
class TransformerEncoder(nn.Module):
def __init__(self, dim, depth, heads, act, mlp_ratio=4, drop_rate=0.1):
super().__init__()
self.Encoder_Blocks = nn.ModuleList([
Encoder_Block(dim, heads, act, mlp_ratio, drop_rate)
for i in range(depth)])
def forward(self, x,y):
for Encoder_Block in self.Encoder_Blocks:
x, y = Encoder_Block(x,y)
return x, y
class enc_dec_attention(nn.Module):
def __init__(self, dim, heads, attention_dropout=0., proj_dropout=0.):
super().__init__()
self.dim = dim
self.heads = heads
self.scale = 1./dim**0.5
"query is molecules"
"key is prot"
"values is again molecule"
self.q_mx = nn.Linear(dim,dim)
self.k_px = nn.Linear(dim,dim)
self.v_mx = nn.Linear(dim,dim)
self.k_pa = nn.Linear(dim,dim)
self.v_ma = nn.Linear(dim,dim)
#self.dropout_dec = nn.Dropout(proj_dropout)
self.out_nd = nn.Linear(dim, dim)
self.out_ed = nn.Linear(dim,dim)
def forward(self, mol_annot, prot_annot, mol_adj, prot_adj):
b, n, c = mol_annot.shape
_, m, _ = prot_annot.shape
query_mol_annot = self.q_mx(mol_annot).view(-1,m, self.heads, c//self.heads)
key_prot_annot = self.k_px(prot_annot).view(-1,n, self.heads, c//self.heads)
value_mol_annot = self.v_mx(mol_annot).view(-1,m, self.heads, c//self.heads)
mol_e = self.v_ma(mol_adj).view(-1,m,m, self.heads, c//self.heads)
prot_e = self.k_pa(prot_adj).view(-1,m,m, self.heads, c//self.heads)
query_mol_annot = query_mol_annot.unsqueeze(2)
key_prot_annot = key_prot_annot.unsqueeze(1)
#attn = torch.einsum('bnchd,bmahd->bnahd', query_mol_annot, key_prot_annot)
attn = query_mol_annot * key_prot_annot
attn = attn/ math.sqrt(self.dim)
attn = attn * (prot_e + 1) * mol_e
mol_e_new = attn.flatten(3)
mol_adj = self.out_ed(mol_e_new)
attn = F.softmax(attn, dim=2)
value_mol_annot = value_mol_annot.unsqueeze(1)
value_mol_annot = attn * value_mol_annot
value_mol_annot = value_mol_annot.sum(dim=2).flatten(2)
mol_annot = self.out_nd(value_mol_annot)
return mol_annot, prot_annot, mol_adj, prot_adj
class Decoder_Block(nn.Module):
def __init__(self, dim, heads, mlp_ratio=4, drop_rate=0.):
super().__init__()
self.ln1_ma = nn.LayerNorm(dim)
self.ln1_pa = nn.LayerNorm(dim)
self.ln1_mx = nn.LayerNorm(dim)
self.ln1_px = nn.LayerNorm(dim)
self.attn2 = Attention_new(dim, heads, drop_rate)
self.ln2_pa = nn.LayerNorm(dim)
self.ln2_px = nn.LayerNorm(dim)
self.dec_attn = enc_dec_attention(dim, heads, drop_rate, drop_rate)
self.ln3_ma = nn.LayerNorm(dim)
self.ln3_mx = nn.LayerNorm(dim)
self.mlp_ma = MLP(dim, dim, dropout=drop_rate)
self.mlp_mx = MLP(dim, dim, dropout=drop_rate)
self.ln4_ma = nn.LayerNorm(dim)
self.ln4_mx = nn.LayerNorm(dim)
def forward(self,mol_annot, prot_annot, mol_adj, prot_adj):
mol_annot = self.ln1_mx(mol_annot)
mol_adj = self.ln1_ma(mol_adj)
prot_annot = self.ln1_px(prot_annot)
prot_adj = self.ln1_pa(prot_adj)
px1, pa1= self.attn2(prot_annot, prot_adj)
prot_annot = prot_annot + px1
prot_adj = prot_adj + pa1
prot_annot = self.ln2_px(prot_annot)
prot_adj = self.ln2_pa(prot_adj)
mx1, prot_annot, ma1, prot_adj = self.dec_attn(mol_annot,prot_annot,mol_adj,prot_adj)
ma1 = mol_adj + ma1
mx1 = mol_annot + mx1
ma2 = self.ln3_ma(ma1)
mx2 = self.ln3_mx(mx1)
ma3 = self.mlp_ma(ma2)
mx3 = self.mlp_mx(mx2)
ma = ma3 + ma2
mx = mx3 + mx2
mol_adj = self.ln4_ma(ma)
mol_annot = self.ln4_mx(mx)
return mol_annot, prot_annot, mol_adj, prot_adj
class TransformerDecoder(nn.Module):
def __init__(self, dim, depth, heads, mlp_ratio=4, drop_rate=0.):
super().__init__()
self.Decoder_Blocks = nn.ModuleList([
Decoder_Block(dim, heads, mlp_ratio, drop_rate)
for i in range(depth)])
def forward(self, mol_annot, prot_annot, mol_adj, prot_adj):
for Decoder_Block in self.Decoder_Blocks:
mol_annot, prot_annot, mol_adj, prot_adj = Decoder_Block(mol_annot, prot_annot, mol_adj, prot_adj)
return mol_annot, prot_annot,mol_adj, prot_adj