File: test_gdc.py

package info (click to toggle)
pytorch-geometric 2.6.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,904 kB
  • sloc: python: 127,155; sh: 338; cpp: 27; makefile: 18; javascript: 16
file content (103 lines) | stat: -rw-r--r-- 3,455 bytes parent folder | download
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
import torch

from torch_geometric.datasets import KarateClub
from torch_geometric.testing import withPackage
from torch_geometric.transforms import GDC
from torch_geometric.utils import to_dense_adj


@withPackage('numba')
def test_gdc():
    data = KarateClub()[0]

    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,
    )
    out = gdc(data)
    mat = to_dense_adj(out.edge_index, edge_attr=out.edge_attr).squeeze()
    assert torch.all(mat >= -1e-8)
    assert torch.allclose(mat, mat.t(), atol=1e-4)

    gdc = GDC(
        self_loop_weight=1,
        normalization_in='sym',
        normalization_out='sym',
        diffusion_kwargs=dict(method='heat', t=10),
        sparsification_kwargs=dict(method='threshold', avg_degree=2),
        exact=True,
    )
    out = gdc(data)
    mat = to_dense_adj(out.edge_index, edge_attr=out.edge_attr).squeeze()
    assert torch.all(mat >= -1e-8)
    assert torch.allclose(mat, mat.t(), atol=1e-4)

    gdc = GDC(
        self_loop_weight=1,
        normalization_in='col',
        normalization_out='col',
        diffusion_kwargs=dict(method='heat', t=10),
        sparsification_kwargs=dict(method='topk', k=2, dim=0),
        exact=True,
    )
    out = gdc(data)
    mat = to_dense_adj(out.edge_index, edge_attr=out.edge_attr).squeeze()
    col_sum = mat.sum(0)
    assert torch.all(mat >= -1e-8)
    assert torch.all(
        torch.isclose(col_sum, torch.tensor(1.0))
        | torch.isclose(col_sum, torch.tensor(0.0)))
    assert torch.all((~torch.isclose(mat, torch.tensor(0.0))).sum(0) == 2)

    gdc = GDC(
        self_loop_weight=1,
        normalization_in='row',
        normalization_out='row',
        diffusion_kwargs=dict(method='heat', t=5),
        sparsification_kwargs=dict(method='topk', k=2, dim=1),
        exact=True,
    )
    out = gdc(data)
    mat = to_dense_adj(out.edge_index, edge_attr=out.edge_attr).squeeze()
    row_sum = mat.sum(1)
    assert torch.all(mat >= -1e-8)
    assert torch.all(
        torch.isclose(row_sum, torch.tensor(1.0))
        | torch.isclose(row_sum, torch.tensor(0.0)))
    assert torch.all((~torch.isclose(mat, torch.tensor(0.0))).sum(1) == 2)

    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,
    )
    out = gdc(data)
    mat = to_dense_adj(out.edge_index, edge_attr=out.edge_attr).squeeze()
    row_sum = mat.sum(1)
    assert torch.all(mat >= -1e-8)
    assert torch.all(
        torch.isclose(row_sum, torch.tensor(1.0))
        | torch.isclose(row_sum, torch.tensor(0.0)))

    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,
    )
    out = gdc(data)
    mat = to_dense_adj(out.edge_index, edge_attr=out.edge_attr).squeeze()
    col_sum = mat.sum(0)
    assert torch.all(mat >= -1e-8)
    assert torch.all(
        torch.isclose(col_sum, torch.tensor(1.0))
        | torch.isclose(col_sum, torch.tensor(0.0)))