-
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.
[Code Coverage]
transforms/gcn_norm.py
(#6673)
Co-authored-by: wsad1 <jinu.sunil@gmail.com> Co-authored-by: rusty1s <matthias.fey@tu-dortmund.de>
- Loading branch information
1 parent
2a80dae
commit 105c0d8
Showing
3 changed files
with
51 additions
and
5 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,45 @@ | ||
import torch | ||
from torch_sparse import SparseTensor | ||
|
||
from torch_geometric.data import Data | ||
from torch_geometric.transforms import GCNNorm | ||
|
||
|
||
def test_gcn_norm(): | ||
edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]]) | ||
edge_weight = torch.ones(edge_index.size(1)) | ||
adj_t = SparseTensor.from_edge_index(edge_index, edge_weight).t() | ||
|
||
transform = GCNNorm() | ||
assert str(transform) == 'GCNNorm(add_self_loops=True)' | ||
|
||
expected_edge_index = [[0, 1, 1, 2, 0, 1, 2], [1, 0, 2, 1, 0, 1, 2]] | ||
expected_edge_weight = torch.tensor( | ||
[0.4082, 0.4082, 0.4082, 0.4082, 0.5000, 0.3333, 0.5000]) | ||
|
||
data = Data(edge_index=edge_index, edge_weight=edge_weight, num_nodes=3) | ||
data = transform(data) | ||
assert len(data) == 3 | ||
assert data.num_nodes == 3 | ||
assert data.edge_index.tolist() == expected_edge_index | ||
assert torch.allclose(data.edge_weight, expected_edge_weight, atol=1e-4) | ||
|
||
data = Data(edge_index=edge_index, num_nodes=3) | ||
data = transform(data) | ||
assert len(data) == 3 | ||
assert data.num_nodes == 3 | ||
assert data.edge_index.tolist() == expected_edge_index | ||
assert torch.allclose(data.edge_weight, expected_edge_weight, atol=1e-4) | ||
|
||
# For `SparseTensor`, expected outputs will be sorted: | ||
expected_edge_index = [[0, 0, 1, 1, 1, 2, 2], [0, 1, 0, 1, 2, 1, 2]] | ||
expected_edge_weight = torch.tensor( | ||
[0.500, 0.4082, 0.4082, 0.3333, 0.4082, 0.4082, 0.5000]) | ||
|
||
data = Data(adj_t=adj_t) | ||
data = transform(data) | ||
assert len(data) == 1 | ||
row, col, value = data.adj_t.coo() | ||
assert row.tolist() == expected_edge_index[0] | ||
assert col.tolist() == expected_edge_index[1] | ||
assert torch.allclose(value, expected_edge_weight, atol=1e-4) |
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