【问题标题】:What do you need for plotting the outcome of a question-answering model绘制问答模型的结果需要什么
【发布时间】:2020-12-31 13:33:53
【问题描述】:

我一直在研究一个问答模型,通过我的词嵌入模型 BERT 接收问题的答案。但我真的很想绘制这样的图:

但问题是,我真的不知道怎么做。我真的被这个任务困住了。我不知道如何在情节中表示部分上下文。我确实有两个变量,名为 answer_start 和 answer_end ,它们指示模型从上下文中的哪个部分得到答案。有人可以帮我解决这个问题并告诉我我需要在我的 pyplot 中放入哪些变量吗?

在我的代码下面:

from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch
import numpy as np
import pandas as pd

max_seq_length = 512

tokenizer = AutoTokenizer.from_pretrained("henryk/bert-base-multilingual-cased-finetuned-dutch-squad2")
model = AutoModelForQuestionAnswering.from_pretrained("henryk/bert-base-multilingual-cased-finetuned-dutch-squad2")

questions = [
    "Welke soorten gladiatoren waren er?",
    "Wat is een provocator?"
]
for question in questions: # voor elke question moet er door alle lines geiterate worden
    print(f"Question: {question}")
    f = open("test.txt", "r")
    for line in f:
      text = str(line) #het antwoord moet een string zijn
      #encoding met tokenizen van de zinnen
      inputs = tokenizer.encode_plus(question,
                                     text,
                                     add_special_tokens=True,
                                     max_length=max_seq_length,
                                     truncation=True,
                                     return_tensors="pt")
      input_ids = inputs["input_ids"].tolist()[0]

  

      #ff uitzoeken wat die ** deed
      answer_start_scores, answer_end_scores = model(**inputs, return_dict=False)

      answer_start = torch.argmax(
          answer_start_scores
          )  # Het antwoord met de hoogste argmax accuracy vanaf het begin woord
      answer_end = torch.argmax(
          answer_end_scores) + 1  # Zelfde maar dan eind woord
      answer = tokenizer.convert_tokens_to_string(
          tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))

      #om het antwoorden [cls] en NaN te voorkomen    
      if answer == '[CLS]':
        continue
      elif answer == '':
        continue
      else:
        print(f"Answer: {answer}")
        print(f"Answer start: {answer_start}")
        print(f"Answer end: {answer_end}") 
      f.seek(0)
      break          
    # f.seek(0)
    # break
  
f.close()

还有输出:

> Question: Welke soorten gladiatoren waren er?
> Answer: de thraex, de retiarius en de murmillo
> Answer start: 24
> Answer end: 37
> Question: Wat is een provocator?
> Answer: telemachus
> Answer start: 87
> Answer end: 90

【问题讨论】:

    标签: python matplotlib gradient bert-language-model


    【解决方案1】:

    我不知道我是否理解你的问题。但是要制作与该图相似的情节,我会这样做:

    import matplotlib.pyplot as plt; plt.rcdefaults()
    import numpy as np
    import matplotlib.pyplot as plt
    
    sentence = ('list' 'of' 'words' 'that' 'make' 'up' 'the' 'sentence' 'in' 'which' 'the' 'answer' 'is' 'found')
    y_pos = np.arange(len(sentence))
    probability = [0.1, 0.2, 0.1, 0.8, 0.6] 
    
    plt.bar(y_pos, probability, align='center', alpha=0.5)
    plt.xticks(y_pos, sentence)
    plt.ylabel('Answer probability')
    plt.title('Words of the sentence')
    
    plt.show()
    
    

    所以假设答案在一个更大的句子/段落中,我要做的是将句子/段落的所有单词插入条形图的 x 轴(变量sentence - 我想是 text.txt) ,而在 y 轴上,百分比表示特定单词是答案的开头或结尾单词的概率(变量 probability)。显然sentenceprobability这两个变量的长度是一样的,其中第一个句子变量对应第一个概率值,以此类推。

    例如answer_start_scoresanswer_end_scores 将是得分最高的单词,因此它们的条形图“条”将是最高的(概率列表中的最高值)。

    最后,在answer_start_scoresanswer_end_scores 中,你应该得到所有最有可能出现开头和结尾单词的分数。

    编辑: 也许,您还可以为答案的首词和最终词制作两个单独的条形图,然后通过添加百分比将它们连接在一起。

    【讨论】:

      猜你喜欢
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 2021-03-31
      • 2021-11-16
      • 2010-09-15
      • 2020-06-24
      相关资源
      最近更新 更多