困惑度(Perplexity)是一种用来衡量序列生成模型性能的指标。在给定一个测试文本集合的情况下,一个好的序列生成模型应该使得测试集合中句子的联合概率尽可能高。困惑度是信息论中的一个概念,用来度量一个分布的不确定性。
对于离散随机变量
,其概率分布为
,困惑度定义如下:
这里的熵
衡量了分布
的不确定性。困惑度可以看作是对观察到的数据集的估计概率的逆。
考虑一个序列长度为 (T) 的测试集,模型的困惑度为:
其中
为测试集中序列的数量,
为第
个序列的长度,
是模型对条件概率的估计。困惑度越低,表示模型在给定数据上的拟合越好。
对于一个未知的数据分布
和一个模型分布
,困惑度可以用来衡量它们之间的差异。两者之间的交叉熵(cross entropy)为:
困惑度可以表示为交叉熵的形式:
困惑度越低,表示模型分布与真实数据分布越接近。
困惑度为每个词条件概率的几何平均数的倒数。测试集中所有序列的概率越大,困惑度越小,模型越好。一般情况下,困惑度范围在50到1000之间。在自然语言处理中,困惑度是一个常用的评估指标,用于衡量语言模型的性能。
BLEU(BiLingual Evaluation Understudy)算法是一种用于衡量机器翻译模型或其他序列生成任务中生成序列和参考序列之间的相似度的评价指标。该算法通过计算N元词组(N-Gram)的重合度来评估生成序列的质量。
设 𝒙 为模型生成的候选序列,
为一组参考序列,𝒲 为从生成的候选序列中提取所有N元组合的集合。BLEU算法的精度(Precision)定义如下:
其中
是N元组合
在生成序列
中出现的次数,
是N元组合
在参考序列
中出现的次数。
为了处理生成序列长度短于参考序列的情况,引入长度惩罚因子
:
其中
是生成序列的长度,
是参考序列的最短长度。
BLEU算法通过计算不同长度的N元组合的精度,并进行几何加权平均,得到最终的BLEU分数:
其中
为最长N元组合的长度,
是不同N元组合的权重,一般设为
。
【深度学习】序列生成模型(五):评价方法计算实例:计算BLEU-N得分【理论到程序】
main_string = 'the cat sat on the mat'
string1 = 'the cat is on the mat'
string2 = 'the bird sat on the bush'
# 计算单词
unique_words = set(main_string.split())
total_occurrences, matching_occurrences = 0, 0
for word in unique_words:
count_main_string = main_string.count(word)
total_occurrences += count_main_string
matching_occurrences += min(count_main_string, max(string1.count(word), string2.count(word)))
similarity_word = matching_occurrences / total_occurrences
print(f"N=1: {similarity_word}")
# 计算双词
word_tokens = main_string.split()
bigrams = set([f"{word_tokens[i]} {word_tokens[i + 1]}" for i in range(len(word_tokens) - 1)])
total_occurrences, matching_occurrences = 0, 0
for bigram in bigrams:
count_main_string = main_string.count(bigram)
total_occurrences += count_main_string
matching_occurrences += min(count_main_string, max(string1.count(bigram), string2.count(bigram)))
similarity_bigram = matching_occurrences / total_occurrences
print(f"N=2: {similarity_bigram}")
ROUGE(Recall-Oriented Understudy for Gisting Evaluation)算法最初被应用于文本摘要领域,类似于BLEU算法,但ROUGE算法关注的是召回率(Recall)。
设
为从模型分布
中生成的一个候选序列,
为从真实数据分布中采样得到的一组参考序列,
为从参考序列中提取N元组合的集合,ROUGE-N算法的定义为:
其中
是N元组合
在生成序列
中出现的次数,
是N元组合
在参考序列
中出现的次数。
【深度学习】序列生成模型(六):评价方法计算实例:计算ROUGE-N得分【理论到程序】
main_string = 'the cat sat on the mat'
string1 = 'the cat is on the mat'
string2 = 'the bird sat on the bush'
words = list(set(string1.split(' ')+string2.split(' '))) # 去除重复元素
total_occurrences, matching_occurrences = 0, 0
for word in words:
matching_occurrences += min(main_string.count(word), string1.count(word)) + min(main_string.count(word), string2.count(word))
total_occurrences += string1.count(word) + string2.count(word)
print(matching_occurrences / total_occurrences)
bigrams = []
split1 = string1.split(' ')
for i in range(len(split1) - 1):
bigrams.append(split1[i] + ' ' + split1[i + 1])
split2 = string2.split(' ')
for i in range(len(split2) - 1):
bigrams.append(split2[i] + ' ' + split2[i + 1])
bigrams = list(set(bigrams)) # 去除重复元素
total_occurrences, matching_occurrences = 0, 0
for bigram in bigrams:
matching_occurrences += min(main_string.count(bigram), string1.count(bigram)) + min(main_string.count(bigram), string2.count(bigram))
total_occurrences += string1.count(bigram) + string2.count(bigram)
print(matching_occurrences / total_occurrences)
在实际应用中,通常会综合使用多个评价指标,以全面评估生成模型的性能。