How to use F1 score and Accuracy for binary classification? #1087
-
|
Hi! I'm having some doubts about a task that I'm facing. I'm doing an image binary classification and I have a question about the metrics. In my task I have two classes that I defined as 0 and 1. Initially, I used the torchmetrics modules with the Now, I changed the metrics' code in this way: self.train_accuracy = torchmetrics.Accuracy(num_classes=1, average='macro', multiclass=False)
self.test_accuracy = torchmetrics.Accuracy(num_classes=1, average='macro', multiclass=False)
self.val_accuracy = torchmetrics.Accuracy(num_classes=1, average="macro", multiclass=False)
self.val_MCC = torchmetrics.MatthewsCorrCoef(num_classes=2, multiclass=False)
self.train_MCC = torchmetrics.MatthewsCorrCoef(num_classes=2, multiclass=False)
self.test_MCC = torchmetrics.MatthewsCorrCoef(num_classes=2, multiclass=False)
self.train_recall = torchmetrics.Recall(num_classes=1, average='macro', multiclass=False)
self.test_recall = torchmetrics.Recall(num_classes=1, average='macro', multiclass=False)
self.train_f1 = torchmetrics.F1Score(num_classes=1, average='macro', multiclass=False)
self.test_f1 = torchmetrics.F1Score(num_classes=1, average='macro', multiclass=False)and even in this case I have always the same I'm confused about the Just another information: in the training (and validation) step what I do is the following: x, y = batch
y_hat = self(x)
loss = F.cross_entropy(y_hat, y)
output = torch.argmax(y_hat, dim=1)and then I simply pass the output to the metrics with Thank you, have a nice day! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
And where do you call the metrics? |
Beta Was this translation helpful? Give feedback.
-
|
@aedoardo — the reason F1 and Accuracy gave identical values is that with The fix with today's API (v1.9.0): from torchmetrics.classification import BinaryF1Score, BinaryAccuracy
from torchmetrics import MetricCollection
metrics = MetricCollection({
"acc": BinaryAccuracy(),
"f1": BinaryF1Score(),
})
# In your step:
y_hat = self(x) # (N, 2) logits
loss = F.cross_entropy(y_hat, y)
probs = y_hat.softmax(dim=1)[:, 1] # probability of positive class
metric_dict = metrics(probs, y)
self.log_dict(metric_dict)Key points:
With this setup, F1 and Accuracy will correctly differ. Docs: BinaryF1Score | BinaryAccuracy |
Beta Was this translation helpful? Give feedback.
@aedoardo — the reason F1 and Accuracy gave identical values is that with
torch.argmaxon binary outputs +average="macro"+ a balanced dataset, they can mathematically converge. But the deeper issue was the old API's confusingnum_classes/multiclassinteraction.The fix with today's API (v1.9.0):