首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果给定双图的概率为0,如何寻找双图的困惑

如果给定双图的概率为0,如何寻找双图的困惑
EN

Stack Overflow用户
提问于 2021-03-31 14:55:21
回答 1查看 739关注 0票数 0

给出计算双图的困惑性的公式(加-1平滑的概率),

概率

当句子中每一个单词预测的概率之一是0时,如何进行?

代码语言:javascript
复制
# 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的概率是

代码语言:javascript
复制
# 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:除以零

EN

回答 1

Stack Overflow用户

发布于 2021-03-31 16:31:36

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

所以在你的例子中,probabilities[bigram] = 1 / <sum of all bigram frequencies>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66890249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档