如何解决如果给定二元组的概率为 0,如何找到二元组的困惑度
给定计算二元组困惑度的公式(以及加1平滑的概率),
当要预测的句子中单词 per 的概率之一为 0 时,如何进行?
With qry1 As (
SELECT Cust_Id,DATEDIFF(year,BirthDate,GETDATE()) As Age
FROM Customers
WHERE BirthDate is not null
),qry2 As (
SELECT Max(Age) As Max_Age
FROM qry1
)
SELECT Customers.Cust_Id,Customers.Prefix,Customers.FirstName,Customers.MiddleName,Customers.LastName,Customers.Suffix,Qry1.Age
FROM Customers
Inner Join Qry1 On Customers.Cust_Id = Qry1.Cust_Id
Inner Join Qry2 On Qry1.Age = Qry2.Max_Age
例如,如果 # 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
ZeroDivisionError: 除以零
解决方法
常见的解决方案是分配不出现小概率的单词,例如1/N,N 是单词总数。所以你假装一个没有出现在你的数据中的词确实出现过一次;这只会引入一个小错误,但会停止除以零。
所以在你的情况下,probabilities[bigram] = 1 / <sum of all bigram frequencies>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。