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

top-k multiclass macro accuracy is not calculated correctly #2418

Closed
markussteindl opened this issue Feb 29, 2024 · 0 comments · Fixed by #2423
Closed

top-k multiclass macro accuracy is not calculated correctly #2418

markussteindl opened this issue Feb 29, 2024 · 0 comments · Fixed by #2423
Labels
bug / fix Something isn't working help wanted Extra attention is needed

Comments

@markussteindl
Copy link

markussteindl commented Feb 29, 2024

🐛 Bug

If some classes are not observed, the top-k multiclass macro accuracy is not calculated correctly.

To Reproduce

from torchmetrics import Accuracy
import torch

acc1 = Accuracy(task="multiclass", num_classes=3, average="macro")
acc2 = Accuracy(task="multiclass", num_classes=3, average="macro", top_k=2)

preds = torch.tensor([[0.9, 0.1, 0.0], [0.9, 0.1, 0.0], [0.9, 0.1, 0.0]])
targets = torch.tensor([0, 0, 2])

print(acc1(preds, targets))  # tensor(0.5000) --> ok
print(acc2(preds, targets))  # tensor(0.3333)  --> inconsistent

Expected behavior

In the example, the top-1 and top-2 accuracy should be equal.

Note that class 1 does not occur in the targets.
The classes have the following top-1 and top-2 accuracies before averaging:

  • class 0: 1.0
  • class 1: nan
  • class 2: 0.0

Thus, the macro aggregation of the top-1 and top-2 accuracy should be equal with a value of 0.5 (or 0.333, if we take the nan into account).

Environment

  • TorchMetrics version: 1.3.0.post0, 1.3.1
  • Python & PyTorch Version (e.g., 1.0): Python 3.11, torch 2.2.0
  • Windows 10, 22H2, 19045.4046
  • Environment managed by poetry
@markussteindl markussteindl added bug / fix Something isn't working help wanted Extra attention is needed labels Feb 29, 2024
@markussteindl markussteindl changed the title top-k multiclass macro accuracy is not calculated correctly. top-k multiclass macro accuracy is not calculated correctly Mar 1, 2024
@SkafteNicki SkafteNicki mentioned this issue Mar 5, 2024
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant