Bug
ClasswiseWrapper converts a metric tensor into a dictionary with zip(self.labels, x). If the number of labels does not match the number of metric values, zip silently truncates the longer input. Duplicate labels also overwrite earlier dictionary entries. Both cases can make class metrics disappear without any error.
Reproduction
import torch
from torchmetrics.classification import MulticlassAccuracy
from torchmetrics.wrappers import ClasswiseWrapper
metric = ClasswiseWrapper(
MulticlassAccuracy(num_classes=3, average=None),
labels=["cat", "dog"],
)
result = metric(
torch.tensor([0, 1, 2]),
torch.tensor([0, 1, 2]),
)
print(result)
The wrapped metric produces three class values, but the result contains only two:
{"multiclassaccuracy_cat": tensor(1.), "multiclassaccuracy_dog": tensor(1.)}
Likewise, labels such as ["cat", "cat", "dog"] produce only two dictionary keys because the second cat overwrites the first.
Expected behavior
Custom labels should define a one-to-one mapping to the wrapped metric values. Mismatched label counts and duplicate labels should raise a clear ValueError instead of silently losing results.
Environment
- TorchMetrics: current
master at e63e92a6
- PyTorch: 2.13.0
- Python: 3.12.13
- macOS
Bug
ClasswiseWrapperconverts a metric tensor into a dictionary withzip(self.labels, x). If the number of labels does not match the number of metric values,zipsilently truncates the longer input. Duplicate labels also overwrite earlier dictionary entries. Both cases can make class metrics disappear without any error.Reproduction
The wrapped metric produces three class values, but the result contains only two:
Likewise, labels such as
["cat", "cat", "dog"]produce only two dictionary keys because the secondcatoverwrites the first.Expected behavior
Custom labels should define a one-to-one mapping to the wrapped metric values. Mismatched label counts and duplicate labels should raise a clear
ValueErrorinstead of silently losing results.Environment
masterate63e92a6