-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
477 lines (376 loc) · 16.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
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
from typing import Callable, Union, Tuple
import torch
import torch.nn as nn
from torch import Tensor
import torch_geometric.nn as gnn
from torch_geometric.data import DataLoader
from torch_geometric.data.batch import Batch
from torch_geometric.typing import OptPairTensor, Adj, OptTensor
from torch_geometric.utils.loop import add_self_loops, remove_self_loops
from torch_sparse import SparseTensor
import torch.nn.functional as F
from torch.nn import Linear, Sequential, ReLU, BatchNorm1d as BN
from torch_geometric.nn import GINConv, global_mean_pool
from torch_geometric.nn import MessagePassing
from torch_geometric.nn.inits import reset
# class adopted from https://github.com/divelab/DIG/tree/main
class GNNBasic(torch.nn.Module):
def __init__(self):
super().__init__()
def arguments_read(self, *args, **kwargs):
data: Batch = kwargs.get('data') or None
if not data:
if not args:
assert 'x' in kwargs
assert 'edge_index' in kwargs
x, edge_index = kwargs['x'], kwargs['edge_index'],
batch = kwargs.get('batch')
if batch is None:
batch = torch.zeros(kwargs['x'].shape[0], dtype=torch.int64, device=x.device)
elif len(args) == 2:
x, edge_index, batch = args[0], args[1], \
torch.zeros(args[0].shape[0], dtype=torch.int64, device=args[0].device) # author-bot: change from x.device to arges[0].device
##
elif len(args) == 3:
x, edge_index, batch = args[0], args[1], args[2]
else:
raise ValueError(f"forward's args should take 2 or 3 arguments but got {len(args)}")
else:
x, edge_index, batch = data.x, data.edge_index, data.batch
return x, edge_index, batch
# author-bot: a general GCN with different hidden dimensions
class GCN(GNNBasic):
def __init__(self, model_level, dim_node, dim_hidden, ffn_dim, num_classes):
super().__init__()
self.conv1 = GCNConv(dim_node, dim_hidden[0])
self.convs = nn.ModuleList(
[
GCNConv(dim_hidden[i], dim_hidden[i+1])
for i in range(len(dim_hidden)-1)
]
)
self.relu1 = nn.ReLU()
self.relus = nn.ModuleList(
[
nn.ReLU()
for _ in range(len(dim_hidden) - 1)
]
)
if model_level == 'node':
self.readout = IdenticalPool()
else:
self.readout = GlobalMaxPool()
# self.readout = GlobalMeanPool()
self.ffn = nn.Sequential(*(
[nn.Linear(dim_hidden[-1], dim_hidden[-1])] +
[nn.ReLU(), nn.Dropout(), nn.Linear(dim_hidden[-1], num_classes)]
))
def forward(self, *args, **kwargs) -> torch.Tensor:
x, edge_index, batch = self.arguments_read(*args, **kwargs)
post_conv = self.relu1(self.conv1(x, edge_index))
for conv, relu in zip(self.convs, self.relus):
post_conv = relu(conv(post_conv, edge_index))
out_readout = self.readout(post_conv, batch)
out = self.ffn(out_readout)
return out
def get_emb(self, *args, **kwargs) -> torch.Tensor:
x, edge_index, batch = self.arguments_read(*args, **kwargs)
post_conv = self.conv1(x, edge_index)
for conv in self.convs:
post_conv = conv(post_conv, edge_index)
return post_conv
# author-bot: a GIN model with the same dimension for each hidden layer
class GIN(GNNBasic):
def __init__(self, model_level, dim_node, dim_hidden, num_classes, num_layer):
super().__init__()
self.conv1 = GINConv(nn.Sequential(nn.Linear(dim_node, dim_hidden), nn.ReLU(),
nn.Linear(dim_hidden, dim_hidden), nn.ReLU()))
self.convs = nn.ModuleList(
[
GINConv(nn.Sequential(nn.Linear(dim_hidden, dim_hidden), nn.ReLU(),
nn.Linear(dim_hidden, dim_hidden), nn.ReLU()))#,
# nn.BatchNorm1d(dim_hidden)))
for _ in range(num_layer - 1)
]
)
self.relu1 = nn.ReLU()
self.relus = nn.ModuleList(
[
nn.ReLU()
for _ in range(num_layer - 1)
]
)
if model_level == 'node':
self.readout = IdenticalPool()
else:
self.readout = GlobalMeanPool()
self.ffn = nn.Sequential(*(
[nn.Linear(dim_hidden, dim_hidden)] +
[nn.ReLU(), nn.Dropout(), nn.Linear(dim_hidden, num_classes)]
))
self.dropout = nn.Dropout()
def forward(self, *args, **kwargs) -> torch.Tensor:
x, edge_index, batch = self.arguments_read(*args, **kwargs)
post_conv = self.conv1(x, edge_index)
for conv in self.convs:
post_conv = conv(post_conv, edge_index)
out_readout = self.readout(post_conv, batch)
out = self.ffn(out_readout)
return out
def get_emb(self, *args, **kwargs) -> torch.Tensor:
x, edge_index, batch = self.arguments_read(*args, **kwargs)
post_conv = self.conv1(x, edge_index)
for conv in self.convs:
post_conv = conv(post_conv, edge_index)
return post_conv
class GIN_3l(GNNBasic):
def __init__(self, model_level, dim_node, dim_hidden, num_classes):
super().__init__()
num_layer = 3
self.conv1 = GINConv(nn.Sequential(nn.Linear(dim_node, dim_hidden), nn.ReLU(),
nn.Linear(dim_hidden, dim_hidden), nn.ReLU()))
self.convs = nn.ModuleList(
[
GINConv(nn.Sequential(nn.Linear(dim_hidden, dim_hidden), nn.ReLU(),
nn.Linear(dim_hidden, dim_hidden), nn.ReLU()))#,
# nn.BatchNorm1d(dim_hidden)))
for _ in range(num_layer - 1)
]
)
self.relu1 = nn.ReLU()
self.relus = nn.ModuleList(
[
nn.ReLU()
for _ in range(num_layer - 1)
]
)
if model_level == 'node':
self.readout = IdenticalPool()
else:
self.readout = GlobalMeanPool()
self.ffn = nn.Sequential(*(
[nn.Linear(dim_hidden, dim_hidden)] +
[nn.ReLU(), nn.Dropout(), nn.Linear(dim_hidden, num_classes)]
))
self.dropout = nn.Dropout()
def forward(self, *args, **kwargs) -> torch.Tensor:
x, edge_index, batch = self.arguments_read(*args, **kwargs)
post_conv = self.conv1(x, edge_index)
for conv in self.convs:
post_conv = conv(post_conv, edge_index)
out_readout = self.readout(post_conv, batch)
out = self.ffn(out_readout)
return out
def get_emb(self, *args, **kwargs) -> torch.Tensor:
x, edge_index, batch = self.arguments_read(*args, **kwargs)
post_conv = self.conv1(x, edge_index)
for conv in self.convs:
post_conv = conv(post_conv, edge_index)
return post_conv
# adopted from https://github.com/divelab/DIG/tree/main
class GCNConv(gnn.GCNConv):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.edge_weight = None
def forward(self, x: Tensor, edge_index: Adj,
edge_weight: OptTensor = None) -> Tensor:
""""""
if self.normalize and edge_weight is None:
if isinstance(edge_index, Tensor):
cache = self._cached_edge_index
if cache is None:
edge_index, edge_weight = gnn.conv.gcn_conv.gcn_norm( # yapf: disable
edge_index, edge_weight, x.size(self.node_dim),
self.improved, self.add_self_loops, dtype=x.dtype)
if self.cached:
self._cached_edge_index = (edge_index, edge_weight)
else:
edge_index, edge_weight = cache[0], cache[1]
elif isinstance(edge_index, SparseTensor):
cache = self._cached_adj_t
if cache is None:
edge_index = gnn.conv.gcn_conv.gcn_norm( # yapf: disable
edge_index, edge_weight, x.size(self.node_dim),
self.improved, self.add_self_loops, dtype=x.dtype)
if self.cached:
self._cached_adj_t = edge_index
else:
edge_index = cache
# --- add require_grad ---
edge_weight.requires_grad_(True)
# x = torch.matmul(x, self.weight)
x = torch.matmul(x, self.lin.weight.T) # fix inconsistence in torch vertion
# propagate_type: (x: Tensor, edge_weight: OptTensor)
out = self.propagate(edge_index, x=x, edge_weight=edge_weight,
size=None)
if self.bias is not None:
out += self.bias
# --- My: record edge_weight ---
self.edge_weight = edge_weight
return out
class GINConv(gnn.GINConv):
def __init__(self, nn: Callable, eps: float = 0., train_eps: bool = False,
**kwargs):
super().__init__(nn, eps, train_eps, **kwargs)
self.edge_weight = None
self.fc_steps = None
self.reweight = None
def forward(self, x: Union[Tensor, OptPairTensor], edge_index: Adj,
edge_weight: OptTensor = None, task='explain', **kwargs) -> Tensor:
""""""
self.num_nodes = x.shape[0]
if isinstance(x, Tensor):
x: OptPairTensor = (x, x)
# propagate_type: (x: OptPairTensor)
if edge_weight is not None:
self.edge_weight = edge_weight
assert edge_weight.shape[0] == edge_index.shape[1]
self.reweight = False
else:
edge_index, _ = remove_self_loops(edge_index)
self_loop_edge_index, _ = add_self_loops(edge_index, num_nodes=self.num_nodes)
if self_loop_edge_index.shape[1] != edge_index.shape[1]:
edge_index = self_loop_edge_index
self.reweight = True
out = self.propagate(edge_index, x=x[0], size=None)
if task == 'explain':
layer_extractor = []
hooks = []
def register_hook(module: nn.Module):
if not list(module.children()):
hooks.append(module.register_forward_hook(forward_hook))
def forward_hook(module: nn.Module, input: Tuple[Tensor], output: Tensor):
# input contains x and edge_index
layer_extractor.append((module, input[0], output))
# --- register hooks ---
self.nn.apply(register_hook)
nn_out = self.nn(out)
for hook in hooks:
hook.remove()
fc_steps = []
step = {'input': None, 'module': [], 'output': None}
for layer in layer_extractor:
if isinstance(layer[0], nn.Linear):
if step['module']:
fc_steps.append(step)
# step = {'input': layer[1], 'module': [], 'output': None}
step = {'input': None, 'module': [], 'output': None}
step['module'].append(layer[0])
if kwargs.get('probe'):
step['output'] = layer[2]
else:
step['output'] = None
if step['module']:
fc_steps.append(step)
self.fc_steps = fc_steps
else:
nn_out = self.nn(out)
return nn_out
def message(self, x_j: Tensor) -> Tensor:
if self.reweight:
edge_weight = torch.ones(x_j.shape[0], device=x_j.device)
edge_weight.data[-self.num_nodes:] += self.eps
edge_weight = edge_weight.detach().clone()
edge_weight.requires_grad_(True)
self.edge_weight = edge_weight
return x_j * self.edge_weight.view(-1, 1)
class GNNPool(nn.Module):
def __init__(self):
super().__init__()
class GlobalMeanPool(GNNPool):
def __init__(self):
super().__init__()
def forward(self, x, batch):
return gnn.global_mean_pool(x, batch)
class GlobalMaxPool(GNNPool):
def __init__(self):
super().__init__()
def forward(self, x, batch):
return gnn.global_max_pool(x, batch)
class IdenticalPool(GNNPool):
def __init__(self):
super().__init__()
def forward(self, x, batch):
return x
# adopted and modified from Tudataset: A collection of benchmark datasets for learning with graphs,” CoRR, vol. abs/2007.08663, 2020.
class GINE0Conv(MessagePassing):
def __init__(self, edge_dim, dim_init, dim):
super(GINE0Conv, self).__init__(aggr="add")
self.edge_encoder = Sequential(Linear(edge_dim, dim_init), ReLU(), Linear(dim_init, dim_init), ReLU(),
BN(dim_init))
self.mlp = Sequential(Linear(dim_init, dim), ReLU(), Linear(dim, dim), ReLU(), BN(dim))
def forward(self, x, edge_index, edge_attr):
edge_embedding = self.edge_encoder(edge_attr)
out = self.mlp(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
def reset_parameters(self):
reset(self.edge_encoder)
reset(self.mlp)
class GINE0(torch.nn.Module):
def __init__(self, num_edge_features, num_node_features, num_classes, num_layers, hidden):
super(GINE0, self).__init__()
self.conv1 = GINE0Conv(num_edge_features, num_node_features, hidden)
self.convs = torch.nn.ModuleList()
for i in range(num_layers - 1):
self.convs.append(GINE0Conv(num_edge_features, hidden, hidden))
self.lin1 = Linear(hidden, hidden)
self.lin2 = Linear(hidden, num_classes)
def reset_parameters(self):
self.conv1.reset_parameters()
for conv in self.convs:
conv.reset_parameters()
self.lin1.reset_parameters()
self.lin2.reset_parameters()
def forward(self, data):
x, edge_index, batch, edge_attr = data.x, data.edge_index, data.batch, data.edge_attr
x = self.conv1(x, edge_index, edge_attr)
for conv in self.convs:
x = conv(x, edge_index, edge_attr)
x = global_mean_pool(x, batch)
x = F.relu(self.lin1(x))
x = F.dropout(x, p=0.5, training=self.training)
x = self.lin2(x)
return F.log_softmax(x, dim=-1)
def __repr__(self):
return self.__class__.__name__
# if __name__ == '__main__':
#
# dataset = SynGraphDataset('./datasets', 'ba_2motifs')
# print('author-bot: finish loading the dataset ba_2motifs')
#
# dataset.data.x = dataset.data.x.to(torch.float32)
# dataset.data.x = dataset.data.x[:, :1]
# # dataset.data.y = dataset.data.y[:, 2]
# dim_node = dataset.num_node_features
# dim_edge = dataset.num_edge_features
# # num_targets = dataset.num_classes
# num_classes = dataset.num_classes
#
# splitted_dataset = split_dataset(dataset)
# print('author-bot: finish splitting the dataset')
# splitted_dataset.data.mask = splitted_dataset.data.test_mask
# splitted_dataset.slices['mask'] = splitted_dataset.slices['train_mask']
# dataloader = DataLoader(splitted_dataset, batch_size=1, shuffle=False)
#
# # model = GCN_2l(model_level='node', dim_node=dim_node, dim_hidden=300, num_classes=num_classes)
# model = GCN_2l(model_level='graph', dim_node=dim_node, dim_hidden=300, num_classes=num_classes)
#
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# # model, data = Net().to(device), data.to(device)
# optimizer = torch.optim.Adam(model.parameters(), lr=0.01) # Only perform weight-decay on first convolution.
#
# data = dataset.data
# model.train()
# optimizer.zero_grad()
# F.nll_loss(model(data.x, data.edge_index), data.y).backward()
# optimizer.step()
#
#
# model.eval()
# pred, accs = model(data.x, data.edge_index).argmax(dim=-1), []
# for _, mask in data('train_mask', 'val_mask', 'test_mask'):
# accs.append(int((pred[mask] == data.y[mask]).sum()) / int(mask.sum()))