我试着从头算起,算出了bleu的分数。
import numpy as np
reference = 'hello how are you i am good here'
output = 'hello baby are you i am fine here'
# calculate Brevity penalty
BP = 0
if len(reference) < len(output):
BP = 1
else:
BP = np.exp(1-(len(reference)/len(output)))
def Bleu(ref, pred):
count = []
clip_count = []
for i in range(1, len(pred)):
clp = 0
cp = 0
start = set()
for j in range(len(pred)):
if j+i >len(pred):
continue
goal = pred[j:i+j]
sum = ''
for k in goal:
sum += k+' '
final = sum[:-1]
cp += 1
if final in ref:
if final in start:
continue
else:
clp += 1
start.add(final)
clip_count.append(clp)
count.append(cp)
return clip_count, count
clip, count = Bleu(reference, output.split())
pn = sum(np.divide(clip, count))
bleu = np.exp((1/len(clip)) * pn) * BP
print(bleu)nltk python的输出结果: Bleu得分
import nltk
t = 'hello how are you i am good here'
m = 'hello baby are you i am fine here'
hypothesis = m.split()
reference = t.split()
#there may be several references
BLEUscore = nltk.translate.bleu_score.sentence_bleu([reference], hypothesis)
print(BLEUscore)我的问题是:
Q1。两个bleu的分数不匹配,错在哪里??谁来帮帮我
Q2。如果我们尝试计算Bleu分数,则bleu分数的值将始终大于1,因为bleu分数的公式为
Bleu得分-> exp( 1/n *sum(精度n-gram) )* Brevity_Penalty
如果x为+ve,则指数函数(e^x)始终大于1,并且精度n元的值始终为正。
那么为什么一般文献都说bleu的分值应该在0到1之间呢?
发布于 2021-09-16 06:42:45
公式中有一个错误。解释平均n-gram精度不会有任何合理的解释。它应该是一个geometric mean。0和1之间的数字的几何平均值将始终在0和1之间。通常计算它的方法是对对数精度的平均值进行指数运算,否则,您将乘以较小的数字,这可能会导致浮点下溢的错误。
下面是来自original paper的公式

https://stackoverflow.com/questions/69178581
复制相似问题