PyTorch:pytorchpretrainedBERT mod的随机输出

2024-04-25 05:17:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用“pytorch pretrained BERT”模型来执行Q&A当我尝试使用它时,我意识到,每次评估示例时,输出都是随机的,因此是不正确的。我在处理这个tutorial。你知道吗

text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]"

(...)

questionAnswering_model = torch.hub.load('huggingface/pytorch-pretrained-BERT', 'bertForQuestionAnswering', 'bert-base-cased')
questionAnswering_model.eval()

# Predict the start and end positions logits
with torch.no_grad():
    start_logits, end_logits = questionAnswering_model(tokens_tensor, segments_tensors)

start = np.argmax(start_logits[0])
end = np.argmax(end_logits[0])

answer = tokens_tensor[start:end]

示例:

1: ['Who', 'was', 'Jim', 'He', '##nson', '?', '[SEP]', 'Jim']

2: ['##nson', 'was', 'a', 'puppet']

我是否以正确的方式使用开始和结束登录来获得答案?我能做些什么来纠正随机数?你知道吗

先谢谢你


Tags: 示例modeltorchpytorchhensonstartsepend