Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weighted AUROC on gpu #606

Merged
merged 5 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix empty predictions in MAP metric ([#594](https://github.com/PyTorchLightning/metrics/pull/594))


- Fix edge case of AUROC with `average=weighted` on GPU ([#606](https://github.com/PyTorchLightning/metrics/pull/606))


## [0.6.0] - 2021-10-28

### Added
Expand Down
10 changes: 7 additions & 3 deletions tests/classification/test_auroc.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,15 @@ def test_error_multiclass_no_num_classes():
_ = auroc(torch.randn(20, 3).softmax(dim=-1), torch.randint(3, (20,)))


def test_weighted_with_empty_classes():
@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_weighted_with_empty_classes(device):
"""Tests that weighted multiclass AUROC calculation yields the same results if a new but empty class exists.

Tests that the proper warnings and errors are raised
"""
if not torch.cuda.is_available() and device == "cuda":
pytest.skip("Test requires gpu to run")

preds = torch.tensor(
[
[0.90, 0.05, 0.05],
Expand All @@ -198,8 +202,8 @@ def test_weighted_with_empty_classes():
[0.85, 0.05, 0.10],
[0.10, 0.10, 0.80],
]
)
target = torch.tensor([0, 1, 1, 2, 2])
).to(device)
target = torch.tensor([0, 1, 1, 2, 2]).to(device)
num_classes = 3
_auroc = auroc(preds, target, average="weighted", num_classes=num_classes)

Expand Down
2 changes: 1 addition & 1 deletion torchmetrics/functional/classification/auroc.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _auroc_compute(
raise ValueError("Detected input to `multiclass` but you did not provide `num_classes` argument")
if average == AverageMethod.WEIGHTED and len(torch.unique(target)) < num_classes:
# If one or more classes has 0 observations, we should exclude them, as its weight will be 0
target_bool_mat = torch.zeros((len(target), num_classes), dtype=bool)
target_bool_mat = torch.zeros((len(target), num_classes), dtype=bool, device=target.device)
target_bool_mat[torch.arange(len(target)), target.long()] = 1
class_observed = target_bool_mat.sum(axis=0) > 0
for c in range(num_classes):
Expand Down