使用 GPT2 时,我们可以简单地传递 'labels' 参数来获得损失,如下所示:
import torch
from transformers import GPT2Tokenizer, GPT2LMHeadModel
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2', return_dict=True)
inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
outputs = model(**inputs, labels=inputs["input_ids"])
loss = outputs.loss
但是,无法找出如何在 ONNX 推理会话中获得相同的损失。我正在使用下面的代码,它只返回“last_hidden_state”:
import onnxruntime as ort
from transformers import GPT2TokenizerFast
#tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
ort_session = ort.InferenceSession("onnx/gpt2/model.onnx")
inputs = tokenizer("Using BERT in ONNX!", return_tensors="np")
outputs = ort_session.run(["last_hidden_state"], dict(inputs))