0

给定计算二元组困惑度的公式(以及加 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:除以零

4

1 回答 1

0

常见的解决方案是分配不会出现小概率的单词,例如1/N,其中N是单词的总数。因此,您假装数据中未出现的单词确实出现过一次;这只会引入一个小错误,但会停止除以零。

所以在你的情况下,probabilities[bigram] = 1 / <sum of all bigram frequencies>

于 2021-03-31T16:31:36.390 回答