如何从管道中获取预测标签和百分比?

How to get prediction label and percentage from pipeline?

我正在使用以下 Hugging Face 转换器代码。

from transformers import pipeline
classifier = pipeline("sentiment-analysis",model='bhadresh-savani/distilbert-base-uncased-emotion', return_all_scores=True)
prediction = classifier("I love using transformers. The best part is wide range of support and its easy to use", )
print(prediction)

结果是:

[[{'label': 'sadness', 'score': 0.010154823772609234}, {'label': 'joy', 'score': 0.5637667179107666}, {'label': 'love', 'score': 0.4066571295261383}, {'label': 'anger', 'score': 0.01734882965683937}, {'label': 'fear', 'score': 0.0011737244203686714}, {'label': 'surprise', 'score': 0.0008987095206975937}]]

我想知道如何获得最高分和标签,但我不确定如何迭代此对象或是否有简单的表达式方法。

您正在使用 TextClassificationPipeline. When you __call__ the pipeline you get a list of dict if return_all_scores=False or a list of list of dict if return_all_scores=True as per the documentation

您可以将 return_all_scores 设置为 False(默认值),然后访问您想要的值 - 在这种情况下,您只会获得最高分标签的分数和文本:

from transformers import pipeline
classifier = pipeline("sentiment-analysis",model='bhadresh-savani/distilbert-base-uncased-emotion', return_all_scores=False)
prediction = classifier("I love using transformers. The best part is wide range of support and its easy to use")
print(prediction[0]["label"], prediction[0]["score"])

或者,如果您想要所有标签的分数,然后只访问得分最高的标签 (return_all_scores=True):

from transformers import pipeline
classifier = pipeline("sentiment-analysis",model='bhadresh-savani/distilbert-base-uncased-emotion', return_all_scores=True)
prediction = classifier("I love using transformers. The best part is wide range of support and its easy to use")
print(max(prediction[0], key=lambda k: k["score"]))