Question about the intended behavior of Accuracy with average="none" and nan results
#466
-
|
Hi, I'm using your Accuracy metric without averaging and have difficulties with Example with 4 binary labels (cat, dog, horse, cow) and 3 samples: from torchmetrics import Accuracy # version '0.5.0'
accs = Accuracy(average="none", num_classes=4)
probs = torch.tensor([[.9, 0.1, 0.9, 0.1],
[.8, 0.2, 0.9, 0.2],
[.7, 0.1, 0.8 , 0.2]])
targets = torch.tensor([[1, 1, 0, 0],
[1, 1, 0, 0],
[1, 1, 0, 0]])
accs(probs, targets)
#> tensor([1., 0., 0., nan])On your Docs you write that this is intended: "If 'none' and a given class doesn’t occur in the preds or target, the value for the class will be nan." But technically the last class has an Accuracy of Internally you treat the last label with an Accuracy of acc = Accuracy(num_classes=4)
acc(probs, targets)
#> tensor(0.5000)To some extend I understand your thoughts, but I want to ask if this is really the intended result or if an argument like Best |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Hi @phiyodr, |
Beta Was this translation helpful? Give feedback.
-
|
@phiyodr — as @SkafteNicki noted, the NaN behavior for absent classes was a known limitation. In v1.9.0, this is handled via the from torchmetrics.classification import MultilabelAccuracy
acc = MultilabelAccuracy(
num_labels=4,
average="none",
zero_division=0.0, # return 0 instead of NaN for absent classes
)
per_class = acc(preds, target)
print(per_class.min(), per_class.max()) # no NaN issues
from torchmetrics.classification import MultilabelF1Score
f1 = MultilabelF1Score(num_labels=4, average="none", zero_division=0.0)
per_label_f1 = f1(preds, target) # no NaNDocs: MultilabelAccuracy |
Beta Was this translation helpful? Give feedback.
@phiyodr — as @SkafteNicki noted, the NaN behavior for absent classes was a known limitation. In v1.9.0, this is handled via the
zero_divisionparameter:zero_divisionis available on all classification metrics (F1, Precision, Recall, Accuracy, etc.):zero_division=0.0— treat absent classes as score 0 (conservative)zero_division=1.0— treat absent classes as score 1 (optimistic)