如何使用精度和召回率等指标评估 Pytorch 模型?

How to evaluate Pytorch model using metrics like precision and recall?

我已经在一些数据上训练了一个简单的 Pytorch 神经网络,现在希望使用准确性、召回率、f1 和精度等指标对其进行测试和评估。我彻底搜索了 Pytorch 文档,但找不到这些指标的任何 classes 或函数。然后我尝试将预测标签和实际标签转换为 numpy 数组并使用 scikit-learn 的指标,但预测标签似乎不是 0 或 1(我的标签),而是连续值。由于这个 scikit-learn 指标不起作用。 Fast.ai 文档也没有多大意义,我无法理解要继承哪个 class 以实现精度等(尽管我能够计算精度)。非常感谢任何帮助。

通常,在二元分类设置中,您的神经网络将输出事件发生的概率(例如,如果您在输出层使用 sigmoid 激活和单个神经元),它是 0 之间的连续值和 1. 要评估模型的精度和召回率(例如,使用 scikit-learn 的 precision_scorerecall_score),需要将模型的概率转换为二进制值。这是通过为模型的概率指定阈值来实现的。 (有关阈值的概述,请查看此参考资料:https://developers.google.com/machine-learning/crash-course/classification/thresholding

Scikit-learn 的 precision_recall_curve (https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_curve.html) is commonly used to understand how precision and recall metrics behave for different probability thresholds. By analysing the precision and recall values per threshold, you will be able to specify the best threshold for your problem (you may want higher precision, so you will aim for higher thresholds, e.g., 90%; or you may want to have a balanced precision and recall, and you will need to check the threshold that returns the best f1 score for your problem). A good overview on the topic may be found in the following reference: https://machinelearningmastery.com/threshold-moving-for-imbalanced-classification/

希望对您有所帮助。