如何将GPT2用作问答系统(上下文中放什么?)
我尝试用Pytorch实现一个问答系统,使用的是GPT2。我把他们的示例代码复制到一个单独的Python文件里,然后运行。代码是可以工作的,但我得到的答案其实就是我提供的上下文内容。文档里提到我用的pipeline()函数需要指定“上下文”,这样函数才能正常运行。所以基本上,我得提前给系统答案,才能让它回答我的问题。这就没什么用处了。我原本希望能找到一种方法,让模型利用它在其他数据集上学到的知识来生成对我问题的回答。但我找不到任何不依赖于指定上下文的GPT2问答版本。有没有办法在不提前给出答案的情况下,仅根据问题生成答案呢?
或者我是不是应该直接把整个维基百科作为上下文呢?
如果对某些人有帮助,我现在使用的代码是:
from transformers import AutoTokenizer, GPT2ForQuestionAnswering
import torch
from transformers import pipeline
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2-large")
model = GPT2ForQuestionAnswering.from_pretrained("openai-community/gpt2-large")
question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
inputs = tokenizer(question, text, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
answer_start_index = outputs.start_logits.argmax()
answer_end_index = outputs.end_logits.argmax()
predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
# target is "nice puppet"
target_start_index = torch.tensor([14])
target_end_index = torch.tensor([15])
outputs = model(**inputs, start_positions=target_start_index, end_positions=target_end_index)
loss = outputs.loss
question_answerer = pipeline("question-answering", model=model, tokenizer=tokenizer)
question_answerer = question_answerer(question=question, context = text)
print(question_answerer)
然后我得到了
Some weights of GPT2ForQuestionAnswering were not initialized from the model checkpoint at openai-community/gpt2-large and are newly initialized: ['qa_outputs.bias', 'qa_outputs.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
{'score': 0.05718426778912544, 'start': 0, 'end': 28, 'answer': 'Jim Henson was a nice puppet'}
1 个回答
暂无回答