-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconv.py
418 lines (355 loc) · 16 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
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
import torch
from typing import List, Optional, Tuple, Union
from torch.nn import LSTM
from torch_geometric.nn.aggr import Aggregation, MultiAggregation
from torch_geometric.utils import spmm
from torch_geometric.utils import remove_self_loops, add_self_loops, softmax, degree
from util import shortest_path_length
from typing import Optional, Tuple, Union
import torch
import torch.nn.functional as F
from torch import Tensor
from torch.nn import Parameter
from torch_geometric.nn.conv import MessagePassing
from torch_geometric.nn.dense.linear import Linear
from torch_geometric.nn.inits import glorot, zeros
from torch_geometric.typing import NoneType # noqa
from torch_geometric.typing import (
Adj,
OptPairTensor,
OptTensor,
Size,
SparseTensor,
torch_sparse,
)
from torch_geometric.utils import (
add_self_loops,
is_torch_sparse_tensor,
remove_self_loops,
softmax,
)
from torch_geometric.utils.sparse import set_sparse_value
class SAGEConv(MessagePassing):
def __init__(
self,
in_channels: Union[int, Tuple[int, int]],
out_channels: int,
aggr: Optional[Union[str, List[str], Aggregation]] = "mean",
normalize: bool = False,
root_weight: bool = True,
project: bool = False,
bias: bool = True,
**kwargs,
):
self.in_channels = in_channels
self.out_channels = out_channels
self.normalize = normalize
self.root_weight = root_weight
self.project = project
if isinstance(in_channels, int):
in_channels = (in_channels, in_channels)
if aggr == 'lstm':
kwargs.setdefault('aggr_kwargs', {})
kwargs['aggr_kwargs'].setdefault('in_channels', in_channels[0])
kwargs['aggr_kwargs'].setdefault('out_channels', in_channels[0])
super().__init__(aggr, **kwargs)
if self.project:
self.lin = Linear(in_channels[0], in_channels[0], bias=True)
if self.aggr is None:
self.fuse = False # No "fused" message_and_aggregate.
self.lstm = LSTM(in_channels[0], in_channels[0], batch_first=True)
if isinstance(self.aggr_module, MultiAggregation):
aggr_out_channels = self.aggr_module.get_out_channels(
in_channels[0])
else:
aggr_out_channels = in_channels[0]
self.lin_l = Linear(aggr_out_channels, out_channels, bias=bias)
if self.root_weight:
self.lin_r = Linear(in_channels[1], out_channels, bias=False)
self.reset_parameters()
def reset_parameters(self):
super().reset_parameters()
if self.project:
self.lin.reset_parameters()
self.lin_l.reset_parameters()
if self.root_weight:
self.lin_r.reset_parameters()
def forward(self, x: Union[Tensor, OptPairTensor], edge_index: Adj, edge_weight,
size: Size = None) -> Tensor:
if isinstance(x, Tensor):
x: OptPairTensor = (x, x)
if self.project and hasattr(self, 'lin'):
x = (self.lin(x[0]).relu(), x[1])
# propagate_type: (x: OptPairTensor)
out = self.propagate(edge_index, x=x, edge_weight=edge_weight, size=size)
out = self.lin_l(out)
x_r = x[1]
if self.root_weight and x_r is not None:
out = 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, edge_weight) -> Tensor:
return x_j if edge_weight is None else edge_weight.view(-1, 1) * 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 spmm(adj_t, x[0], reduce=self.aggr)
def __repr__(self) -> str:
return (f'{self.__class__.__name__}({self.in_channels}, '
f'{self.out_channels}, aggr={self.aggr})')
class GATConv(MessagePassing):
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,
edge_dim: Optional[int] = None,
fill_value: Union[float, Tensor, str] = 'mean',
bias: bool = True,
**kwargs,
):
kwargs.setdefault('aggr', 'add')
super().__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
self.edge_dim = edge_dim
self.fill_value = fill_value
# 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 edge_dim is not None:
self.lin_edge = Linear(edge_dim, heads * out_channels, bias=False,
weight_initializer='glorot')
self.att_edge = Parameter(torch.Tensor(1, heads, out_channels))
else:
self.lin_edge = None
self.register_parameter('att_edge', None)
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.reset_parameters()
def reset_parameters(self):
super().reset_parameters()
self.lin_src.reset_parameters()
self.lin_dst.reset_parameters()
if self.lin_edge is not None:
self.lin_edge.reset_parameters()
glorot(self.att_src)
glorot(self.att_dst)
glorot(self.att_edge)
zeros(self.bias)
def forward(self, x: Union[Tensor, OptPairTensor], edge_index: Adj, edge_weight: OptTensor = None,
edge_attr: OptTensor = None, size: Size = None,
return_attention_weights=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, edge_weight = remove_self_loops(
edge_index, edge_weight)
edge_index, edge_weight = add_self_loops(
edge_index, edge_weight, fill_value=self.fill_value,
num_nodes=num_nodes)
elif isinstance(edge_index, SparseTensor):
if self.edge_dim is None:
edge_index = torch_sparse.set_diag(edge_index)
else:
raise NotImplementedError(
"The usage of 'edge_attr' and 'add_self_loops' "
"simultaneously is currently not yet supported for "
"'edge_index' in a 'SparseTensor' form")
# edge_updater_type: (alpha: OptPairTensor, edge_attr: OptTensor)
alpha = self.edge_updater(edge_index, alpha=alpha, edge_attr=edge_attr)
# propagate_type: (x: OptPairTensor, alpha: Tensor)
out = self.propagate(edge_index, x=x, alpha=alpha, size=size, edge_weight=edge_weight)
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 = out + self.bias
if isinstance(return_attention_weights, bool):
if isinstance(edge_index, Tensor):
if is_torch_sparse_tensor(edge_index):
# TODO TorchScript requires to return a tuple
adj = set_sparse_value(edge_index, alpha)
return out, (adj, alpha)
else:
return out, (edge_index, alpha)
elif isinstance(edge_index, SparseTensor):
return out, edge_index.set_value(alpha, layout='coo')
else:
return out
def edge_update(self, alpha_j: Tensor, alpha_i: OptTensor,
edge_attr: OptTensor, index: Tensor, ptr: OptTensor,
size_i: Optional[int]) -> Tensor:
# Given edge-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
if index.numel() == 0:
return alpha
if edge_attr is not None and self.lin_edge is not None:
if edge_attr.dim() == 1:
edge_attr = edge_attr.view(-1, 1)
edge_attr = self.lin_edge(edge_attr)
edge_attr = edge_attr.view(-1, self.heads, self.out_channels)
alpha_edge = (edge_attr * self.att_edge).sum(dim=-1)
alpha = alpha + alpha_edge
alpha = F.leaky_relu(alpha, self.negative_slope)
alpha = softmax(alpha, index, ptr, size_i)
alpha = F.dropout(alpha, p=self.dropout, training=self.training)
return alpha
def message(self, x_j: Tensor, alpha: Tensor, edge_weight: OptTensor) -> Tensor:
return alpha.unsqueeze(-1) * x_j if edge_weight is None else edge_weight.view(-1, 1, 1) * alpha.unsqueeze(-1) * x_j
def __repr__(self) -> str:
return (f'{self.__class__.__name__}({self.in_channels}, '
f'{self.out_channels}, heads={self.heads})')
class CalibAttentionLayer(MessagePassing):
_alpha: OptTensor
def __init__(
self,
in_channels: int,
out_channels: int,
edge_index: Adj,
num_nodes: int,
train_mask: Tensor,
dist_to_train: Tensor = None,
heads: int = 8,
negative_slope: float = 0.2,
bias: float = 1,
self_loops: bool = True,
fill_value: Union[float, Tensor, str] = 'mean',
bfs_depth=2,
**kwargs,
):
kwargs.setdefault('aggr', 'add')
super().__init__(node_dim=0, **kwargs)
self.in_channels = in_channels
self.out_channels = out_channels
self.heads = heads
self.negative_slope = negative_slope
self.fill_value = fill_value
self.edge_index = edge_index
self.num_nodes = num_nodes
self.temp_lin = Linear(in_channels, heads,
bias=False, weight_initializer='glorot')
# The learnable clustering coefficient for training node and their neighbors
self.conf_coef = Parameter(torch.zeros([]))
self.bias = Parameter(torch.ones(1) * bias)
self.train_a = Parameter(torch.ones(1))
self.dist1_a = Parameter(torch.ones(1))
# Compute the distances to the nearest training node of each node
dist_to_train = dist_to_train if dist_to_train is not None else shortest_path_length(edge_index, train_mask, bfs_depth)
self.register_buffer('dist_to_train', dist_to_train)
self.reset_parameters()
if self_loops:
# We only want to add self-loops for nodes that appear both as
# source and target nodes:
self.edge_index, _ = remove_self_loops(
self.edge_index, None)
self.edge_index, _ = add_self_loops(
self.edge_index, None, fill_value=self.fill_value,
num_nodes=num_nodes)
def reset_parameters(self):
self.temp_lin.reset_parameters()
def forward(self, x: Union[Tensor, OptPairTensor]):
N, H = self.num_nodes, self.heads
# Individual Temperature
normalized_x = x - torch.min(x, 1, keepdim=True)[0]
normalized_x /= torch.max(x, 1, keepdim=True)[0] - \
torch.min(x, 1, keepdim=True)[0]
# t_delta for individual nodes
# x_sorted_scalar: [N, 1]
x_sorted = torch.sort(normalized_x, -1)[0]
temp = self.temp_lin(x_sorted)
# Next, we assign spatial coefficient
# a_cluster:[N]
a_cluster = torch.ones(N, dtype=torch.float32, device=x[0].device)
a_cluster[self.dist_to_train == 0] = self.train_a
a_cluster[self.dist_to_train == 1] = self.dist1_a
# For confidence smoothing
conf = F.softmax(x, dim=1).amax(-1)
deg = degree(self.edge_index[0, :], self.num_nodes)
deg_inverse = 1 / deg
deg_inverse[deg_inverse == float('inf')] = 0
out = self.propagate(self.edge_index,
temp=temp.view(N, H) * a_cluster.unsqueeze(-1),
alpha=x / a_cluster.unsqueeze(-1),
conf=conf)
sim, dconf = out[:, :-1], out[:, -1:]
out = F.softplus(sim + self.conf_coef * dconf * deg_inverse.unsqueeze(-1))
out = out.mean(dim=1) + self.bias
return out.unsqueeze(1)
def message(
self,
temp_j: Tensor,
alpha_j: Tensor,
alpha_i: OptTensor,
conf_i: Tensor,
conf_j: Tensor,
index: Tensor,
ptr: OptTensor,
size_i: Optional[int]) -> Tensor:
"""
alpha_i, alpha_j: [E, H]
temp_j: [E, H]
"""
if alpha_i is None:
print("alphai is none")
alpha = (alpha_j * alpha_i).sum(dim=-1)
alpha = F.leaky_relu(alpha, self.negative_slope)
alpha = softmax(alpha, index, ptr, size_i)
# Agreement smoothing + Confidence smoothing
return torch.cat([
(temp_j * alpha.unsqueeze(-1).expand_as(temp_j)),
(conf_i - conf_j).unsqueeze(-1)], -1)
def __repr__(self) -> str:
return (
f'{self.__class__.__name__}{self.out_channels}, heads={self.heads}')