-
Notifications
You must be signed in to change notification settings - Fork 1
/
layer.py
536 lines (418 loc) · 18.9 KB
/
layer.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
from typing import Optional, Union, Tuple, Callable
from torch import Tensor
from torch_sparse import SparseTensor, matmul, set_diag
import torch.nn.functional as F
from torch_geometric.nn.conv import MessagePassing
from torch_geometric.typing import Adj, PairTensor, OptPairTensor, Size, OptTensor, NoneType, SparseTensor
from torch_scatter import scatter
from torch_geometric.nn import GCNConv
from torch.nn import Sequential, Linear
from torch_geometric.nn.dense.linear import Linear
import torch.nn as nn
import torch
from torch.nn import Parameter
from torch_geometric.utils import remove_self_loops, add_self_loops, softmax
from torch_geometric.nn.inits import glorot, zeros, reset
class NeighborPropagate(MessagePassing):
def __init__(self, aggr: str = 'mean', **kwargs,):
kwargs['aggr'] = aggr if aggr != 'lstm' else None
super().__init__(**kwargs)
def forward(self, x: Union[Tensor, OptPairTensor], edge_index: Adj,
size: Size = None) -> Tensor:
""""""
if isinstance(x, Tensor):
x: OptPairTensor = (x, x)
# propagate_type: (x: OptPairTensor)
out = self.propagate(edge_index, x=x, size=size)
return out
def message(self, x_j: Tensor) -> Tensor:
return x_j
def aggregate(self, x: Tensor, index: Tensor, ptr: Optional[Tensor] = None, dim_size: Optional[int] = None) -> Tensor:
return scatter(x, index, dim=self.node_dim, dim_size=dim_size, reduce=self.aggr)
class GCN(nn.Module):
def __init__(self, args):
super(GCN, self).__init__()
self.args = args
self.num_features = args.num_features
self.nhid = args.nhid
self.num_classes = args.num_classes
self.dropout_ratio = args.dropout_ratio
self.num_layers = args.num_layers
self.convs = nn.ModuleList()
self.bns = nn.ModuleList()
self.convs.append(GCNConv(self.num_features, self.nhid))
self.bns.append(nn.BatchNorm1d(self.nhid))
for _ in range(self.num_layers - 1):
self.convs.append(GCNConv(self.nhid, self.nhid))
self.bns.append(nn.BatchNorm1d(self.nhid))
self.cls = torch.nn.Linear(self.nhid, self.num_classes)
self.activation = F.relu
self.use_bn = args.use_bn
def reset_parameters(self):
for conv in self.convs:
conv.reset_parameters()
for bn in self.bns:
bn.reset_parameters()
def forward(self, x, edge_index, edge_weight=None):
x = self.feat_bottleneck(x, edge_index, edge_weight)
x = self.feat_classifier(x)
return x
def feat_bottleneck(self, x, edge_index, edge_weight=None):
for i, conv in enumerate(self.convs):
x = conv(x, edge_index, edge_weight)
if self.use_bn:
x = self.bns[i](x)
x = self.activation(x)
x = F.dropout(x, p=self.dropout_ratio, training=self.training)
return x
def feat_classifier(self, x):
x = self.cls(x)
return x
class SAGEConv(MessagePassing):
def __init__(self, in_channels: Union[int, Tuple[int, int]],
out_channels: int, normalize: bool = False,
bias: bool = True, **kwargs): # yapf: disable
kwargs.setdefault('aggr', 'mean')
super(SAGEConv, self).__init__(**kwargs)
self.in_channels = in_channels
self.out_channels = out_channels
self.normalize = normalize
if isinstance(in_channels, int):
in_channels = (in_channels, in_channels)
self.lin_l = Linear(in_channels[0], out_channels, bias=bias)
self.lin_r = Linear(in_channels[1], out_channels, bias=False)
self.reset_parameters()
def reset_parameters(self):
self.lin_l.reset_parameters()
self.lin_r.reset_parameters()
def forward(self, x: Union[Tensor, OptPairTensor], edge_index: Adj,
size: Size = None) -> Tensor:
""""""
if 0:
if isinstance(x, Tensor):
x: OptPairTensor = (x, x)
# propagate_type: (x: OptPairTensor)
out = self.propagate(edge_index, x=x, size=size)
out = self.lin_l(out)
else:
if isinstance(x, Tensor):
x: OptPairTensor = (x, x)
out = self.lin_l(x[0])
# propagate_type: (x: OptPairTensor)
out = self.propagate(edge_index, x=(out, out), size=size)
x_r = x[1]
if x_r is not None:
out += self.lin_r(x_r)
if self.normalize:
out = F.normalize(out, p=2., dim=-1)
return out
def message(self, x_j: Tensor) -> Tensor:
return x_j
def message_and_aggregate(self, adj_t: SparseTensor,
x: OptPairTensor) -> Tensor:
# Deleted the following line to make propagation differentiable
# adj_t = adj_t.set_value(None, layout=None)
return matmul(adj_t, x[0], reduce=self.aggr)
def __repr__(self):
return '{}({}, {})'.format(self.__class__.__name__, self.in_channels,
self.out_channels)
class SAGE(nn.Module):
def __init__(self, args):
super(SAGE, self).__init__()
self.args = args
self.num_features = args.num_features
self.nhid = args.nhid
self.num_classes = args.num_classes
self.dropout_ratio = args.dropout_ratio
self.num_layers = args.num_layers
self.convs = nn.ModuleList()
self.bns = nn.ModuleList()
self.convs.append(SAGEConv(self.num_features, self.nhid))
self.bns.append(nn.BatchNorm1d(self.nhid))
for _ in range(self.num_layers - 1):
self.convs.append(SAGEConv(self.nhid, self.nhid))
self.bns.append(nn.BatchNorm1d(self.nhid))
self.cls = torch.nn.Linear(self.nhid, self.num_classes)
self.activation = F.relu
self.use_bn = args.use_bn
def reset_parameters(self):
for conv in self.convs:
conv.reset_parameters()
for bn in self.bns:
bn.reset_parameters()
def forward(self, x, edge_index, edge_weight=None):
x = self.feat_bottleneck(x, edge_index, edge_weight)
x = self.feat_classifier(x)
return x
def feat_bottleneck(self, x, edge_index, edge_weight=None):
if edge_weight is not None:
adj = SparseTensor.from_edge_index(edge_index, edge_weight, sparse_sizes=2 * x.shape[:1]).t()
for i, conv in enumerate(self.convs):
if edge_weight is not None:
x = conv(x, adj)
else:
x = conv(x, edge_index, edge_weight)
if self.use_bn:
x = self.bns[i](x)
x = self.activation(x)
x = F.dropout(x, p=self.dropout_ratio, training=self.training)
return x
def feat_classifier(self, x):
x = self.cls(x)
return x
class GATConv(MessagePassing):
_alpha: OptTensor
def __init__(self, in_channels: Union[int, Tuple[int, int]],
out_channels: int, heads: int = 1, concat: bool = True,
negative_slope: float = 0.2, dropout: float = 0.0,
add_self_loops: bool = True, bias: bool = True, **kwargs):
kwargs.setdefault('aggr', 'add')
super(GATConv, self).__init__(node_dim=0, **kwargs)
self.in_channels = in_channels
self.out_channels = out_channels
self.heads = heads
self.concat = concat
self.negative_slope = negative_slope
self.dropout = dropout
self.add_self_loops = add_self_loops
# In case we are operating in bipartite graphs, we apply separate
# transformations 'lin_src' and 'lin_dst' to source and target nodes:
if isinstance(in_channels, int):
self.lin_src = Linear(in_channels, heads * out_channels,
bias=False, weight_initializer='glorot')
self.lin_dst = self.lin_src
else:
self.lin_src = Linear(in_channels[0], heads * out_channels, False,
weight_initializer='glorot')
self.lin_dst = Linear(in_channels[1], heads * out_channels, False,
weight_initializer='glorot')
# The learnable parameters to compute attention coefficients:
self.att_src = Parameter(torch.Tensor(1, heads, out_channels))
self.att_dst = Parameter(torch.Tensor(1, heads, out_channels))
if bias and concat:
self.bias = Parameter(torch.Tensor(heads * out_channels))
elif bias and not concat:
self.bias = Parameter(torch.Tensor(out_channels))
else:
self.register_parameter('bias', None)
self._alpha = None
self.reset_parameters()
self.edge_weight = None
def reset_parameters(self):
self.lin_src.reset_parameters()
self.lin_dst.reset_parameters()
glorot(self.att_src)
glorot(self.att_dst)
zeros(self.bias)
def forward(self, x: Union[Tensor, OptPairTensor], edge_index: Adj,
size: Size = None, return_attention_weights=None, edge_weight=None):
# type: (Union[Tensor, OptPairTensor], Tensor, Size, NoneType) -> Tensor # noqa
# type: (Union[Tensor, OptPairTensor], SparseTensor, Size, NoneType) -> Tensor # noqa
# type: (Union[Tensor, OptPairTensor], Tensor, Size, bool) -> Tuple[Tensor, Tuple[Tensor, Tensor]] # noqa
# type: (Union[Tensor, OptPairTensor], SparseTensor, Size, bool) -> Tuple[Tensor, SparseTensor] # noqa
r"""
Args:
return_attention_weights (bool, optional): If set to :obj:`True`,
will additionally return the tuple
:obj:`(edge_index, attention_weights)`, holding the computed
attention weights for each edge. (default: :obj:`None`)
"""
H, C = self.heads, self.out_channels
# We first transform the input node features. If a tuple is passed, we
# transform source and target node features via separate weights:
if isinstance(x, Tensor):
assert x.dim() == 2, "Static graphs not supported in 'GATConv'"
x_src = x_dst = self.lin_src(x).view(-1, H, C)
else: # Tuple of source and target node features:
x_src, x_dst = x
assert x_src.dim() == 2, "Static graphs not supported in 'GATConv'"
x_src = self.lin_src(x_src).view(-1, H, C)
if x_dst is not None:
x_dst = self.lin_dst(x_dst).view(-1, H, C)
x = (x_src, x_dst)
# Next, we compute node-level attention coefficients, both for source
# and target nodes (if present):
alpha_src = (x_src * self.att_src).sum(dim=-1)
alpha_dst = None if x_dst is None else (x_dst * self.att_dst).sum(-1)
alpha = (alpha_src, alpha_dst)
if self.add_self_loops:
if isinstance(edge_index, Tensor):
# We only want to add self-loops for nodes that appear both as
# source and target nodes:
num_nodes = x_src.size(0)
if x_dst is not None:
num_nodes = min(num_nodes, x_dst.size(0))
num_nodes = min(size) if size is not None else num_nodes
# edge_index, _ = remove_self_loops(edge_index)
# edge_index, _ = add_self_loops(edge_index, num_nodes=num_nodes)
edge_index, edge_weight = remove_self_loops(edge_index, edge_weight)
edge_index, edge_weight = add_self_loops(edge_index, edge_weight, num_nodes=num_nodes)
self.edge_weight = edge_weight
# if edge_index.size(1) != self.edge_weight.shape[0]:
# self.edge_weight = None
elif isinstance(edge_index, SparseTensor):
edge_index = set_diag(edge_index)
# propagate_type: (x: OptPairTensor, alpha: OptPairTensor)
out = self.propagate(edge_index, x=x, alpha=alpha, size=size)
alpha = self._alpha
assert alpha is not None
self._alpha = None
if self.concat:
out = out.view(-1, self.heads * self.out_channels)
else:
out = out.mean(dim=1)
if self.bias is not None:
out += self.bias
if isinstance(return_attention_weights, bool):
if isinstance(edge_index, Tensor):
return out, (edge_index, alpha)
elif isinstance(edge_index, SparseTensor):
return out, edge_index.set_value(alpha, layout='coo')
else:
return out
def message(self, x_j: Tensor, alpha_j: Tensor, alpha_i: OptTensor,
index: Tensor, ptr: OptTensor,
size_i: Optional[int]) -> Tensor:
# Given egel-level attention coefficients for source and target nodes,
# we simply need to sum them up to "emulate" concatenation:
alpha = alpha_j if alpha_i is None else alpha_j + alpha_i
alpha = F.leaky_relu(alpha, self.negative_slope)
alpha = softmax(alpha, index, ptr, size_i)
self._alpha = alpha # Save for later use.
alpha = F.dropout(alpha, p=self.dropout, training=self.training)
if self.edge_weight is not None:
x_j = self.edge_weight.view(-1, 1, 1) * x_j
return x_j * alpha.unsqueeze(-1)
def __repr__(self):
return '{}({}, {}, heads={})'.format(self.__class__.__name__,
self.in_channels,
self.out_channels, self.heads)
class GAT(nn.Module):
def __init__(self, args):
super(GAT, self).__init__()
self.args = args
self.num_features = args.num_features
self.nhid = args.nhid
self.num_classes = args.num_classes
self.dropout_ratio = args.dropout_ratio
self.num_layers = args.num_layers
self.convs = nn.ModuleList()
self.bns = nn.ModuleList()
self.convs.append(GATConv(self.num_features, self.nhid, heads=1, concat=False))
self.bns.append(nn.BatchNorm1d(self.nhid))
for _ in range(self.num_layers - 1):
self.convs.append(GATConv(self.nhid, self.nhid, heads=1, concat=False))
self.bns.append(nn.BatchNorm1d(self.nhid))
self.cls = torch.nn.Linear(self.nhid, self.num_classes)
self.activation = F.relu
self.use_bn = args.use_bn
def reset_parameters(self):
for conv in self.convs:
conv.reset_parameters()
for bn in self.bns:
bn.reset_parameters()
def forward(self, x, edge_index, edge_weight=None):
x = self.feat_bottleneck(x, edge_index, edge_weight)
x = self.feat_classifier(x)
return x
def _ensure_contiguousness(self, x, edge_idx, edge_weight):
if not x.is_sparse:
x = x.contiguous()
if hasattr(edge_idx, 'contiguous'):
edge_idx = edge_idx.contiguous()
if edge_weight is not None:
edge_weight = edge_weight.contiguous()
return x, edge_idx, edge_weight
def feat_bottleneck(self, x, edge_index, edge_weight=None):
x, edge_index, edge_weight = self._ensure_contiguousness(x, edge_index, edge_weight)
for i, conv in enumerate(self.convs):
x = conv(x, edge_index, edge_weight=edge_weight)
if self.use_bn:
x = self.bns[i](x)
x = self.activation(x)
x = F.dropout(x, p=self.dropout_ratio, training=self.training)
return x
def feat_classifier(self, x):
x = self.cls(x)
return x
class GINConv(MessagePassing):
def __init__(self, nn: Callable, eps: float = 0., train_eps: bool = False,
**kwargs):
kwargs.setdefault('aggr', 'add')
super().__init__(**kwargs)
self.nn = nn
self.initial_eps = eps
if train_eps:
self.eps = torch.nn.Parameter(torch.Tensor([eps]))
else:
self.register_buffer('eps', torch.Tensor([eps]))
self.reset_parameters()
def reset_parameters(self):
super().reset_parameters()
reset(self.nn)
self.eps.data.fill_(self.initial_eps)
def forward(self, x: Union[Tensor, OptPairTensor], edge_index: Adj,
size: Size = None) -> Tensor:
if isinstance(x, Tensor):
x: OptPairTensor = (x, x)
# propagate_type: (x: OptPairTensor)
out = self.propagate(edge_index, x=x, size=size)
x_r = x[1]
if x_r is not None:
out = out + (1 + self.eps) * x_r
return self.nn(out)
def message(self, x_j: Tensor) -> Tensor:
return x_j
def message_and_aggregate(self, adj_t: SparseTensor,
x: OptPairTensor) -> Tensor:
# if isinstance(adj_t, SparseTensor):
# adj_t = adj_t.set_value(None, layout=None)
return matmul(adj_t, x[0], reduce=self.aggr)
def __repr__(self) -> str:
return f'{self.__class__.__name__}(nn={self.nn})'
class GIN(nn.Module):
def __init__(self, args):
super(GIN, self).__init__()
self.args = args
self.num_features = args.num_features
self.nhid = args.nhid
self.num_classes = args.num_classes
self.dropout_ratio = args.dropout_ratio
self.num_layers = args.num_layers
self.convs = nn.ModuleList()
self.bns = nn.ModuleList()
self.lin = torch.nn.Linear(self.num_features, self.nhid)
self.convs.append(GINConv(Sequential(Linear(self.nhid, self.nhid)), train_eps=True))
self.bns.append(nn.BatchNorm1d(self.nhid))
for _ in range(self.num_layers - 1):
self.convs.append(GINConv(Sequential(Linear(self.nhid, self.nhid)), train_eps=True))
self.bns.append(nn.BatchNorm1d(self.nhid))
self.cls = torch.nn.Linear(self.nhid, self.num_classes)
self.activation = F.relu
self.use_bn = args.use_bn
def reset_parameters(self):
for conv in self.convs:
conv.reset_parameters()
for bn in self.bns:
bn.reset_parameters()
def forward(self, x, edge_index, edge_weight=None):
x = self.feat_bottleneck(x, edge_index, edge_weight)
x = self.feat_classifier(x)
return x
def feat_bottleneck(self, x, edge_index, edge_weight=None):
x = self.lin(x)
if edge_weight is not None:
adj = SparseTensor.from_edge_index(edge_index, edge_weight, sparse_sizes=2 * x.shape[:1]).t()
for i, conv in enumerate(self.convs):
if edge_weight is not None:
x = conv(x, adj)
else:
x = conv(x, edge_index, edge_weight)
if self.use_bn:
x = self.bns[i](x)
x = self.activation(x)
x = F.dropout(x, p=self.dropout_ratio, training=self.training)
return x
def feat_classifier(self, x):
x = self.cls(x)
return x