use*_*622 1 python nlp huggingface-transformers
有没有办法使用 返回概率和实际类别Trainer.predict?
我检查了此页面的文档但无法弄清楚。截至目前,它似乎正在返回 logits
显然,概率和实际类别都可以使用额外的编码来计算,但想知道是否有任何预构建的方法可以执行相同的操作
我当前的输出如下
new_predictions=trainer.predict(dataset_for_future_predicition_after_tokenizer)
new_predictions
PredictionOutput(predictions=array([[-0.43005577, 3.646306 , -0.8073783 , -1.0651836 , -1.3480505 ,
-1.108013 ],
[ 3.5415223 , -0.8513837 , -1.8553216 , -0.18011567, -0.35627165,
-1.8364134 ],
[-1.0167522 , -0.8911268 , -1.7115675 , 0.01204597, 1.7177908 ,
1.0401527 ],
[-0.82407415, -0.46043932, -1.089274 , 2.6252217 , 0.33935028,
-1.3623345 ]], dtype=float32), label_ids=None, metrics={'test_runtime': 0.0182, 'test_samples_per_second': 219.931, 'test_steps_per_second': 54.983})
Run Code Online (Sandbox Code Playgroud)
正如您所提到的,Trainer.predict返回模型预测的输出,即逻辑。
pipeline如果您想获得每个类别的不同标签和分数,我建议您根据任务(TextClassification、TokenClassification 等)使用模型对应的标签和分数。该方法的方法上pipeline有一个return_all_scores参数__call__,允许您获取预测中每个标签的所有分数。
这是一个例子:
from transformers import TextClassificationPipeline, AutoTokenizer, AutoModelForSequenceClassification
MODEL_NAME = "..."
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer)
prediction = pipe("The text to predict", return_all_scores=True)
Run Code Online (Sandbox Code Playgroud)
这是该prediction变量的示例:
[{label: 'LABEL1', score: 0.80}, {label: 'LABEL2', score: 0.15}, {label: 'LABEL3', score: 0.05}]
Run Code Online (Sandbox Code Playgroud)
标签名称可以在模型config.json文件中设置,也可以在创建模型时(训练之前)通过定义id2label模型label2id参数来设置:
[{label: 'LABEL1', score: 0.80}, {label: 'LABEL2', score: 0.15}, {label: 'LABEL3', score: 0.05}]
Run Code Online (Sandbox Code Playgroud)