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 normalize mode at confusion matrix (replace nans with zeros) #3465

Merged
merged 9 commits into from
Sep 14, 2020
6 changes: 5 additions & 1 deletion pytorch_lightning/metrics/functional/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ def confusion_matrix(
cm = bins.reshape(num_classes, num_classes).squeeze().float()

if normalize:
cm = cm / cm.sum(-1)
cm = cm / cm.sum(-1, keepdim=True)
nan_elements = cm[torch.isnan(cm)].nelement()
if nan_elements != 0:
cm[torch.isnan(cm)] = 0
rank_zero_warn(f'You have {nan_elements} nan values at confusion matrix replaced with zeros.')

return cm

Expand Down
3 changes: 3 additions & 0 deletions tests/metrics/functional/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ def test_confusion_matrix():
cm = confusion_matrix(pred, target, normalize=False, num_classes=3)
assert torch.allclose(cm, torch.tensor([[5., 0., 0.], [0., 0., 0.], [0., 0., 0.]]))

cm = confusion_matrix(pred, target, normalize=True, num_classes=3)
assert torch.allclose(cm, torch.tensor([[1., 0., 0.], [0., 0., 0.], [0., 0., 0.]]))


@pytest.mark.parametrize(['pred', 'target', 'expected_prec', 'expected_rec'], [
pytest.param(torch.tensor([1., 0., 1., 0.]), torch.tensor([0., 1., 1., 0.]), [0.5, 0.5], [0.5, 0.5]),
Expand Down
5 changes: 3 additions & 2 deletions tests/metrics/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def test_accuracy(num_classes):
@pytest.mark.parametrize(['normalize', 'num_classes'], [
pytest.param(False, None),
pytest.param(True, None),
pytest.param(False, 3)
pytest.param(False, 3),
pytest.param(True, 3)
])
def test_confusion_matrix(normalize, num_classes):
conf_matrix = ConfusionMatrix(normalize=normalize, num_classes=num_classes)
Expand All @@ -50,7 +51,7 @@ def test_confusion_matrix(normalize, num_classes):
target = (torch.arange(120) % 3).view(-1, 1)
pred = target.clone()
cm = conf_matrix(pred, target)
assert isinstance(cm, torch.Tensor)
assert isinstance(cm, torch.Tensor) and cm[torch.isnan(cm)].nelement() == 0


@pytest.mark.parametrize('pos_label', [1, 2.])
Expand Down