Lightning-AI / Lightning-AI/torchmetrics
Group fairness metrics can relabel missing group IDs
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.5k
- Forks
- 526
- Avg merge
- 6d 11h
- Merged PRs (30d)
- 5
Description
When a batch does not contain every configured group, `BinaryGroupStatRates` currently compresses the group IDs.
```python
import torch
from torchmetrics.classification import BinaryGroupStatRates
from torchmetrics.functional.classification import binary_groups_stat_rates
preds = torch.tensor([1, 0])
target = torch.tensor([1, 0])
groups = torch.tensor([2, 2])
print(binary_groups_stat_rates(preds, target, groups, num_groups=3))
# {'group_0': tensor([0.5000, 0.0000, 0.5000, 0.0000])}
metric = BinaryGroupStatRates(num_groups=3)
metric.update(preds, target, groups)
print(metric.tp) # tensor([1, 0, 0])
```
The observations belong to group `2`, but they are returned and accumulated as group `0`. Negative and out-of-range IDs are similarly relabeled, and documented multidimensional group tensors fail in `bincount`.
This reproduces on current `master` (`8d008de1`) with Python 3.12.13 and PyTorch 2.13.0. I have a fix and regression tests ready :)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with BinaryGroupStatRates and binary_groups_stat_rates, reproducing the supplied example on the current master revision. Check the handling of missing, negative, out-of-range, and multidimensional group IDs, then run or extend the regression tests so group 2 remains group 2 and documented tensor shapes work without bincount errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100