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

Incorrect result in computing MulticlassRecall macro average when ignore_index is specified #2441

Open
JeroenMandersloot opened this issue Mar 8, 2024 · 5 comments · Fixed by rittik9/torchmetrics#1 · May be fixed by #2710 or #2443

Comments

@JeroenMandersloot
Copy link

JeroenMandersloot commented Mar 8, 2024

🐛 Bug

Specifying the ignore_index argument in MulticlassRecall leads to incorrect results when computing the macro average.

To Reproduce

import torch
from torchmetrics.classification.precision_recall import MulticlassRecall

metric = MulticlassRecall(num_classes=2, ignore_index=0, average="macro")
y_true = torch.tensor([0, 0, 1, 1])
y_pred = torch.tensor([
    [0.9, 0.1],  # Correct
    [0.9, 0.1],  # Correct
    [0.9, 0.1],  # Incorrect
    [0.1, 0.9],  # Correct
])
metric.update(y_pred, y_true)
print(metric.compute())  # Prints 0.25, but I would expect the result to be 0.5

Expected behavior

In the toy example above there are two classes (0 and 1) and two instances of each class. I want to ignore class 0 altogether (hence ignore_index=0), so I would expect the first two predictions to be ignored entirely (since they relate to class 0 instances). Of the remaining two predictions only one is correct, so I would expect the recall score to be 0.5.

However, the actual result is 0.25. It looks as if the recall score for the ignore_index class is simply set to 0 before computing the average, causing the average to be computed as (0 + 0.5) / 2 instead of 0.5 / 1.

Environment

  • TorchMetrics version: 1.3.1 (installed via pip)
  • Python version: 3.10
  • PyTorch version: 2.2.1
  • OS: Ubuntu 22.04
@JeroenMandersloot JeroenMandersloot added bug / fix Something isn't working help wanted Extra attention is needed labels Mar 8, 2024
Copy link

github-actions bot commented Mar 8, 2024

Hi! thanks for your contribution!, great first issue!

@Borda Borda added the v1.3.x label Mar 8, 2024
@JeroenMandersloot JeroenMandersloot changed the title Error in computing MulticlassRecall macro average when ignore_index is specified Incorrect result in computing MulticlassRecall macro average when ignore_index is specified Mar 8, 2024
@ding3820
Copy link

This issue has been there for a while. After v0.9.3, average="macro" does not work with ignore_index.

@Borda
Copy link
Member

Borda commented Aug 2, 2024

This issue has been there for a while. After v0.9.3, average="macro" does not work with ignore_index.

thank you for the adjustment, would yo be interested in sending a PR?

@Borda Borda added the good first issue Good for newcomers label Aug 29, 2024
@rittik9
Copy link
Contributor

rittik9 commented Aug 29, 2024

Pls assign it to me @Borda

@Borda Borda removed the help wanted Extra attention is needed label Aug 29, 2024
@Borda
Copy link
Member

Borda commented Aug 29, 2024

Pls assign it to me @Borda

cc: @SkafteNicki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment