-
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]
models/test_correct_and_smooth.py
(#6637)
Add tests for `CorrectAndSmooth` Minor documentation fix Also minor fix of `smooth` in `CorrectAndSmooth` to avoid changing the `y_soft` tensor in the `smooth` function --------- Co-authored-by: Jinu Sunil <jinu.sunil@gmail.com>
- Loading branch information
Showing
4 changed files
with
38 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,35 @@ | ||
import torch | ||
from torch_sparse import SparseTensor | ||
|
||
from torch_geometric.nn.models import CorrectAndSmooth | ||
|
||
|
||
def test_correct_and_smooth(): | ||
y_soft = torch.tensor([0.1, 0.5, 0.4]).repeat(6, 1) | ||
y_true = torch.tensor([1, 0, 0, 2, 1, 1]) | ||
edge_index = torch.tensor([[0, 1, 1, 2, 4, 5], [1, 0, 2, 1, 5, 4]]) | ||
adj = SparseTensor.from_edge_index(edge_index, sparse_sizes=(6, 6)) | ||
mask = torch.randint(0, 2, (6, ), dtype=torch.bool) | ||
|
||
model = CorrectAndSmooth(num_correction_layers=2, correction_alpha=0.5, | ||
num_smoothing_layers=2, smoothing_alpha=0.5) | ||
assert str(model) == ('CorrectAndSmooth(\n' | ||
' correct: num_layers=2, alpha=0.5\n' | ||
' smooth: num_layers=2, alpha=0.5\n' | ||
' autoscale=True, scale=1.0\n' | ||
')') | ||
|
||
correct_out = model.correct(y_soft, y_true[mask], mask, edge_index) | ||
assert correct_out.size() == (6, 3) | ||
assert torch.allclose(correct_out, | ||
model.correct(y_soft, y_true[mask], mask, adj)) | ||
smooth_out = model.smooth(y_soft, y_true[mask], mask, edge_index) | ||
assert smooth_out.size() == (6, 3) | ||
assert torch.allclose(smooth_out, | ||
model.smooth(y_soft, y_true[mask], mask, adj)) | ||
|
||
# Test without autoscale: | ||
model = CorrectAndSmooth(num_correction_layers=2, correction_alpha=0.5, | ||
num_smoothing_layers=2, smoothing_alpha=0.5, | ||
autoscale=False) | ||
model.correct(y_soft, y_true[mask], mask, edge_index) |
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