NLP:从问答渠道中选出5名最佳候选人

NLP:从问答渠道中选出5名最佳候选人,nlp,huggingface-transformers,bert-language-model,question-answering,Nlp,Huggingface Transformers,Bert Language Model,Question Answering,我正在使用huggingface transformers库开发一个法语问答模型。我使用的是一个预先训练过的卡门贝尔模型,它与罗伯塔非常相似,但适合法语 目前,我能够使用transformers library的问答管道,在我自己的文本中获得问题的最佳答案候选人 这是我的代码摘录 QA_model = "illuin/camembert-large-fquad" CamTokQA = CamembertTokenizer.from_pretrained(QA_model) C

我正在使用huggingface transformers库开发一个法语问答模型。我使用的是一个预先训练过的卡门贝尔模型,它与罗伯塔非常相似,但适合法语

目前,我能够使用transformers library的问答管道,在我自己的文本中获得问题的最佳答案候选人

这是我的代码摘录

QA_model = "illuin/camembert-large-fquad"
CamTokQA = CamembertTokenizer.from_pretrained(QA_model)
CamQA = CamembertForQuestionAnswering.from_pretrained(QA_model)

device_pipeline = 0 if torch.cuda.is_available() else -1
q_a_pipeline = QuestionAnsweringPipeline(model=CamQA,
                                         tokenizer=CamTokQA,
                                         device=device_pipeline)

ctx = open("text/Sample.txt", "r").read()
question = 'Quel est la taille de la personne ?'
res = q_a_pipeline({'question': question, 'context': ctx})
print(res)
我目前得到的结果是:
{'score':0.9630325870663725,'start':2421,'end':2424,'answer':{21'}
,这是错误的


因此,我想获得答案的5个最佳候选答案。有人知道如何做到这一点吗?

调用管道时,可以通过topk参数指定结果数。例如,对于5个最可能的答案,请执行以下操作:

res = q_a_pipeline({'question': question, 'context': ctx}, topk=5)
这将产生一个字典列表:
[{'score':0.0013586128421753108,'start':885,'end':896,'response':'L'ingénieur',{'score':0.001112090628598246,'start':200,'end':209,'response':'français.},{'score':0.000108080886718235663,'start':164,'end':209,'response':'ingénieur hydraulicien et et essate français',{'score':5.04539705303228015E-05,'start':153,'end':209,'response':'urbaniste,ingénieur hydraulicien et essayiste français.},{'score':4.455333667193265e-05,'start':190,'end':209,'response':'essayiste français.}

当您查看时,您可以看到问答管道接受一个名为topk的参数