简体   繁体   English

如何在 SageMaker Inference 中返回所有标签和分数?

[英]How to return all labels and scores in SageMaker Inference?

I am using the HuggingFacePredictor from sagemaker.huggingface to inference some text and I would like to get all label scores.我正在使用HuggingFacePredictor中的sagemaker.huggingface来推断一些文本,我想获得所有 label 分数。

Is there any way of getting, as response from the endpoint:作为端点的响应,有什么方法可以获取:

{
    "labels": ["help", "Greeting", "Farewell"] ,
    "score": [0.81, 0.1, 0.09],
}

(or similar) (或类似的)

Instead of:代替:

{
    "label": "help",
    "score": 0.81,
}

Here is some example code:这是一些示例代码:

import boto3

from sagemaker.huggingface import HuggingFacePredictor
from sagemaker.session import Session

sagemaker_session = Session(boto_session=boto3.session.Session())

predictor = HuggingFacePredictor(
    endpoint_name=project, sagemaker_session=sagemaker_session
)
prediciton = predictor.predict({"inputs": text})[0]

With your current code sample, it is not quite clear what specific task you are performing, but for the sake of this answer, I'll assume you're doing text classification.对于您当前的代码示例,尚不清楚您正在执行什么特定任务,但为了这个答案,我假设您正在执行文本分类。

Most importantly, though, we can read the following in Huggingface's Sagemaker reference document (bold highlight by me):不过,最重要的是,我们可以在Huggingface 的 Sagemaker 参考文档中阅读以下内容(我的粗体突出显示):

The Inference Toolkit accepts inputs in the inputs key, and supports additional pipelines parameters in the parameters key .推理工具包接受输入键中的输入,并支持parameters键中的其他pipelines参数 You can provide any of the supported kwargs from pipelines as parameters.您可以从pipelines中提供任何受支持的kwargs作为参数。

If we check out the accepted arguments by the TextClassificationPipeline , we can see that there is indeed one that returns all samples:如果我们检查TextClassificationPipeline 接受的TextClassificationPipeline ,我们可以看到确实有一个返回所有样本:

return_all_scores (bool, optional, defaults to False) — Whether to return scores for all labels. return_all_scores (bool, optional, 默认为 False) — 是否返回所有标签的分数。

While I unfortunately don't have access to Sagemaker inference, I can run a sample to illustrate the output with a local pipeline:虽然不幸的是我无法访问 Sagemaker 推理,但我可以运行一个示例来说明具有本地管道的 output:

from transformers import pipeline
# uses 2-way sentiment classification model per default
pipe = pipeline("text-classification") 

pipe("I am really angry right now >:(", return_all_scores=True)
# Output: [[{'label': 'NEGATIVE', 'score': 0.9989138841629028},
#           {'label': 'POSITIVE', 'score': 0.0010860705515369773}]]

Based on the slightly different input format expected by Sagemaker, coupled with the example given in this notebook , I would assume that a corrected input in your own example code should look like this:基于 Sagemaker 所期望的略有不同的输入格式,再加上本笔记本中给出的示例,我假设您自己的示例代码中的更正输入应如下所示:

{
    "inputs": text,
    "parameters": {"return_all_scores": True}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM