-
Notifications
You must be signed in to change notification settings - Fork 11
/
models.py
353 lines (276 loc) · 14.3 KB
/
models.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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_sparse import SparseTensor
from torch_geometric.nn import global_mean_pool, global_add_pool, radius, knn
from torch_geometric.utils import remove_self_loops
from layers import Global_MessagePassing, Local_MessagePassing, Local_MessagePassing_s, \
BesselBasisLayer, SphericalBasisLayer, MLP
class Config(object):
def __init__(self, dataset, dim, n_layer, cutoff_l, cutoff_g, flow='source_to_target'):
self.dataset = dataset
self.dim = dim
self.n_layer = n_layer
self.cutoff_l = cutoff_l
self.cutoff_g = cutoff_g
self.flow = flow
class PAMNet(nn.Module):
def __init__(self, config: Config, num_spherical=7, num_radial=6, envelope_exponent=5):
super(PAMNet, self).__init__()
self.dataset = config.dataset
self.dim = config.dim
self.n_layer = config.n_layer
self.cutoff_l = config.cutoff_l
self.cutoff_g = config.cutoff_g
if self.dataset[:3].lower() == "rna":
self.embeddings = nn.Parameter(torch.ones((3, self.dim))) # only C, N, O atoms for RNA
else:
self.embeddings = nn.Parameter(torch.ones((5, self.dim)))
self.init_linear = nn.Linear(18, self.dim, bias=False)
self.rbf_g = BesselBasisLayer(16, self.cutoff_g, envelope_exponent)
self.rbf_l = BesselBasisLayer(16, self.cutoff_l, envelope_exponent)
self.sbf = SphericalBasisLayer(num_spherical, num_radial, self.cutoff_l, envelope_exponent)
self.mlp_rbf_g = MLP([16, self.dim])
self.mlp_rbf_l = MLP([16, self.dim])
self.mlp_sbf1 = MLP([num_spherical * num_radial, self.dim])
self.mlp_sbf2 = MLP([num_spherical * num_radial, self.dim])
self.global_layer = torch.nn.ModuleList()
for _ in range(config.n_layer):
self.global_layer.append(Global_MessagePassing(config))
self.local_layer = torch.nn.ModuleList()
for _ in range(config.n_layer):
self.local_layer.append(Local_MessagePassing(config))
self.softmax = nn.Softmax(dim=-1)
self.init()
def init(self):
stdv = math.sqrt(3)
self.embeddings.data.uniform_(-stdv, stdv)
def get_edge_info(self, edge_index, pos):
edge_index, _ = remove_self_loops(edge_index)
j, i = edge_index
dist = (pos[i] - pos[j]).pow(2).sum(dim=-1).sqrt()
return edge_index, dist
def indices(self, edge_index, num_nodes):
row, col = edge_index
value = torch.arange(row.size(0), device=row.device)
adj_t = SparseTensor(row=col, col=row, value=value,
sparse_sizes=(num_nodes, num_nodes))
adj_t_row = adj_t[row]
num_triplets = adj_t_row.set_value(None).sum(dim=1).to(torch.long)
idx_i = col.repeat_interleave(num_triplets)
idx_j = row.repeat_interleave(num_triplets)
idx_k = adj_t_row.storage.col()
mask = idx_i != idx_k # Remove i == k triplets.
idx_i, idx_j, idx_k = idx_i[mask], idx_j[mask], idx_k[mask]
idx_kj = adj_t_row.storage.value()[mask]
idx_ji = adj_t_row.storage.row()[mask]
adj_t_col = adj_t[col]
num_pairs = adj_t_col.set_value(None).sum(dim=1).to(torch.long)
idx_i_pair = row.repeat_interleave(num_pairs)
idx_j1_pair = col.repeat_interleave(num_pairs)
idx_j2_pair = adj_t_col.storage.col()
mask_j = idx_j1_pair != idx_j2_pair # Remove j == j' triplets.
idx_i_pair, idx_j1_pair, idx_j2_pair = idx_i_pair[mask_j], idx_j1_pair[mask_j], idx_j2_pair[mask_j]
idx_ji_pair = adj_t_col.storage.row()[mask_j]
idx_jj_pair = adj_t_col.storage.value()[mask_j]
return idx_i, idx_j, idx_k, idx_kj, idx_ji, idx_i_pair, idx_j1_pair, idx_j2_pair, idx_jj_pair, idx_ji_pair
def forward(self, data):
x_raw = data.x
batch = data.batch
if self.dataset == "QM9":
edge_index_l = data.edge_index
pos = data.pos
x = torch.index_select(self.embeddings, 0, x_raw.long())
# Compute pairwise distances in global layer
row, col = radius(pos, pos, self.cutoff_g, batch, batch, max_num_neighbors=1000)
edge_index_g = torch.stack([row, col], dim=0)
edge_index_g, dist_g = self.get_edge_info(edge_index_g, pos)
# Compute pairwise distances in local layer
edge_index_l, dist_l = self.get_edge_info(edge_index_l, pos)
elif self.dataset == "PDBbind":
x_raw = x_raw.unsqueeze(-1) if x_raw.dim() == 1 else x_raw
x = self.init_linear(x_raw[:, 3:])
pos = x_raw[:,:3].contiguous()
# Indices for computing energy difference
pos_index = torch.ones_like(pos[:, 0])
neg_index = torch.ones_like(pos[:, 0]) * (-1.0)
all_index = torch.where(pos[:, 0] > 40.0, neg_index, pos_index)
# Compute pairwise distances in global layer
row, col = radius(pos, pos, self.cutoff_g, batch, batch, max_num_neighbors=1000)
edge_index_g = torch.stack([row, col], dim=0)
edge_index_g, dist_g = self.get_edge_info(edge_index_g, pos)
# Compute pairwise distances in local layer
tensor_l = torch.ones_like(dist_g, device=dist_g.device) * self.cutoff_l
mask_l = dist_g <= tensor_l
edge_index_l = edge_index_g[:, mask_l]
edge_index_l, dist_l = self.get_edge_info(edge_index_l, pos)
elif self.dataset[:3].lower() == "rna":
x_raw = x_raw.unsqueeze(-1) if x_raw.dim() == 1 else x_raw
x = torch.index_select(self.embeddings, 0, x_raw[:, -1].long())
pos = x_raw[:,:3].contiguous()
row, col = knn(pos, pos, 50, batch, batch)
edge_index_knn = torch.stack([row, col], dim=0)
edge_index_knn, dist_knn = self.get_edge_info(edge_index_knn, pos)
# Compute pairwise distances in global layer
tensor_g = torch.ones_like(dist_knn, device=dist_knn.device) * self.cutoff_g
mask_g = dist_knn <= tensor_g
edge_index_g = edge_index_knn[:, mask_g]
edge_index_g, dist_g = self.get_edge_info(edge_index_g, pos)
# Compute pairwise distances in local layer
tensor_l = torch.ones_like(dist_knn, device=dist_knn.device) * self.cutoff_l
mask_l = dist_knn <= tensor_l
edge_index_l = edge_index_knn[:, mask_l]
edge_index_l, dist_l = self.get_edge_info(edge_index_l, pos)
else:
raise ValueError("Invalid dataset. If you are using any dataset related to RNA 3D structure prediction, be sure to use 'rna' as the first 3 characters of the dataset name.")
idx_i, idx_j, idx_k, idx_kj, idx_ji, idx_i_pair, idx_j1_pair, idx_j2_pair, idx_jj_pair, idx_ji_pair = self.indices(edge_index_l, num_nodes=x.size(0))
# Compute two-hop angles in local layer
pos_ji, pos_kj = pos[idx_j] - pos[idx_i], pos[idx_k] - pos[idx_j]
a = (pos_ji * pos_kj).sum(dim=-1)
b = torch.cross(pos_ji, pos_kj).norm(dim=-1)
angle2 = torch.atan2(b, a)
# Compute one-hop angles in local layer
pos_i_pair = pos[idx_i_pair]
pos_j1_pair = pos[idx_j1_pair]
pos_j2_pair = pos[idx_j2_pair]
pos_ji_pair, pos_jj_pair = pos_j1_pair - pos_i_pair, pos_j2_pair - pos_j1_pair
a = (pos_ji_pair * pos_jj_pair).sum(dim=-1)
b = torch.cross(pos_ji_pair, pos_jj_pair).norm(dim=-1)
angle1 = torch.atan2(b, a)
# Get rbf and sbf embeddings
rbf_l = self.rbf_l(dist_l)
rbf_g = self.rbf_g(dist_g)
sbf1 = self.sbf(dist_l, angle1, idx_jj_pair)
sbf2 = self.sbf(dist_l, angle2, idx_kj)
edge_attr_rbf_l = self.mlp_rbf_l(rbf_l)
edge_attr_rbf_g = self.mlp_rbf_g(rbf_g)
edge_attr_sbf1 = self.mlp_sbf1(sbf1)
edge_attr_sbf2 = self.mlp_sbf2(sbf2)
# Message Passing Modules
out_global = []
out_local = []
att_score_global = []
att_score_local = []
for layer in range(self.n_layer):
x, out_g, att_score_g = self.global_layer[layer](x, edge_attr_rbf_g, edge_index_g)
out_global.append(out_g)
att_score_global.append(att_score_g)
x, out_l, att_score_l = self.local_layer[layer](x, edge_attr_rbf_l, edge_attr_sbf2, edge_attr_sbf1, \
idx_kj, idx_ji, idx_jj_pair, idx_ji_pair, edge_index_l)
out_local.append(out_l)
att_score_local.append(att_score_l)
# Fusion Module
att_score = torch.cat((torch.cat(att_score_global, 0), torch.cat(att_score_local, 0)), -1)
att_score = F.leaky_relu(att_score, 0.2)
att_weight = self.softmax(att_score)
out = torch.cat((torch.cat(out_global, 0), torch.cat(out_local, 0)), -1)
out = (out * att_weight).sum(dim=-1)
out = out.sum(dim=0).unsqueeze(-1)
if self.dataset == "QM9":
out = global_add_pool(out, batch)
elif self.dataset == "PDBbind":
out = out * all_index.unsqueeze(-1)
out = global_add_pool(out, batch)
elif self.dataset[:3].lower() == "rna":
out = global_mean_pool(out, batch)
else:
raise ValueError("Invalid dataset.")
return out.view(-1)
class PAMNet_s(nn.Module):
def __init__(self, config: Config, num_spherical=7, num_radial=6, envelope_exponent=5):
super(PAMNet_s, self).__init__()
self.dataset = config.dataset
self.dim = config.dim
self.n_layer = config.n_layer
self.cutoff_l = config.cutoff_l
self.cutoff_g = config.cutoff_g
self.embeddings = nn.Parameter(torch.ones((5, self.dim)))
self.rbf_g = BesselBasisLayer(16, self.cutoff_g, envelope_exponent)
self.rbf_l = BesselBasisLayer(16, self.cutoff_l, envelope_exponent)
self.sbf = SphericalBasisLayer(num_spherical, num_radial, self.cutoff_l, envelope_exponent)
self.mlp_rbf_g = MLP([16, self.dim])
self.mlp_rbf_l = MLP([16, self.dim])
self.mlp_sbf = MLP([num_spherical * num_radial, self.dim])
self.global_layer = torch.nn.ModuleList()
for _ in range(config.n_layer):
self.global_layer.append(Global_MessagePassing(config))
self.local_layer = torch.nn.ModuleList()
for _ in range(config.n_layer):
self.local_layer.append(Local_MessagePassing_s(config))
self.softmax = nn.Softmax(dim=-1)
self.init()
def init(self):
stdv = math.sqrt(3)
self.embeddings.data.uniform_(-stdv, stdv)
def indices(self, edge_index, num_nodes):
row, col = edge_index
value = torch.arange(row.size(0), device=row.device)
adj_t = SparseTensor(row=col, col=row, value=value,
sparse_sizes=(num_nodes, num_nodes))
adj_t_col = adj_t[col]
num_pairs = adj_t_col.set_value(None).sum(dim=1).to(torch.long)
idx_i_pair = row.repeat_interleave(num_pairs)
idx_j1_pair = col.repeat_interleave(num_pairs)
idx_j2_pair = adj_t_col.storage.col()
mask_j = idx_j1_pair != idx_j2_pair # Remove j == j' triplets.
idx_i_pair, idx_j1_pair, idx_j2_pair = idx_i_pair[mask_j], idx_j1_pair[mask_j], idx_j2_pair[mask_j]
idx_ji_pair = adj_t_col.storage.row()[mask_j]
idx_jj_pair = adj_t_col.storage.value()[mask_j]
return idx_i_pair, idx_j1_pair, idx_j2_pair, idx_jj_pair, idx_ji_pair
def forward(self, data):
if self.dataset != "QM9":
raise ValueError("Invalid dataset. The current PAMNet_s is only for QM9 experiments.")
x_raw = data.x
edge_index_l = data.edge_index
pos = data.pos
batch = data.batch
x = torch.index_select(self.embeddings, 0, x_raw.long())
# Compute pairwise distances in local layer
edge_index_l, _ = remove_self_loops(edge_index_l)
j_l, i_l = edge_index_l
dist_l = (pos[i_l] - pos[j_l]).pow(2).sum(dim=-1).sqrt()
# Compute pairwise distances in global layer
row, col = radius(pos, pos, self.cutoff_g, batch, batch, max_num_neighbors=500)
edge_index_g = torch.stack([row, col], dim=0)
edge_index_g, _ = remove_self_loops(edge_index_g)
j_g, i_g = edge_index_g
dist_g = (pos[i_g] - pos[j_g]).pow(2).sum(dim=-1).sqrt()
idx_i_pair, idx_j1_pair, idx_j2_pair, idx_jj_pair, idx_ji_pair = self.indices(edge_index_l, num_nodes=x.size(0))
# Compute one-hop angles in local layer
pos_i_pair = pos[idx_i_pair]
pos_j1_pair = pos[idx_j1_pair]
pos_j2_pair = pos[idx_j2_pair]
pos_ji_pair, pos_jj_pair = pos_j1_pair - pos_i_pair, pos_j2_pair - pos_j1_pair
a = (pos_ji_pair * pos_jj_pair).sum(dim=-1)
b = torch.cross(pos_ji_pair, pos_jj_pair).norm(dim=-1)
angle = torch.atan2(b, a)
# Get rbf and sbf embeddings
rbf_l = self.rbf_l(dist_l)
rbf_g = self.rbf_g(dist_g)
sbf = self.sbf(dist_l, angle, idx_jj_pair)
edge_attr_rbf_l = self.mlp_rbf_l(rbf_l)
edge_attr_rbf_g = self.mlp_rbf_g(rbf_g)
edge_attr_sbf = self.mlp_sbf(sbf)
# Message Passing Modules
out_global = []
out_local = []
att_score_global = []
att_score_local = []
for layer in range(self.n_layer):
x, out_g, att_score_g = self.global_layer[layer](x, edge_attr_rbf_g, edge_index_g)
out_global.append(out_g)
att_score_global.append(att_score_g)
x, out_l, att_score_l = self.local_layer[layer](x, edge_attr_rbf_l, edge_attr_sbf, \
idx_jj_pair, idx_ji_pair, edge_index_l)
out_local.append(out_l)
att_score_local.append(att_score_l)
# Fusion Module
att_score = torch.cat((torch.cat(att_score_global, 0), torch.cat(att_score_local, 0)), -1)
att_score = F.leaky_relu(att_score, 0.2)
att_weight = self.softmax(att_score)
out = torch.cat((torch.cat(out_global, 0), torch.cat(out_local, 0)), -1)
out = (out * att_weight).sum(dim=-1)
out = out.sum(dim=0).unsqueeze(-1)
out = global_add_pool(out, batch)
return out.view(-1)