-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpooling.py
244 lines (169 loc) · 8.39 KB
/
pooling.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
from typing import Callable, Optional, Union
import torch
from torch.nn import Parameter
from torch_scatter import scatter, scatter_add, scatter_max, scatter_mean
from torch_geometric.data import Batch
from torch_geometric.utils import softmax, remove_self_loops, coalesce
from torch_geometric.utils.num_nodes import maybe_num_nodes
from torch_geometric.nn.inits import uniform
from torch_geometric.nn.pool.consecutive import consecutive_cluster
def topk(x, ratio, batch, min_score=None, tol=1e-7):
if min_score is not None:
# Make sure that we do not drop all nodes in a graph.
scores_max = scatter_max(x, batch)[0].index_select(0, batch) - tol
scores_min = scores_max.clamp(max=min_score)
perm = (x > scores_min).nonzero(as_tuple=False).view(-1)
else:
num_nodes = scatter_add(batch.new_ones(x.size(0)), batch, dim=0)
batch_size, max_num_nodes = num_nodes.size(0), num_nodes.max().item()
cum_num_nodes = torch.cat(
[num_nodes.new_zeros(1),
num_nodes.cumsum(dim=0)[:-1]], dim=0)
index = torch.arange(batch.size(0), dtype=torch.long, device=x.device)
index = (index - cum_num_nodes[batch]) + (batch * max_num_nodes)
dense_x = x.new_full((batch_size * max_num_nodes, ),
torch.finfo(x.dtype).min)
dense_x[index] = x
dense_x = dense_x.view(batch_size, max_num_nodes)
_, perm = dense_x.sort(dim=-1, descending=True)
perm = perm + cum_num_nodes.view(-1, 1)
perm = perm.view(-1)
if isinstance(ratio, int):
k = num_nodes.new_full((num_nodes.size(0), ), ratio)
k = torch.min(k, num_nodes)
else:
k = (ratio * num_nodes.to(torch.float)).ceil().to(torch.long)
mask = [
torch.arange(k[i], dtype=torch.long, device=x.device) +
i * max_num_nodes for i in range(batch_size)
]
mask = torch.cat(mask, dim=0)
perm = perm[mask]
return perm
def filter_adj(edge_index, edge_attr, perm, num_nodes=None):
num_nodes = maybe_num_nodes(edge_index, num_nodes)
mask = perm.new_full((num_nodes, ), -1)
i = torch.arange(perm.size(0), dtype=torch.long, device=perm.device)
mask[perm] = i
row, col = edge_index
row, col = mask[row], mask[col]
mask = (row >= 0) & (col >= 0)
row, col = row[mask], col[mask]
if edge_attr is not None:
edge_attr = edge_attr[mask]
return torch.stack([row, col], dim=0), edge_attr, mask
class TopKPooling_Mod(torch.nn.Module):
r""":math:`\mathrm{top}_k` pooling operator from the `"Graph U-Nets"
<https://arxiv.org/abs/1905.05178>`_, `"Towards Sparse
Hierarchical Graph Classifiers" <https://arxiv.org/abs/1811.01287>`_
and `"Understanding Attention and Generalization in Graph Neural
Networks" <https://arxiv.org/abs/1905.02850>`_ papers
if min_score :math:`\tilde{\alpha}` is None:
.. math::
\mathbf{y} &= \frac{\mathbf{X}\mathbf{p}}{\| \mathbf{p} \|}
\mathbf{i} &= \mathrm{top}_k(\mathbf{y})
\mathbf{X}^{\prime} &= (\mathbf{X} \odot
\mathrm{tanh}(\mathbf{y}))_{\mathbf{i}}
\mathbf{A}^{\prime} &= \mathbf{A}_{\mathbf{i},\mathbf{i}}
if min_score :math:`\tilde{\alpha}` is a value in [0, 1]:
.. math::
\mathbf{y} &= \mathrm{softmax}(\mathbf{X}\mathbf{p})
\mathbf{i} &= \mathbf{y}_i > \tilde{\alpha}
\mathbf{X}^{\prime} &= (\mathbf{X} \odot \mathbf{y})_{\mathbf{i}}
\mathbf{A}^{\prime} &= \mathbf{A}_{\mathbf{i},\mathbf{i}},
where nodes are dropped based on a learnable projection score
:math:`\mathbf{p}`.
Args:
in_channels (int): Size of each input sample.
ratio (float or int): Graph pooling ratio, which is used to compute
:math:`k = \lceil \mathrm{ratio} \cdot N \rceil`, or the value
of :math:`k` itself, depending on whether the type of :obj:`ratio`
is :obj:`float` or :obj:`int`.
This value is ignored if :obj:`min_score` is not :obj:`None`.
(default: :obj:`0.5`)
min_score (float, optional): Minimal node score :math:`\tilde{\alpha}`
which is used to compute indices of pooled nodes
:math:`\mathbf{i} = \mathbf{y}_i > \tilde{\alpha}`.
When this value is not :obj:`None`, the :obj:`ratio` argument is
ignored. (default: :obj:`None`)
multiplier (float, optional): Coefficient by which features gets
multiplied after pooling. This can be useful for large graphs and
when :obj:`min_score` is used. (default: :obj:`1`)
nonlinearity (torch.nn.functional, optional): The nonlinearity to use.
(default: :obj:`torch.tanh`)
"""
def __init__(self, in_channels: int, ratio: Union[int, float] = 0.5,
min_score: Optional[float] = None, multiplier: float = 1.,
nonlinearity: Callable = torch.tanh):
super().__init__()
self.in_channels = in_channels
self.ratio = ratio
self.min_score = min_score
self.multiplier = multiplier
self.nonlinearity = nonlinearity
self.weight = Parameter(torch.Tensor(1, in_channels))
self.reset_parameters()
def reset_parameters(self):
size = self.in_channels
uniform(size, self.weight)
def forward(self, x, edge_index, edge_attr=None, batch=None, attn=None):
""""""
if batch is None:
batch = edge_index.new_zeros(x.size(0))
attn = x if attn is None else attn
attn = attn.unsqueeze(-1) if attn.dim() == 1 else attn
score = (attn * self.weight).sum(dim=-1)
if self.min_score is None:
score = self.nonlinearity(score / self.weight.norm(p=2, dim=-1))
else:
score = softmax(score, batch)
perm = topk(score, self.ratio, batch, self.min_score)
x = x[perm] * score[perm].view(-1, 1)
x = self.multiplier * x if self.multiplier != 1 else x
batch = batch[perm]
edge_index, edge_attr, edge_mask = filter_adj(edge_index, edge_attr, perm,
num_nodes=score.size(0))
return x, edge_index, edge_attr, batch, perm, edge_mask, score[perm]
def __repr__(self) -> str:
if self.min_score is None:
ratio = f'ratio={self.ratio}'
else:
ratio = f'min_score={self.min_score}'
return (f'{self.__class__.__name__}({self.in_channels}, {ratio}, '
f'multiplier={self.multiplier})')
# Edge pooling
def pool_edge_mean(cluster, edge_index, edge_attr: Optional[torch.Tensor] = None):
num_nodes = cluster.size(0)
edge_index = cluster[edge_index.view(-1)].view(2, -1)
edge_index, edge_attr = remove_self_loops(edge_index, edge_attr)
if edge_index.numel() > 0:
edge_index, edge_attr = coalesce(edge_index, edge_attr, reduce='mean')
return edge_index, edge_attr
# avg pooling
def avg_pool_mod(cluster, x, edge_index, edge_attr, batch, pos):
# Makes cluster indices consecutive, to allow for scatter operations
# -- cluster = [0, 0, 4, 3, 3, 4]
# -- cons_cluster = [0, 0, 2, 1, 1, 2]
cluster, perm = consecutive_cluster(cluster)
# Pool node attributes
# x_pool = None if x is None else _avg_pool_x(cluster, x)
x_pool = None if x is None else scatter(x, cluster, dim=0, dim_size=None, reduce='mean')
# Pool edge attributes
edge_index_pool, edge_attr_pool = pool_edge_mean(cluster, edge_index, edge_attr)
# Pool batch
batch_pool = None if batch is None else batch[perm]
# Pool node positions
pos_pool = None if pos is None else scatter_mean(pos, cluster, dim=0)
return x_pool, edge_index_pool, edge_attr_pool, batch_pool, pos_pool, cluster, perm
def avg_pool_mod_no_x(cluster, edge_index, edge_attr, batch, pos):
# Makes cluster indices consecutive, to allow for scatter operations
# -- cluster = [0, 0, 4, 3, 3, 4]
# -- cons_cluster = [0, 0, 2, 1, 1, 2]
cluster, perm = consecutive_cluster(cluster)
# Pool edge attributes
edge_index_pool, edge_attr_pool = pool_edge_mean(cluster, edge_index, edge_attr)
# Pool batch
batch_pool = None if batch is None else batch[perm]
# Pool node positions
pos_pool = None if pos is None else scatter_mean(pos, cluster, dim=0)
return edge_index_pool, edge_attr_pool, batch_pool, pos_pool, cluster, perm