Lightning-AI / Lightning-AI/torchmetrics

Refactored Accuracy metric no longer tolerate empty tensor

Open
#2,279 1 comment 0 reactions 1 assignee View on GitHub

@SkafteNicki is already working on this.

Since Aug 29, 2024.

help wanted question v1.2.x
Dominant language
Python
Stars
2.5k
Forks
526
Avg merge
6d 11h
Merged PRs (30d)
5

Description

## 🐛 Bug

In the middle of upgrading from 0.8.1 to 1.2.1, I found `Accuracy` metric no longer takes empty tensor.

### To Reproduce

Same code breaks with 1.2.1:

```py
metric = torchmetrics.Accuracy(
task="multiclass",
num_classes=3,
ignore_index=2,
)
mask = torch.tensor([False, False])
preds = torch.tensor([1, 1])
target = torch.tensor([1, 1])
acc = metric(preds[mask], target[mask])
```

It ends up here: https://github.com/Lightning-AI/torchmetrics/blob/v1.2.1/src/torchmetrics/functional/classification/stat_scores.py#L339

I think this may apply to other metrics as well, like binary accuracy, etc.

Code sample

### Expected behavior

To keep the backward compatibility, IMO this should be a no-op.

### Environment

- TorchMetrics version (and how you installed TM, e.g. `conda`, `pip`, build from source):
- Python & PyTorch Version (e.g., 1.0):
- Any other relevant information such as OS (e.g., Linux):
PyTorch : 2.0
Python: 3.10

### Additional context

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.