-
Notifications
You must be signed in to change notification settings - Fork 8
/
conv.py
211 lines (155 loc) · 7.21 KB
/
conv.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
"""
Code taken from ogb examples and adapted
"""
import torch
import torch.nn.functional as F
from ogb.graphproppred.mol_encoder import BondEncoder
from torch_geometric.nn import GINConv as PyGINConv
from torch_geometric.nn import GraphConv
from torch_geometric.nn import MessagePassing
from torch_geometric.utils import degree
### GIN convolution along the graph structure
class GINConv(MessagePassing):
def __init__(self, in_dim, emb_dim):
'''
emb_dim (int): node embedding dimensionality
'''
super(GINConv, self).__init__(aggr="add")
self.mlp = torch.nn.Sequential(torch.nn.Linear(in_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]))
self.bond_encoder = BondEncoder(emb_dim=in_dim)
def forward(self, x, edge_index, edge_attr):
edge_embedding = self.bond_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 ZINCGINConv(MessagePassing):
def __init__(self, in_dim, emb_dim):
super(ZINCGINConv, self).__init__(aggr="add")
self.mlp = torch.nn.Sequential(torch.nn.Linear(in_dim, emb_dim), torch.nn.BatchNorm1d(emb_dim), torch.nn.ReLU(),
torch.nn.Linear(emb_dim, emb_dim))
self.eps = torch.nn.Parameter(torch.Tensor([0]))
self.bond_encoder = torch.nn.Embedding(4, in_dim)
def forward(self, x, edge_index, edge_attr):
edge_embedding = self.bond_encoder(edge_attr.squeeze())
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 OriginalGINConv(torch.nn.Module):
def __init__(self, in_dim, emb_dim):
super(OriginalGINConv, self).__init__()
mlp = torch.nn.Sequential(
torch.nn.Linear(in_dim, emb_dim),
torch.nn.BatchNorm1d(emb_dim),
torch.nn.ReLU(),
torch.nn.Linear(emb_dim, emb_dim)
)
self.layer = PyGINConv(nn=mlp, train_eps=False)
def forward(self, x, edge_index, edge_attr):
return self.layer(x, edge_index)
### GCN convolution along the graph structure
class GCNConv(MessagePassing):
def __init__(self, in_dim, emb_dim):
super(GCNConv, self).__init__(aggr='add')
self.linear = torch.nn.Linear(in_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
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
### GNN to generate node embedding
class GNN_node(torch.nn.Module):
"""
Output:
node representations
"""
def __init__(self, num_layer, in_dim, emb_dim, drop_ratio=0.5, JK="last", residual=False, gnn_type='gin',
num_random_features=0, feature_encoder=lambda x: x):
'''
emb_dim (int): node embedding dimensionality
num_layer (int): number of GNN message passing layers
'''
super(GNN_node, self).__init__()
self.num_layer = num_layer
self.drop_ratio = drop_ratio
self.JK = JK
### add residual connection or not
self.residual = residual
self.gnn_type = gnn_type
if self.num_layer < 2:
raise ValueError("Number of GNN layers must be greater than 1.")
self.atom_encoder = feature_encoder
self.num_random_features = num_random_features
if num_random_features > 0:
assert gnn_type == 'graphconv'
self.initial_layers = torch.nn.ModuleList(
[GraphConv(in_dim, emb_dim // 2), GraphConv(emb_dim // 2, emb_dim - num_random_features)]
)
# now the next layers will have dimension emb_dim
in_dim = emb_dim
###List of GNNs
self.convs = torch.nn.ModuleList()
self.batch_norms = torch.nn.ModuleList()
for layer in range(num_layer):
if gnn_type == 'gin':
self.convs.append(GINConv(emb_dim if layer != 0 else in_dim, emb_dim))
elif gnn_type == 'gcn':
self.convs.append(GCNConv(emb_dim if layer != 0 else in_dim, emb_dim))
elif gnn_type == 'originalgin':
self.convs.append(OriginalGINConv(emb_dim if layer != 0 else in_dim, emb_dim))
elif gnn_type == 'zincgin':
self.convs.append(ZINCGINConv(emb_dim if layer != 0 else in_dim, emb_dim))
elif gnn_type == 'graphconv':
self.convs.append(GraphConv(emb_dim if layer != 0 else in_dim, emb_dim))
else:
raise ValueError('Undefined GNN type called {}'.format(gnn_type))
self.batch_norms.append(torch.nn.BatchNorm1d(emb_dim))
def forward(self, batched_data):
x, edge_index, edge_attr, batch = batched_data.x, batched_data.edge_index, batched_data.edge_attr, batched_data.batch
if self.num_random_features > 0:
for layer in self.initial_layers:
x = F.elu(layer(x, edge_index, edge_attr))
# Implementation of RNI
random_dims = torch.empty(x.shape[0], self.num_random_features).to(x.device)
torch.nn.init.normal_(random_dims)
x = torch.cat([x, random_dims], dim=1)
### computing input node embedding
h_list = [self.atom_encoder(x)]
for layer in range(self.num_layer):
h = self.convs[layer](h_list[layer], edge_index, edge_attr)
h = self.batch_norms[layer](h)
if self.gnn_type not in ['gin', 'gcn'] or layer != self.num_layer - 1:
h = F.relu(h) # remove last relu for ogb
if self.drop_ratio > 0.:
h = F.dropout(h, self.drop_ratio, training=self.training)
if self.residual:
h += h_list[layer]
h_list.append(h)
### 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 + 1):
node_representation += h_list[layer]
elif self.JK == "concat":
node_representation = torch.cat(h_list, dim=1)
return node_representation