给定计算二元组困惑度的公式(以及加 1 平滑的概率),
当句子中单词 per 的预测概率之一为 0 时,如何进行?
# just examples, don't mind the counts
corpus_bigram = {'<s> now': 2, 'now is': 1, 'is as': 6, 'as one': 1, 'one mordant': 1, 'mordant </s>': 5}
word_dict = {'<s>': 2, 'now': 1, 'is': 6, 'as': 1, 'one': 1, 'mordant': 5, '</s>': 5}
test_bigram = {'<s> now': 2, 'now <UNK>': 1, '<UNK> as': 6, 'as </s>': 5}
n = 1 # Add one smoothing
probabilities = {}
for bigram in test_bigram:
if bigram in corpus_bigram:
value = corpus_bigram[bigram]
first_word = bigram.split()[0]
probabilities[bigram] = (value + n) / (word_dict.get(first_word) + (n * len(word_dict)))
else:
probabilities[bigram] = 0
例如,如果出现的概率test_bigram
为
# Again just dummy probability values
probabilities = {{'<s> now': 0.35332322, 'now <UNK>': 0, '<UNK> as': 0, 'as </s>': 0.632782318}}
perplexity = 1
for key in probabilities:
# when probabilities[key] == 0 ????
perplexity = perplexity * (1 / probabilities[key])
N = len(sentence)
perplexity = pow(perplexity, 1 / N)
ZeroDivisionError:除以零