experiments.models.gnn

experiments/models/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
import torch
import torch.nn.functional as F
from torch_geometric.nn import global_mean_pool, global_add_pool, GCNConv, GINConv, ChebConv
from ogb.graphproppred.mol_encoder import AtomEncoder

from .conv import GCNConvNew, GINConvNew, ChebConvNew

Cheb_K = 3


# mol
class GNN(torch.nn.Module):
    """
    Graph Isomorphism Network augmented with virtual node for multi-task binary graph classification
    Input:
        - batched Pytorch Geometric graph object
    Output:
        - prediction (Tensor): float torch tensor of shape (num_graphs, num_tasks)
    """

    def __init__(self, gnn_type, dataset_group, num_tasks=128, num_layers=5, emb_dim=300, dropout=0.5, is_pooled=True,
                 **model_kwargs):
        """
        Args:
            - num_tasks (int): number of binary label tasks. default to 128 (number of tasks of ogbg-molpcba)
            - num_layers (int): number of message passing layers of GNN
            - emb_dim (int): dimensionality of hidden channels
            - dropout (float): dropout ratio applied to hidden channels
        """
        self.gnn_type = gnn_type
        self.dataset_group = dataset_group

        super(GNN, self).__init__()

        if self.gnn_type.endswith('layers') :
            num_layers = int(self.gnn_type.split('_')[1])
            residual = True
        else :
            residual = False

        self.num_layers = num_layers
        self.dropout = dropout
        self.emb_dim = emb_dim
        self.num_tasks = num_tasks
        self.is_pooled = is_pooled

        if num_tasks is None:
            self.d_out = self.emb_dim
        else:
            self.d_out = self.num_tasks

        if self.num_layers < 2:
            raise ValueError("Number of GNN layers must be greater than 1.")

        if self.gnn_type.endswith('virtual'):
            self.gnn_node = GNN_node_Virtualnode(num_layers, emb_dim, dataset_group=self.dataset_group,
                                                 gnn_type=self.gnn_type.split('_')[0], dropout=dropout,
                                                 residual=residual)
        else:
            self.gnn_node = GNN_node(num_layers, emb_dim, dataset_group=self.dataset_group,
                                     gnn_type=self.gnn_type.split('_')[0], dropout=dropout,
                                     residual=residual)

        # Pooling function to generate whole-graph embeddings
        if self.is_pooled:
            self.pool = global_mean_pool
        else:
            self.pool = None
        if num_tasks is None:
            self.graph_pred_linear = None
        else:
            assert self.pool is not None
            self.graph_pred_linear = torch.nn.Linear(self.emb_dim, self.num_tasks)

    def forward(self, batched_data, perturb=None):
        h_node = self.gnn_node(batched_data, perturb)

        if self.graph_pred_linear is None:
            if self.pool is None:
                return h_node, batched_data.batch
            else:
                return self.pool(h_node, batched_data.batch)
        else:
            return self.graph_pred_linear(self.pool(h_node, batched_data.batch))


### GNN to generate node embedding
class GNN_node(torch.nn.Module):
    """
    Output:
        node representations
    """

    def __init__(self, num_layer, emb_dim, dataset_group='mol', gnn_type='gin', dropout=0.5, JK="last",
                 residual=False, batchnorm=True):
        '''
            emb_dim (int): node embedding dimensionality
            num_layer (int): number of GNN message passing layers
        '''

        super(GNN_node, self).__init__()

        self.dataset_group = dataset_group
        self.gnn_type = gnn_type

        self.num_layer = num_layer
        self.drop_ratio = dropout
        self.JK = JK
        ### add residual connection or not
        self.residual = residual
        self.batchnorm = batchnorm

        if self.num_layer < 2:
            raise ValueError("Number of GNN layers must be greater than 1.")

        if self.dataset_group == 'mol':
            self.node_encoder = AtomEncoder(emb_dim)
        elif self.dataset_group == 'ppa':
            self.node_encoder = torch.nn.Embedding(1, emb_dim) # uniform input node embedding
        elif self.dataset_group == 'RotatedMNIST':
            self.node_encoder = torch.nn.Linear(1, emb_dim)
        elif self.dataset_group == 'ColoredMNIST' :
            self.node_encoder = torch.nn.Linear(2, emb_dim)
            # self.node_encoder_cate = torch.nn.Embedding(8, emb_dim)
        elif self.dataset_group == 'SBM' :
            self.node_encoder = torch.nn.Embedding(8, emb_dim)
        elif self.dataset_group == 'UPFD':
            self.node_encoder = torch.nn.Embedding(8, emb_dim)
        else:
            raise NotImplementedError

        ###List of GNNs
        self.convs = torch.nn.ModuleList()
        self.batch_norms = torch.nn.ModuleList()

        for layer in range(num_layer):
            if gnn_type == 'gin':
                if self.dataset_group in  ['RotatedMNIST', 'ColoredMNIST', 'SBM', 'UPFD']:
                    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.convs.append(GINConv(mlp, train_eps=True))
                else:
                    self.convs.append(GINConvNew(emb_dim, self.dataset_group))
            elif gnn_type == 'gcn':
                if self.dataset_group in  ['RotatedMNIST', 'ColoredMNIST', 'SBM', 'UPFD']:
                    self.convs.append(GCNConv(emb_dim, emb_dim))
                else:
                    self.convs.append(GCNConvNew(emb_dim, self.dataset_group))
            elif gnn_type == 'cheb' :
                if self.dataset_group in  ['RotatedMNIST', 'ColoredMNIST', 'SBM', 'UPFD']:
                    self.convs.append(ChebConv(emb_dim, emb_dim, Cheb_K))
                else:
                    self.convs.append(ChebConvNew(emb_dim, Cheb_K, self.dataset_group))
            else:
                raise ValueError('Undefined GNN type called {}'.format(gnn_type))

            self.batch_norms.append(torch.nn.BatchNorm1d(emb_dim))

    def destroy_node_encoder(self):
        self.node_encoder = None

    def forward(self, batched_data, perturb=None):
        x, edge_index, edge_attr, batch = batched_data.x, batched_data.edge_index, batched_data.edge_attr, batched_data.batch

        # FLAG injects perturbation
        if self.node_encoder is None:
            h_list = [x]
        else:
            # if self.dataset_group == 'ColoredMNIST' :
            #     h_list = [self.node_encoder(x[:,:2]) + self.node_encoder_cate(x[:,2:].to(torch.int).squeeze()) + perturb if perturb is not None else self.node_encoder(x[:,:2]) + self.node_encoder_cate(x[:,2:].to(torch.int).squeeze())]
            # else :
            h_list = [self.node_encoder(x) + perturb if perturb is not None else self.node_encoder(x)]

        for layer in range(self.num_layer):

            h = self.convs[layer](h_list[layer], edge_index, edge_attr)
            if self.batchnorm :
                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_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]

        return node_representation


### Virtual GNN to generate node embedding
class GNN_node_Virtualnode(torch.nn.Module):
    """
    Output:
        node representations
    """

    def __init__(self, num_layer, emb_dim, dataset_group='mol', gnn_type='gin', dropout=0.5, JK="last",
                 residual=False, batchnorm=True):
        '''
            emb_dim (int): node embedding dimensionality
        '''

        super(GNN_node_Virtualnode, self).__init__()

        self.dataset_group = dataset_group
        self.gnn_type = gnn_type

        self.num_layer = num_layer
        self.drop_ratio = dropout
        self.JK = JK
        ### add residual connection or not
        self.residual = residual
        self.batchnorm = batchnorm

        if self.num_layer < 2:
            raise ValueError("Number of GNN layers must be greater than 1.")

        if self.dataset_group == 'mol':
            self.node_encoder = AtomEncoder(emb_dim)
        elif self.dataset_group == 'ppa':
            self.node_encoder = torch.nn.Embedding(1, emb_dim) # uniform input node embedding
        elif self.dataset_group == 'RotatedMNIST':
            self.node_encoder = torch.nn.Linear(1, emb_dim)
        elif self.dataset_group == 'ColoredMNIST' :
            self.node_encoder = torch.nn.Linear(2, emb_dim)
            # self.node_encoder_cate = torch.nn.Embedding(8, emb_dim)
        elif self.dataset_group == 'SBM' :
            self.node_encoder = torch.nn.Embedding(8, emb_dim)
        elif self.dataset_group == 'UPFD':
            self.node_encoder = torch.nn.Embedding(8, emb_dim)
        else:
            raise NotImplementedError

        ### 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()

        ### List of MLPs to transform virtual node at every layer
        self.mlp_virtualnode_list = torch.nn.ModuleList()

        for layer in range(num_layer):
            if gnn_type == 'gin':
                if self.dataset_group in  ['RotatedMNIST', 'ColoredMNIST', 'SBM', 'UPFD']:
                    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.convs.append(GINConv(mlp, train_eps=True))
                else:
                    self.convs.append(GINConvNew(emb_dim, self.dataset_group))
            elif gnn_type == 'gcn':
                if self.dataset_group in  ['RotatedMNIST', 'ColoredMNIST', 'SBM', 'UPFD']:
                    self.convs.append(GCNConv(emb_dim, emb_dim))
                else:
                    self.convs.append(GCNConvNew(emb_dim, self.dataset_group))
            elif gnn_type == 'cheb' :
                if self.dataset_group in  ['RotatedMNIST', 'ColoredMNIST', 'SBM', 'UPFD']:
                    self.convs.append(ChebConv(emb_dim, emb_dim, Cheb_K))
                else:
                    self.convs.append(ChebConvNew(emb_dim, Cheb_K, self.dataset_group))
            else:
                raise ValueError('Undefined GNN type called {}'.format(gnn_type))

            self.batch_norms.append(torch.nn.BatchNorm1d(emb_dim))

        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 destroy_node_encoder(self):
        self.node_encoder = None

    def forward(self, batched_data, perturb=None):

        x, edge_index, edge_attr, batch = batched_data.x, batched_data.edge_index, batched_data.edge_attr, batched_data.batch

        ### virtual node embeddings for graphs
        virtualnode_embedding = self.virtualnode_embedding(
            torch.zeros(batch[-1].item() + 1).to(edge_index.dtype).to(edge_index.device))

        # FLAG injects perturbation
        if self.node_encoder is None:
            h_list = [x]
        else:
            # if self.dataset_group == 'ColoredMNIST' :
            #     h_list = [self.node_encoder(x[:,:2]) + self.node_encoder_cate(x[:,2:].to(torch.int).squeeze()) + perturb if perturb is not None else self.node_encoder(x[:,:2]) + self.node_encoder_cate(x[:,2:].to(torch.int).squeeze())]
            # else :
            h_list = [self.node_encoder(x) + perturb if perturb is not None else self.node_encoder(x)]

        for layer in range(self.num_layer):
            ### add message from virtual nodes to graph nodes
            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)
            if self.batchnorm:
                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)

            ### update the virtual nodes
            if layer < self.num_layer - 1:
                ### add message from graph nodes to virtual nodes
                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 + 1):
                node_representation += h_list[layer]

        return node_representation