Skip to content
Permalink
Browse files

Implement GDC

  • Loading branch information
klicperajo committed Nov 21, 2019
1 parent 2b178a7 commit c80c4e0aa4098286ac8683c7c3cdb3bd112ad6c7
Showing with 512 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 README.md
  3. +1 −0 setup.py
  4. +58 −0 test/transforms/test_gdc.py
  5. +2 −0 torch_geometric/transforms/__init__.py
  6. +449 −0 torch_geometric/transforms/gdc.py
@@ -8,6 +8,7 @@ alpha/
*.egg-info/
.coverage
.coverage.*
.vscode

!torch_geometric/data/
!test/data/
@@ -101,6 +101,7 @@ In detail, the following methods are currently implemented:
* **[RENet](https://pytorch-geometric.readthedocs.io/en/latest/modules/nn.html#torch_geometric.nn.models.RENet)** from Jin *et al.*: [Recurrent Event Network for Reasoning over Temporal Knowledge Graphs](https://arxiv.org/abs/1904.05530) (ICLR-W 2019)
* **[GraphUNet](https://pytorch-geometric.readthedocs.io/en/latest/modules/nn.html#torch_geometric.nn.models.GraphUNet)** from Gao and Ji: [Graph U-Nets](https://arxiv.org/abs/1905.05178) (ICML 2019)
* **[NeighborSampler](https://pytorch-geometric.readthedocs.io/en/latest/modules/data.html#torch_geometric.data.NeighborSampler)** from Hamilton *et al.*: [Inductive Representation Learning on Large Graphs](https://arxiv.org/abs/1706.02216) (NIPS 2017)
* **[GDC](https://pytorch-geometric.readthedocs.io/en/latest/modules/transforms.html#torch_geometric.transforms.gdc)** from Klicpera *et al.*: [Diffusion Improves Graph Learning](https://arxiv.org/abs/1911.05485) (NeurIPS 2019)
--------------------------------------------------------------------------------
@@ -10,6 +10,7 @@
'networkx',
'scikit-learn',
'scikit-image',
'numba',
'requests',
'plyfile',
'pandas',
@@ -0,0 +1,58 @@
import numpy as np
import torch
from torch_geometric.data import Data
from torch_geometric.transforms import GDC
from torch_geometric.utils import to_dense_adj


def test_gdc():
edge_index = torch.tensor([[0, 0, 1, 1, 2, 2, 2, 3, 3, 4],
[1, 2, 0, 2, 0, 1, 3, 2, 4, 3]])

data = Data(edge_index=edge_index, num_nodes=5)
gdc = GDC(self_loop_weight=1,
normalization_in='sym', normalization_out='sym',
diffusion_kwargs=dict(method='ppr', alpha=0.15),
sparsification_kwargs=dict(method='threshold', avg_degree=2),
exact=True)
data = gdc(data)
mat = to_dense_adj(data.edge_index, edge_attr=data.edge_attr).squeeze().numpy()
assert np.all(mat >= 0)
assert np.all(mat == mat.T)

data = Data(edge_index=edge_index, num_nodes=5)
gdc = GDC(self_loop_weight=1,
normalization_in='col', normalization_out='col',
diffusion_kwargs=dict(method='heat', t=5),
sparsification_kwargs=dict(method='topk', k=2),
exact=True)
data = gdc(data)
mat = to_dense_adj(data.edge_index, edge_attr=data.edge_attr).squeeze().numpy()
col_sum = mat.sum(0)
assert np.all(mat >= 0)
assert np.all(np.isclose(col_sum, 1) | np.isclose(col_sum, 0))

data = Data(edge_index=edge_index, num_nodes=5)
gdc = GDC(self_loop_weight=1,
normalization_in='row', normalization_out='row',
diffusion_kwargs=dict(method='coeff', coeffs=[0.8, 0.3, 0.1]),
sparsification_kwargs=dict(method='threshold', eps=0.1),
exact=True)
data = gdc(data)
mat = to_dense_adj(data.edge_index, edge_attr=data.edge_attr).squeeze().numpy()
row_sum = mat.sum(1)
assert np.all(mat >= 0)
assert np.all(np.isclose(row_sum, 1) | np.isclose(row_sum, 0))

data = Data(edge_index=edge_index, num_nodes=5)
gdc = GDC(self_loop_weight=1,
normalization_in='sym', normalization_out='col',
diffusion_kwargs=dict(method='ppr', alpha=0.15, eps=1e-4),
sparsification_kwargs=dict(method='threshold', avg_degree=2),
exact=False)
data = gdc(data)
mat = to_dense_adj(data.edge_index, edge_attr=data.edge_attr).squeeze().numpy()
mat = to_dense_adj(data.edge_index, edge_attr=data.edge_attr).squeeze().numpy()
col_sum = mat.sum(0)
assert np.all(mat >= 0)
assert np.all(np.isclose(col_sum, 1) | np.isclose(col_sum, 0))
@@ -33,6 +33,7 @@
from .generate_mesh_normals import GenerateMeshNormals
from .delaunay import Delaunay
from .to_superpixels import ToSLIC
from .gdc import GDC

__all__ = [
'Compose',
@@ -70,4 +71,5 @@
'GenerateMeshNormals',
'Delaunay',
'ToSLIC',
'GDC'
]

0 comments on commit c80c4e0

Please sign in to comment.
You can’t perform that action at this time.