f1 score is a Model classification metrics. Please refer to that note to decide when to use it.
binary problems
from sklearn.metrics import f1_score
import torch
y_gt = []
y_bin_pred = []
model.eval()
with torch.no_grad():
for X, y in val_dataloader:
pred = model(X)
prob = torch.sigmoid(pred)
# maybe this require .flatten() and .cpu()
y_bin_pred.append(prob > 0.5)
y_gt.append(y)
f1 = f1_score(y_bin_pred, y_gt)
print(f"F1 Score: {f1:.2f}")