繁体   English   中英

如何使用 BERT 预测空字符串的概率

[英]How to predict the probability of an empty string using BERT

假设我们有一个这样的模板语句:

  • “____家是我们的聚会场所。”

我们有一个形容词列表来填补空白,例如:

  • “黄色”
  • “大的”
  • “”

请注意,其中之一是空字符串。

目标是将概率与 select 的概率进行比较,select 是在给定句子上下文的情况下最有可能描述“房子”的词。 , this should also be taken into consideration.如果它更有可能什么都,这也应该考虑在内。

我们可以预测每个单词填空的概率,但是我们如何预测没有形容词来描述“房子”的概率呢?

预测一个单词的概率:

from transformers import BertTokenizer, BertForMaskedLM
import torch
from torch.nn import functional as F

# Load BERT tokenizer and pre-trained model
tokenizer = BertTokenizer.from_pretrained('bert-large-uncased')
model = BertForMaskedLM.from_pretrained('bert-large-uncased', return_dict=True)

targets = ["yellow", "large"]
sentence = "The [MASK] house is our meeting place."

# Using BERT, compute probability over its entire vocabulary, returning logits
input = tokenizer.encode_plus(sentence, return_tensors = "pt") 
mask_index = torch.where(input["input_ids"][0] == tokenizer.mask_token_id)[0] 
with torch.no_grad():
    output = model(**input) 

# Run softmax over the logits to get the probabilities
softmax = F.softmax(output.logits[0], dim=-1)

# Find the words' probabilities in this probability distribution
target_probabilities = {t: softmax[mask_index, tokenizer.vocab[t]].numpy()[0] for t in targets}
target_probabilities

这会输出单词列表及其相关概率:

{'yellow': 0.0061520976, 'large': 0.00071377633}

如果我尝试将空字符串添加到列表中,则会收到以下错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-62-6f726220a108> in <module>
     18 
     19 # Find the words' probabilities in this probability distribution
---> 20 target_probabilities = {t: softmax[mask_index, tokenizer.vocab[t]].numpy()[0] for t in targets}
     21 target_probabilities

<ipython-input-62-6f726220a108> in <dictcomp>(.0)
     18 
     19 # Find the words' probabilities in this probability distribution
---> 20 target_probabilities = {t: softmax[mask_index, tokenizer.vocab[t]].numpy()[0] for t in targets}
     21 target_probabilities

KeyError: ''

这是因为 BERT 的词汇表不包含空字符串,所以我们无法查找 model 中不存在的东西的概率。

我们应该如何获得没有单词可以填空的概率? model 可以做到这一点吗? 使用空标记[PAD]而不是空字符串有意义吗? (我只看到[PAD]用在句子的末尾,使一组句子的长度相同。)

这个问题的一种解决方案是通过添加每个标记的 log-softmax 来比较句子分数。

首先,我应该说,当您对它们使用 softmax 时,BERT 中的 logits 分数并不是真正的概率。 但这似乎是一种可以接受的方法。 所以,我也会使用它。

其次,您还应该考虑形容词有多个标记的情况。 我的解决方案还解决了多个令牌的问题。

这是代码修复:

targets = ["", "yellow", "large", "very large"]
target_log_P = {t: None for t in targets}
for target in target_log_P:
     input = tokenizer.encode_plus(sentence.replace("[MASK]", target), return_tensors = "pt")
     output = model(**input)
     target_log_P[target] = sum([
         torch.log(F.softmax(output.logits[0][i], dim=-1)[idx])
         for i, idx in enumerate(input['input_ids'][0])
     ]).item()

也许有一个管道,我在这里的解决方案不是标准方式,但它似乎工作......

结果如下:

>>> target_log_P
{'': -37.5234375, 'yellow': -37.08171463012695, 'large': -35.85972213745117, 'very large': -46.483154296875}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM