-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
474 lines (461 loc) · 17 KB
/
util.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
import torch
import torch_geometric
import inspect
from torch_scatter import scatter_sum
from torch_geometric.typing import Adj, OptTensor, PairTensor, Optional
import util
from .nn.conv.flow_conv import FlowConv
# Setup str -> function maps for all PyTorch and PYG functions/classes needed for model definition.
# These maps allow users to call, for example, the LSTM module using layer_fn_map["LSTM"]().
# This allows for higher-level model definitions that are agnostic of chosen layers, activations, etc.
# For example, we can now define a cell-agnostic RNN model by passing a layer argument (e.g. layer="LSTM")
# that specifies the specific layer to use. Thus, this model would define a general RNN architecture,
# such as sequence-to-sequence, with the specific layer type as a hyper-parameter.
# init gcn layer -> class constructor function map
gcnlayer_fn_map = {}
for name, fn in inspect.getmembers(torch_geometric.nn.conv, inspect.isclass):
if issubclass(fn, torch_geometric.nn.conv.MessagePassing) and not name in ["MessagePassing"]:
gcnlayer_fn_map[name] = fn
for name, fn in inspect.getmembers(torch_geometric.nn.dense, inspect.isclass):
if name.endswith("Conv"):
gcnlayer_fn_map[name] = fn
gcnlayer_fn_map["FlowConv"] = FlowConv
# init gcn layer -> supported feature list map
gcnlayer_supported_map = {
"APPNP": ["SparseTensor", "edge_weight", "static"],
"ARMAConv": ["SparseTensor", "edge_weight", "static", "lazy"],
"CGConv": ["SparseTensor", "edge_attr", "bipartite", "static"],
"ChebConv": ["edge_weight", "static", "lazy"],
"DNAConv": ["SparseTensor", "edge_weight"],
"FAConv": ["SparseTensor", "edge_weight", "static", "lazy"],
"FlowConv": ["SparseTensor", "edge_weight", "static", "lazy"],
"GCNConv": ["SparseTensor", "edge_weight", "static", "lazy"],
"GCN2Conv": ["SparseTensor", "edge_weight", "static"],
"GraphConv": ["SparseTensor", "edge_weight", "bipartite","static", "lazy"],
"GatedGraphConv": ["SparseTensor", "edge_weight", "static"],
"LEConv": ["SparseTensor", "edge_weight", "bipartite", "static", "lazy"],
"LGConv": ["SparseTensor", "edge_weight", "static"],
"ResGatedGraphConv": ["SparseTensor", "bipartite", "static", "lazy"],
"SAGEConv": ["SparseTensor", "bipartite", "static", "lazy"],
"SGConv": ["SparseTensor", "edge_weight", "static", "lazy"],
"TAGConv": ["SparseTensor", "edge_weight", "static", "lazy"],
}
# init gcn layer -> requirements dict map
gcnlayer_required_map = {
"AGNNConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"APPNP": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"edge_weight.shape": ["?", "|E|"],
},
"ARMAConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"edge_weight.shape": ["|E|"],
},
"CGConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"ChebConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"edge_weight.shape": ["|E|"],
},
"ClusterGCNConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"DenseGCNConv": {
"inputs": ["x", "adj"],
"x.shape": ["?", "|V|", "F"],
"adj.shape": ["?", "|V|", "|V|"],
},
"DenseGINConv": {
"inputs": ["x", "adj"],
"x.shape": ["?", "|V|", "F"],
"adj.shape": ["?", "|V|", "|V|"],
},
"DenseGraphConv": {
"inputs": ["x", "adj"],
"x.shape": ["N", "|V|", "F"],
"adj.shape": ["N", "|V|", "|V|"],
},
"DenseSAGEConv": {
"inputs": ["x", "adj"],
"x.shape": ["?", "|V|", "F"],
"adj.shape": ["?", "|V|", "|V|"],
},
"DNAConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"DynamicEdgeConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"ECConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"EdgeConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"EGConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"FAConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"FastRGCNConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"FeaStConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"FlowConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"FiLMConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"edge_weight.shape": ["?", "|E|"],
},
"GatedGraphConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"GATConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"GATv2Conv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"GCN2Conv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"GCNConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"edge_weight.shape": ["|E|"],
},
"GENConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"GeneralConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"GINConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"GINEConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"GMMConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"GraphConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"edge_weight.shape": ["|E|"],
},
"GravNetConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"HGTConv": {
"inputs": ["x_dict", "edge_index_dict"],
"x_dict.shape": ["|V|", "F"],
"edge_index_dict.shape": [2, "|E|"],
},
"HypergraphConv": {
"inputs": ["x", "hyperedge_index"],
"x.shape": ["|V|", "F"],
"hyperedge_index.shape": ["|V|", "|E|"],
},
"LEConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"edge_weight.shape": ["|E|"],
},
"LEConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"edge_weight.shape": ["|E|"],
},
"MFConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"NNConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"PANConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"PDNConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"PNAConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"PointConv": {
"inputs": ["x", "pos", "edge_index"],
"x.shape": ["|V|", "F"],
"pos.shape": ["|V|", 3],
"edge_index.shape": [2, "|E|"],
},
"PointNetConv": {
"inputs": ["x", "pos", "edge_index"],
"x.shape": ["|V|", "F"],
"pos.shape": ["|V|", 3],
"edge_index.shape": [2, "|E|"],
},
"PPFConv": {
"inputs": ["x", "pos", "normal", "edge_index"],
"x.shape": ["|V|", "F"],
"pos.shape": ["|V|", 3],
"normal.shape": ["|V|", 3],
"edge_index.shape": [2, "|E|"],
},
"ResGatedGraphConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"RGCNConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"SAGEConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"SGConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"edge_weight.shape": ["|E|"],
},
"SignedConv": {
"inputs": ["x", "pos_edge_index", "neg_edge_index"],
"x.shape": ["?", "|V|", "F"],
"pos_edge_index.shape": [2, "|E^(+)|"],
"neg_edge_index.shape": [2, "|E^(-)|"],
},
"SplineConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
"SuperGATConv": {
"inputs": ["x", "edge_index", "neg_edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"neg_edge_index.shape": [2, "|E^(-)|"],
},
"TAGConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["?", "|V|", "F"],
"edge_index.shape": [2, "|E|"],
"edge_weight.shape": ["|E|"],
},
"TransformerConv": {
"inputs": ["x", "edge_index"],
"x.shape": ["|V|", "F"],
"edge_index.shape": [2, "|E|"],
},
}
def add_remaining_self_loops(edge_index, edge_weight: Optional[torch.Tensor] = None, fill_value: float = 1., num_nodes: Optional[int] = None):
r"""Adds remaining self-loop :math:`(i,i) \in \mathcal{E}` to every node
:math:`i \in \mathcal{V}` in the graph given by :attr:`edge_index`.
In case the graph is weighted and already contains a few self-loops, only
non-existent self-loops will be added with edge weights denoted by
:obj:`fill_value`.
Args:
edge_index (LongTensor): The edge indices.
edge_weight (Tensor, optional): One-dimensional edge weights.
(default: :obj:`None`)
fill_value (float, optional): If :obj:`edge_weight` is not :obj:`None`,
will add self-loops with edge weights of :obj:`fill_value` to the
graph. (default: :obj:`1.`)
num_nodes (int, optional): The number of nodes, *i.e.*
:obj:`max_val + 1` of :attr:`edge_index`. (default: :obj:`None`)
:rtype: (:class:`LongTensor`, :class:`Tensor`)
"""
if edge_index.dim() > 2:
orig_size = edge_index.size()
n_edges = orig_size[-1]
edge_index = edge_index.view(-1, 2, n_edges)
edge_index_set, edge_weight_set = [], []
for i in range(edge_index.size(0)):
_edge_index = edge_index[i]
_edge_weight = edge_weight
if not edge_weight is None:
_edge_weight = edge_weight[i]
_edge_index, _edge_weight = torch_geometric.utils.add_remaining_self_loops(
_edge_index, _edge_weight, fill_value, num_nodes
)
edge_index_set.append(_edge_index)
edge_weight_set.append(_edge_weight)
try:
edge_index = torch.cat(edge_index_set)
except RuntimeError as e:
if "Sizes of tensors must match" in str(e):
raise RuntimeError(
(
"Failed to add self loops to dynamic edge_index because number "
"of added self loops was not uniform across instances resulting "
"in a ragged tensor."
)
)
edge_index = edge_index.view(orig_size[:-1] + edge_index.size()[-1:])
if not edge_weight is None:
edge_weight = torch.cat(edge_weight_set, 0)
edge_weight = edge_weight.view(orig_size[:-1] + edge_weight.size()[-1:])
return edge_index, edge_weight
return torch_geometric.utils.add_remaining_self_loops(edge_index, edge_weight, fill_value, num_nodes)
def get_sink_sums(edge_index, edge_weight, normalize, debug=0):
source_index = torch.squeeze(
torch.index_select(edge_index, -2, torch.tensor(0, device=edge_weight.device)), -2
)
sink_index = torch.squeeze(
torch.index_select(edge_index, -2, torch.tensor(1, device=edge_weight.device)), -2
)
if normalize == 1: # edge_weight.shape=(?, |E|)
if debug:
print("sink_index =", sink_index.shape, "=")
if debug > 1:
print(sink_index)
if sink_index.shape > edge_weight.shape:
edge_weight = edge_weight.expand(sink_index.size())
if debug:
print("edge_weight reshaped =", edge_weight.shape, "=")
if debug > 1:
print(edge_weight)
sink_sums = scatter_sum(edge_weight, sink_index, dim=-1)
if debug:
print("sink_sums 1. =", sink_sums.shape, "=")
if debug > 1:
print(sink_sums)
sink_sums = torch.take_along_dim(sink_sums, sink_index, -1)
if debug:
print("sink_sums 2. =", sink_sums.shape, "=")
if debug > 1:
print(sink_sums)
# Check for and fix unsafe division - Summation s=0 when w(i,j)=0 for all i
# For e(i,j), setting s=1 gives j = w(i,j) / s * i => j = 0 / 1 * i
# This does not transform w(i,j) but the computation is correct with i=0
sink_sums[sink_sums == 0] = 1
if debug:
print("sink_sums 3. =", sink_sums.shape, "=")
if debug > 1:
print(sink_sums)
elif normalize == 2: # edge_weight.shape=(?, |V|)
if debug:
print("source_index =", source_index.shape, "=")
if debug > 1:
print(source_index)
if debug:
print("sink_index =", sink_index.shape, "=")
if debug > 1:
print(sink_index)
source_weight = torch.take_along_dim(edge_weight, source_index, -1)
sink_weight = torch.take_along_dim(edge_weight, sink_index, -1)
if 0:
if sink_index.shape > edge_weight.shape:
edge_weight = edge_weight.expand(sink_index.size())
if debug:
print("edge_weight reshaped =", edge_weight.shape, "=")
if debug > 1:
print(edge_weight)
# Get summation of edge weights for sink node i across all incoming nodes j \in N(i)
# For method=2, this is simply the feature value (streamflow) of the sink node
sink_sums = sink_weight
if debug:
print("sink_sums 1. =", sink_sums.shape, "=")
if debug > 1:
print(sink_sums)
# Check for and fix unsafe division - Summation s=0 when w(j,?)=0
# For e(i,j), setting s=1 gives j = w(i,j) / s * i => j = w(i,j) / 1 * i
# This does not transform w(i,j) AND the computation is incorrect!
# ***The best we can do is use normalization=1 since the s in undefined in this situation
mask = sink_sums == 0
sink_sums[mask] = get_sink_sums(edge_index, source_weight, 1)[mask]
else:
raise NotImplementedError("Unknown option for \"normalize\" %d" % (normalize))
return sink_sums
def normalize_edge_weight(edge_index, edge_weight, normalize, debug=0):
sink_sums = get_sink_sums(edge_index, edge_weight, normalize, debug)
source_index = torch.squeeze(
torch.index_select(edge_index, -2, torch.tensor(0, device=edge_weight.device)), -2
)
sink_index = torch.squeeze(
torch.index_select(edge_index, -2, torch.tensor(1, device=edge_weight.device)), -2
)
if normalize == 1: # edge_weight.shape=(?, |E|)
edge_weight = edge_weight / sink_sums
elif normalize == 2: # edge_weight.shape=(?, |V|)
source_weight = torch.take_along_dim(edge_weight, source_index, -1)
edge_weight = source_weight / sink_sums
else:
raise NotImplementedError("Unknown option for \"normalize\" %d" % (normalize))
return edge_weight