Search code examples
pytorchmetrics

torchmetrics behaviour for one-hot encoded values


I am having a hard time understanding the following scenario. I have a output probability of 0.0 on each class which means value of metrics such as f1 score, accuracy and recall should be zero? However i get the following:

import torch, torchmetrics
preds = torch.tensor([[0., 0., 0.],
                      [0., 0., 0.],
                      [0., 0., 0.]])

target = torch.tensor([[1, 0, 0],
                       [0, 1, 0],
                       [0, 0, 1]])

print("F1: ", torchmetrics.functional.f1_score(preds, target))
print("Accuracy: ", torchmetrics.functional.accuracy(preds, target))
print("Recall: ", torchmetrics.functional.recall(preds, target))
print("Precision: ", torchmetrics.functional.precision(preds, target))

Output:

F1:  tensor(0.)
Accuracy:  tensor(0.6667)
Recall:  tensor(0.)
Precision:  tensor(0.)

Why is accuracy 0.6667? I would expect all outputs to be 0.0.


Solution

  • Your preds is a probabilities array for multi-label classification problem:

    To make it simpler, I will assume the example like that:

    preds = torch.tensor([[0., 0., 0.]]) # multi-labels [label1, label2, label3]
    
    target = torch.tensor([[1, 0, 0]])
    

    The true negatives are 2 since classifier predicts not existence for label2 and label3 while label2 and label3 indeed should not be existed.

    The true positives are 0 since classifier predicts the existence of any label while a label should be existed.

    The false negative is 1 since classifier predicts no existence for label1 while label1 should be existed.

    The false positives are 0 since classifier predicts any label while a label should not be existed.

    enter image description here

    According to the above equation, Accuracy = 2/3 = 0.6667

    You can read here more about different metrics and their calculations.