-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added variance-preserving Aggregation (#9075)
A novel aggregation function as described in [GNN-VPA](https://arxiv.org/pdf/2403.04747.pdf) --------- Co-authored-by: Richard Freinschlag <freinschlag@ml.jku.at> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: rusty1s <matthias.fey@tu-dortmund.de>
- Loading branch information
1 parent
8c070ad
commit 8a9ace7
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import torch | ||
|
||
from torch_geometric.nn import ( | ||
MeanAggregation, | ||
SumAggregation, | ||
VariancePreservingAggregation, | ||
) | ||
|
||
|
||
def test_variance_preserving(): | ||
x = torch.randn(6, 16) | ||
index = torch.tensor([0, 0, 1, 1, 1, 3]) | ||
ptr = torch.tensor([0, 2, 5, 5, 6]) | ||
|
||
vpa_aggr = VariancePreservingAggregation() | ||
mean_aggr = MeanAggregation() | ||
sum_aggr = SumAggregation() | ||
|
||
out_vpa = vpa_aggr(x, index) | ||
out_mean = mean_aggr(x, index) | ||
out_sum = sum_aggr(x, index) | ||
|
||
# Equivalent formulation: | ||
expected = torch.sqrt(out_mean.abs() * out_sum.abs()) * out_sum.sign() | ||
|
||
assert out_vpa.size() == (4, 16) | ||
assert torch.allclose(out_vpa, expected) | ||
assert torch.allclose(out_vpa, vpa_aggr(x, ptr=ptr)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Optional | ||
|
||
from torch import Tensor | ||
|
||
from torch_geometric.nn.aggr import Aggregation | ||
from torch_geometric.utils import degree | ||
from torch_geometric.utils._scatter import broadcast | ||
|
||
|
||
class VariancePreservingAggregation(Aggregation): | ||
r"""Performs the Variance Preserving Aggregation (VPA) from the `"GNN-VPA: | ||
A Variance-Preserving Aggregation Strategy for Graph Neural Networks" | ||
<https://arxiv.org/abs/2403.04747>`_ paper. | ||
.. math:: | ||
\mathrm{vpa}(\mathcal{X}) = \frac{1}{\sqrt{|\mathcal{X}|}} | ||
\sum_{\mathbf{x}_i \in \mathcal{X}} \mathbf{x}_i | ||
""" | ||
def forward(self, x: Tensor, index: Optional[Tensor] = None, | ||
ptr: Optional[Tensor] = None, dim_size: Optional[int] = None, | ||
dim: int = -2) -> Tensor: | ||
|
||
out = self.reduce(x, index, ptr, dim_size, dim, reduce='sum') | ||
|
||
if ptr is not None: | ||
count = ptr.diff().to(out.dtype) | ||
else: | ||
count = degree(index, dim_size, dtype=out.dtype) | ||
|
||
count = count.sqrt().clamp(min=1.0) | ||
count = broadcast(count, ref=out, dim=dim) | ||
|
||
return out / count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters