-
Notifications
You must be signed in to change notification settings - Fork 11
/
ogb_mol_gnn.py
548 lines (446 loc) · 19.7 KB
/
ogb_mol_gnn.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import torch
from torch_geometric.nn import MessagePassing
from torch_geometric.nn import global_add_pool, global_mean_pool, global_max_pool
from torch_geometric.nn import GlobalAttention, Set2Set
import torch.nn.functional as F
from torch_geometric.nn.inits import uniform
from ogb.graphproppred.mol_encoder import BondEncoder
from torch_geometric.utils import degree, dropout_adj, to_dense_batch, to_dense_adj
from ogb.utils.features import get_atom_feature_dims
from torch_geometric.data import Data
import math
import numpy as np
from scipy.sparse.csgraph import shortest_path
from torch_scatter import scatter, scatter_mean
from modules.ppgn_modules import *
from modules.ppgn_layers import *
import pdb
def center_pool(x, node_to_subgraph):
node_to_subgraph = node_to_subgraph.cpu().numpy()
# the first node of each subgraph is its center
_, center_indices = np.unique(node_to_subgraph, return_index=True)
x = x[center_indices]
return x
def center_pool_virtual(x, node_to_subgraph, virtual_embedding):
node_to_subgraph = node_to_subgraph.cpu().numpy()
# the first node of each subgraph is its center
_, center_indices = np.unique(node_to_subgraph, return_index=True)
x[center_indices] = x[center_indices] + virtual_embedding
return x
class GNN(torch.nn.Module):
def __init__(self, dataset, num_tasks, num_layer=5, emb_dim=300, gnn_type='gin',
virtual_node=True, residual=False, drop_ratio=0.5, JK="last",
graph_pooling="mean", subgraph_pooling="mean",
use_rd=False, use_rp=None,
RNI=False, **kwargs):
super(GNN, self).__init__()
self.num_layer = num_layer
self.drop_ratio = drop_ratio
self.JK = JK
self.emb_dim = emb_dim
self.num_tasks = num_tasks
self.graph_pooling = graph_pooling
self.subgraph_pooling = subgraph_pooling
self.center_pool_virtual = subgraph_pooling=="center" and virtual_node
self.use_rd = use_rd
self.use_rp = use_rp
### GNN to generate node embeddings
self.gnn_node = GNN_node(
dataset,
num_layer,
emb_dim,
JK=JK,
drop_ratio=drop_ratio,
residual=residual,
gnn_type=gnn_type,
virtual_node=virtual_node,
center_pool_virtual=self.center_pool_virtual,
use_rd=use_rd,
use_rp=use_rp,
RNI=RNI,
)
### Pooling function to generate whole-graph embeddings
if self.graph_pooling == "sum":
self.pool = global_add_pool
elif self.graph_pooling == "mean":
self.pool = global_mean_pool
elif self.graph_pooling == "max":
self.pool = global_max_pool
elif self.graph_pooling == "attention":
self.pool = GlobalAttention(
gate_nn = torch.nn.Sequential(
torch.nn.Linear(emb_dim, 2*emb_dim),
torch.nn.BatchNorm1d(2*emb_dim),
torch.nn.ReLU(),
torch.nn.Linear(2*emb_dim, 1)
)
)
elif self.graph_pooling == "set2set":
self.pool = Set2Set(emb_dim, processing_steps = 2)
elif self.graph_pooling == 'sort':
self.k = 20
conv1d_channels = [16, 32]
conv1d_activation = torch.nn.ReLU()
conv1d_kws = [self.emb_dim, 5]
self.conv1d_params1 = torch.nn.Conv1d(
1, conv1d_channels[0], conv1d_kws[0], conv1d_kws[0]
)
self.maxpool1d = torch.nn.MaxPool1d(2, 2)
self.conv1d_params2 = torch.nn.Conv1d(
conv1d_channels[0], conv1d_channels[1], conv1d_kws[1], 1
)
dense_dim = int((self.k - 2) / 2 + 1)
self.dense_dim = (dense_dim - conv1d_kws[1] + 1) * conv1d_channels[1]
else:
raise ValueError("Invalid graph pooling type.")
if graph_pooling == "set2set":
self.graph_pred_linear = torch.nn.Linear(2*self.emb_dim, self.num_tasks)
elif graph_pooling == 'sort':
self.graph_pred_linear = torch.nn.Linear(self.dense_dim, self.num_tasks)
else:
self.graph_pred_linear = torch.nn.Linear(self.emb_dim, self.num_tasks)
### Pooling function to generate sub-graph embeddings
if self.subgraph_pooling == "sum":
self.subpool = global_add_pool
elif self.subgraph_pooling == "mean":
self.subpool = global_mean_pool
elif self.subgraph_pooling == "max":
self.subpool = global_max_pool
elif self.subgraph_pooling == "attention":
self.subpool = GlobalAttention(
gate_nn = torch.nn.Sequential(
torch.nn.Linear(emb_dim, 2*emb_dim),
torch.nn.BatchNorm1d(2*emb_dim),
torch.nn.ReLU(),
torch.nn.Linear(2*emb_dim, 1)
)
)
elif self.subgraph_pooling == "center":
self.subpool = center_pool
else:
self.subpool = None
def forward(self, data, perturb=None):
x = self.gnn_node(data, perturb=perturb)
if 'node_to_subgraph' in data and 'subgraph_to_graph' in data:
x = self.subpool(x, data.node_to_subgraph)
x = self.pool(x, data.subgraph_to_graph)
else:
x = self.pool(x, data.batch)
return self.graph_pred_linear(x)
class AtomEncoder(torch.nn.Module):
def __init__(self, emb_dim):
super(AtomEncoder, self).__init__()
self.atom_embedding_list = torch.nn.ModuleList()
full_atom_feature_dims = get_atom_feature_dims()
for i, dim in enumerate(full_atom_feature_dims):
emb = torch.nn.Embedding(dim, emb_dim)
torch.nn.init.xavier_uniform_(emb.weight.data)
self.atom_embedding_list.append(emb)
def forward(self, x):
x_embedding = 0
for i in range(x.shape[1]):
x_embedding += self.atom_embedding_list[i](x[:,i])
return x_embedding
### GIN convolution along the graph structure
class GINConv(MessagePassing):
def __init__(self, dataset, emb_dim):
'''
emb_dim (int): node embedding dimensionality
'''
super(GINConv, self).__init__(aggr = "add")
self.mlp = torch.nn.Sequential(
torch.nn.Linear(emb_dim, 2*emb_dim),
torch.nn.BatchNorm1d(2*emb_dim),
torch.nn.ReLU(),
torch.nn.Linear(2*emb_dim, emb_dim)
)
self.eps = torch.nn.Parameter(torch.Tensor([0]))
if dataset.startswith('ogbg-mol'):
self.edge_encoder = BondEncoder(emb_dim = emb_dim)
elif dataset.startswith('ogbg-ppa'):
self.edge_encoder = torch.nn.Linear(7, emb_dim)
def forward(self, x, edge_index, edge_attr):
edge_embedding = self.edge_encoder(edge_attr)
out = self.mlp(
(1 + self.eps) * x + self.propagate(edge_index, x=x, edge_attr=edge_embedding)
)
return out
def message(self, x_j, edge_attr):
return F.relu(x_j + edge_attr)
def update(self, aggr_out):
return aggr_out
class GINConvNoEdge(MessagePassing):
def __init__(self, emb_dim):
'''
emb_dim (int): node embedding dimensionality
'''
super(GINConvNoEdge, self).__init__(aggr = "add")
self.mlp = torch.nn.Sequential(
torch.nn.Linear(emb_dim, 2*emb_dim),
torch.nn.BatchNorm1d(2*emb_dim),
torch.nn.ReLU(),
torch.nn.Linear(2*emb_dim, emb_dim)
)
self.eps = torch.nn.Parameter(torch.Tensor([0]))
def forward(self, x, edge_index):
out = self.mlp((1 + self.eps) *x + self.propagate(edge_index, x=x))
return out
def message(self, x_j):
return F.relu(x_j)
def update(self, aggr_out):
return aggr_out
### GCN convolution along the graph structure
class GCNConv(MessagePassing):
def __init__(self, emb_dim):
super(GCNConv, self).__init__(aggr='add')
self.linear = torch.nn.Linear(emb_dim, emb_dim)
self.root_emb = torch.nn.Embedding(1, emb_dim)
self.bond_encoder = BondEncoder(emb_dim = emb_dim)
def forward(self, x, edge_index, edge_attr):
x = self.linear(x)
edge_embedding = self.bond_encoder(edge_attr)
row, col = edge_index
#edge_weight = torch.ones((edge_index.size(1), ), device=edge_index.device)
deg = degree(row, x.size(0), dtype = x.dtype) + 1
deg_inv_sqrt = deg.pow(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
norm = deg_inv_sqrt[row] * deg_inv_sqrt[col]
return self.propagate(
edge_index, x=x, edge_attr = edge_embedding, norm=norm
) + F.relu(x + self.root_emb.weight) * 1./deg.view(-1,1)
def message(self, x_j, edge_attr, norm):
return norm.view(-1, 1) * F.relu(x_j + edge_attr)
def update(self, aggr_out):
return aggr_out
### Virtual GNN to generate node embedding
class GNN_node(torch.nn.Module):
"""
Output:
node representations
"""
def __init__(self, dataset, num_layer, emb_dim, drop_ratio=0.5, JK="last",
residual=False, gnn_type='gin', virtual_node=True, use_rd=False,
adj_dropout=0, skip_node_encoder=False, use_rp=None,
center_pool_virtual=False, RNI=False):
super(GNN_node, self).__init__()
self.num_layer = num_layer
self.drop_ratio = drop_ratio
self.JK = JK
self.residual = residual
self.virtual_node = virtual_node
self.use_rd = use_rd
self.use_rp = use_rp
self.adj_dropout = adj_dropout
self.center_pool_virtual = center_pool_virtual
self.RNI = RNI
z_emb_dim, x_emb_dim = emb_dim, emb_dim
if use_rd:
self.rd_projection = torch.nn.Linear(1, z_emb_dim)
if use_rp is not None:
self.rp_projection = torch.nn.Linear(use_rp, z_emb_dim)
self.z_embedding = torch.nn.Embedding(1000, z_emb_dim)
self.skip_node_encoder = skip_node_encoder
if not self.skip_node_encoder:
if dataset.startswith('ogbg-mol'):
self.node_encoder = AtomEncoder(x_emb_dim)
elif dataset.startswith('ogbg-ppa'):
self.node_encoder = torch.nn.Embedding(1, x_emb_dim)
if self.virtual_node:
# set the initial virtual node embedding to 0.
self.virtualnode_embedding = torch.nn.Embedding(1, emb_dim)
torch.nn.init.constant_(self.virtualnode_embedding.weight.data, 0)
### List of GNNs
self.convs = torch.nn.ModuleList()
### batch norms applied to node embeddings
self.batch_norms = torch.nn.ModuleList()
for layer in range(num_layer):
if gnn_type == 'gin':
self.convs.append(GINConv(dataset, emb_dim))
elif gnn_type == 'gcn':
self.convs.append(GCNConv(emb_dim))
else:
ValueError('Undefined GNN type called {}'.format(gnn_type))
self.batch_norms.append(torch.nn.BatchNorm1d(emb_dim))
if self.virtual_node:
# List of MLPs to transform virtual node at every layer
self.mlp_virtualnode_list = torch.nn.ModuleList()
for layer in range(num_layer - 1):
self.mlp_virtualnode_list.append(torch.nn.Sequential(
torch.nn.Linear(emb_dim, 2*emb_dim),
torch.nn.BatchNorm1d(2*emb_dim),
torch.nn.ReLU(),
torch.nn.Linear(2*emb_dim, emb_dim),
torch.nn.BatchNorm1d(emb_dim),
torch.nn.ReLU()))
def forward(self, batched_data, x=None, edge_index=None, edge_attr=None,
batch=None, perturb=None):
if batched_data is not None:
x, edge_index, edge_attr, batch = (
batched_data.x, batched_data.edge_index,
batched_data.edge_attr, batched_data.batch
)
if self.adj_dropout > 0:
edge_index, edge_attr = dropout_adj(
edge_index, edge_attr, p=self.adj_dropout, num_nodes=len(x),
training=self.training
)
if self.virtual_node:
virtualnode_embedding = self.virtualnode_embedding(
torch.zeros(batch[-1].item() + 1).to(edge_index.dtype).to(edge_index.device)
)
if self.skip_node_encoder:
h0 = x
else:
h0 = self.node_encoder(x)
z_emb = 0
if 'z' in batched_data:
### computing input node embedding
z_emb = self.z_embedding(batched_data.z)
if z_emb.ndim == 3:
z_emb = z_emb.sum(dim=1)
if self.use_rd and 'rd' in batched_data:
rd_proj = self.rd_projection(batched_data.rd)
z_emb = z_emb + rd_proj
if self.use_rp and 'rp' in batched_data:
rp_proj = self.rp_projection(batched_data.rp)
if rp_proj.shape[0] != batched_data.num_nodes:
rp_proj = rp_proj[batched_data.node_to_subgraph]
z_emb = z_emb + rp_proj
h0 += z_emb
if self.RNI:
rand_x = torch.rand(*h0.size()).to(h0.device) * 2 - 1
h0 += rand_x
h_list = [h0]
if perturb is not None:
h_list[0] = h_list[0] + perturb
for layer in range(self.num_layer):
if self.virtual_node:
# add message from virtual nodes to graph nodes
if self.center_pool_virtual:
# only add virtual node embedding to the center node within each subgraph
# suitable when using center subgraph-pooling
h_list[layer] = center_pool_virtual(
h_list[layer], batched_data.node_to_subgraph,
virtualnode_embedding[batched_data.subgraph_to_graph])
else:
h_list[layer] = h_list[layer] + virtualnode_embedding[batch]
### Message passing among graph nodes
h = self.convs[layer](h_list[layer], edge_index, edge_attr)
h = self.batch_norms[layer](h)
if layer == self.num_layer - 1:
#remove relu for the last layer
h = F.dropout(h, self.drop_ratio, training = self.training)
else:
h = F.dropout(F.relu(h), self.drop_ratio, training = self.training)
if self.residual:
h = h + h_list[layer]
h_list.append(h)
if self.virtual_node:
# update the virtual nodes
if layer < self.num_layer - 1:
# add message from graph nodes to virtual nodes
if self.center_pool_virtual:
center_embedding = center_pool(
h_list[layer], batched_data.node_to_subgraph
)
virtualnode_embedding_temp = global_add_pool(
center_embedding, batched_data.subgraph_to_graph
) + virtualnode_embedding
else:
virtualnode_embedding_temp = global_add_pool(
h_list[layer], batch
) + virtualnode_embedding
# transform virtual nodes using MLP
if self.residual:
virtualnode_embedding = virtualnode_embedding + F.dropout(
self.mlp_virtualnode_list[layer](virtualnode_embedding_temp),
self.drop_ratio, training = self.training
)
else:
virtualnode_embedding = F.dropout(
self.mlp_virtualnode_list[layer](virtualnode_embedding_temp),
self.drop_ratio, training = self.training
)
# Different implementations of Jk-concat
if self.JK == "last":
node_representation = h_list[-1]
elif self.JK == "sum":
node_representation = 0
for layer in range(self.num_layer):
node_representation += h_list[layer]
return node_representation
# Provably Powerful Graph Networks
class PPGN(torch.nn.Module):
# Provably powerful graph networks
def __init__(self, num_tasks, emb_dim=300, use_embedding=True, use_spd=False,
**kwargs):
super(PPGN, self).__init__()
self.use_embedding = use_embedding
self.use_spd = use_spd
if self.use_embedding:
self.bond_encoder = BondEncoder(emb_dim=emb_dim)
self.atom_encoder = AtomEncoder(emb_dim)
initial_dim = 1 + emb_dim * 2
else:
initial_dim = 1 + 3 + 9 # 9 atom features + 3 bond types + adj
if self.use_spd:
initial_dim += 1
# ppgn modules
num_blocks = 2
num_rb_layers = 4
num_fc_layers = 2
self.ppgn_rb = torch.nn.ModuleList()
self.ppgn_rb.append(RegularBlock(num_blocks, initial_dim, emb_dim))
for i in range(num_rb_layers - 1):
self.ppgn_rb.append(RegularBlock(num_blocks, emb_dim, emb_dim))
self.ppgn_fc = torch.nn.ModuleList()
self.ppgn_fc.append(FullyConnected(emb_dim * 2, emb_dim))
for i in range(num_fc_layers - 2):
self.ppgn_fc.append(FullyConnected(emb_dim, emb_dim))
self.ppgn_fc.append(FullyConnected(emb_dim, num_tasks, activation_fn=None))
def forward(self, data):
if self.use_embedding:
edge_embedding = self.bond_encoder(data.edge_attr)
node_embedding = self.atom_encoder(data.x)
else:
edge_embedding = data.edge_attr.to(torch.float)
node_embedding = data.x.to(torch.float)
# prepare dense data
device = data.edge_attr.device
edge_adj = torch.ones(data.edge_attr.shape[0], 1).to(device)
edge_data = torch.cat([edge_adj, edge_embedding], 1)
dense_edge_data = to_dense_adj(
data.edge_index, data.batch, edge_data
) # |graphs| * max_nodes * max_nodes * edge_data_dim
dense_node_data = to_dense_batch(node_embedding, data.batch)[0] # |graphs| * max_nodes * d
shape = dense_node_data.shape
shape = (shape[0], shape[1], shape[1], shape[2])
diag_node_data = torch.empty(*shape).to(data.edge_attr.device)
if self.use_spd:
dense_dist_mat = torch.zeros(shape[0], shape[1], shape[1], 1).to(device)
for g in range(shape[0]):
if self.use_spd:
g_adj = dense_edge_data[g, :, :, 0].cpu().detach().numpy()
g_dist_mat = torch.tensor(shortest_path(g_adj))
g_dist_mat[torch.isinf(g_dist_mat)] = 0
g_dist_mat /= g_dist_mat.max() + 1 # normalize
g_dist_mat = g_dist_mat.unsqueeze(0).to(device)
dense_dist_mat[g, :, :, 0] = g_dist_mat
for i in range(shape[-1]):
diag_node_data[g, :, :, i] = torch.diag(dense_node_data[g, :, i])
if self.use_spd:
z = torch.cat([dense_dist_mat, dense_edge_data, diag_node_data], -1)
else:
z = torch.cat([dense_edge_data, diag_node_data], -1)
z = torch.transpose(z, 1, 3)
# ppng
for rb in self.ppgn_rb:
z = rb(z)
#z = diag_offdiag_maxpool(z)
z = diag_offdiag_meanpool(z)
for fc in self.ppgn_fc:
z = fc(z)
torch.cuda.empty_cache()
return z
if __name__ == '__main__':
GNN(num_tasks = 10)