如何计算 BERT 中多类分类的所有召回准确率精度和 f1 度量?

问题描述

from sklearn.metrics import f1_score

def f1_score_func(preds,labels):
    preds_flat = np.argmax(preds,axis=1).flatten()
    labels_flat = labels.flatten()
    return f1_score(labels_flat,preds_flat,average='weighted')

def accuracy_per_class(preds,labels):
    label_dict_inverse = {v: k for k,v in label_dict.items()}
    
    preds_flat = np.argmax(preds,axis=1).flatten()
    labels_flat = labels.flatten()

    for label in np.unique(labels_flat):
        y_preds = preds_flat[labels_flat==label]
        y_true = labels_flat[labels_flat==label]
        print(f'Class: {label_dict_inverse[label]}')
        print(f'Accuracy: {len(y_preds[y_preds==label])}/{len(y_true)}\n')

需要计算多类模型的分类报告,但它只给出准确率和 f1 分数

解决方法

我想您正在使用 Pytorch 环境。这是打印数据集中每个类的 F1、召回率和精度的正确代码。如果您有经过训练的模型,请加载它以及要测试的数据集。

from sklearn.metrics import classification_report,confusion_matrix

val_dataset = LoadDataset('/content/val.csv')
val_loader = torch.utils.data.DataLoader(val_dataset,batch_size=51) # Load the data

model.load_state_dict(torch.load('vit-base.bin')) # Load the trained model
model.cuda()                                      # For putting model on GPUs
with torch.no_grad():
 image,target = next(iter(val_loader))
 image = image.to(device)
 target = target.flatten().to(device)
 prediction = model(image)

prediction = prediction.argmax(dim=1).view(target.size()).cpu().numpy()
target = target.cpu().numpy()
print(classification_report(target,prediction,target_names=val_dataset.LE.classes_)) # LE is the label encoder