简体   繁体   中英

How to find perplexity of bigram if probability of given bigram is 0

Given the formula to calculate the perplexity of a bigram (and probability with add-1 smoothing),

在此处输入图像描述

Probability在此处输入图像描述

How does one proceed when one of the probabilities of the word per in the sentence to predict is 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 

If for instance, the probabilities of the test_bigram come out as

# 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: division by zero

The common solution is to assign words that don't occur a small probability, eg 1/N ,with N being the number of words in total. So you pretend that a word that didn't occur in your data did occur once; that introduces only a minor error, but stops divisions by zero.

So in your case, probabilities[bigram] = 1 / <sum of all bigram frequencies>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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