常见文本生成评价指标——非训练评价指标

1,539 阅读6分钟

最近开始写毕业论文了,我本人就是做文本生成方向的,梳理一下论文相关的一些内容。今天先简单说一下评价指标吧。

在人工智能研究领域,各种任务模型的评估指标一直备受关注,自然语言处理领域也不例外。自然语言生成(NLG)的几种自动评估指标可以简单地分为两大类:非训练评估指标机器学习评估指标

今天就简单介绍一下非训练评估指标。


非训练评估指标,顾名思义就是直接用,不需要额外的训练成本。

这些指标通常被广泛应用于NLG社区,因为它们易于实施且不需要额外的训练成本。它们主要是基于内容重叠、字符串距离或词汇多样性等因素,将机器生成的文本与自然语言进行比较。

BLEU

Bilingual Evaluation Understudy (BLEU) Metric出自Bleu: a Method for Automatic Evaluation of Machine Translation - ACL Anthology

BLEU是用基于匹配的n-gram短语的加权平均来计算两个句子的共现频率。最初用于机器翻译的评估,后来也被广泛用于问题生成、主题到文章生成、文本风格转换和对话生成等各种NLG任务。

最初,BLEU定义为机器生成文本与人类生成文本之间的相似程度:"机器翻译越接近专业人工翻译,质量越高"。

BLEU(Bilingual Evaluation Understudy)的计算方法是统计机器生成的翻译文本与参考文本(可以有多个参考文本)中 N-gram 匹配的数量与机器生成文本中所有 N-gram 总数的比例,也就是 N-gram 的精确度。由于这种评估方法仅考虑了精确度而忽略了召回率,因此它倾向于较短的翻译。因此,BLEU 引入了一个长度惩罚因子,鼓励机器生成的文本中的单词数尽量接近参考文本中的单词数。最终,BLEU 的取值范围在 0 到 1 之间,得分越高表示机器翻译系统的翻译质量越高。

pip install evaluate

import evaluate

predictions = ["It is a guide to action which ensures that the military always obeys the commands of the party."]

references = ["It is a guide to action that ensures that the military will forever heed Party commands."]

bleu = evaluate.load("bleu")

results = bleu.compute(predictions=predictions, references=references)

print(results)

输出:

{'bleu': 0.39670882908365773, 'precisions': [0.631578947368421, 0.4444444444444444, 0.35294117647058826, 0.25], 'brevity_penalty': 1.0, 'length_ratio': 1.1176470588235294, 'translation_length': 19, 'reference_length': 17}

ROUGE

Recall-Oriented Understudy for Gisting Evaluation (ROUGE) Metric:ROUGE用于基于召回分数来衡量生成文本与参考文本之间的相似性。这个指标通常用于文本摘要领域,包括ROUGE-n(测量n-gram共现统计)、ROUGE-l(测量最长公共子序列)、ROUGE-w(测量加权的最长公共子序列)和ROUGE-s(测量跳跃二元共现统计)。ROUGE也广泛应用于其他NLG任务,如问题生成、干扰生成和对话生成。

METEOR

Metric for Evaluation of Translation with Explicit Ordering (METEOR) Metricgoogle-research/rouge at master · google-research/google-research (github.com)

METEOR是对BLEU的改进,旨在解决BLEU的一些弱点,包括召回不足、使用更高阶的n-gram、翻译与参考文本之间缺乏明确的词匹配以及使用n-gram的几何平均值。它通过计算单字精度和召回的调和平均值来得出分数。METEOR除了用于机器翻译,还广泛用于文本摘要、问题生成和对话生成等任务中。

pip install evaluate

pip install rouge_score

import evaluate

predictions = ["It is a guide to action which ensures that the military always obeys the commands of the party."]

references = ["It is a guide to action that ensures that the military will forever heed Party commands."]

rouge = evaluate.load('rouge')

results = rouge.compute(predictions=predictions, references=references)

print(results)

输出:

{'rouge1': 0.7058823529411765, 'rouge2': 0.5, 'rougeL': 0.6470588235294118, 'rougeLsum': 0.6470588235294118}

输出的四个结果分别是:

  • "rouge1":基于单字(1-gram)的评分。

  • "rouge2":基于双字(2-gram)的评分。

  • "rougeL":基于最长公共子序列的评分。

  • "rougeLSum":使用 "\n" 拆分文本的评分。

Distinct

Distinct Metric:Distinct用于衡量对话生成中生成文本序列的多样性。它计算生成文本中不同单词和二元组的数量,以反映多样性程度。为避免偏好较长的序列,该值还会根据生成的tokens总数进行缩放。

原始论文A Diversity-Promoting Objective Function for Neural Conversation Models - ACL Anthology将其用于语料库级别。原始的Distinct存在一个致命问题是它对句子长度和数量敏感。因此Rethinking and Refining the Distinct Metric - ACL Anthology对其改进提出Expectation Adjusted Distinct。

image.png

对于这个评价指标,huggingface提供了两种计算方式:

需要传入的参数列表如下:

  • predictions(字符串列表):要测试多样性的句子列表。每个预测应为一个字符串。
  • mode(字符串):“Expectation-Adjusted-Distinct”或“Distinct”,用于多样性计算。如果选择“Expectation-Adjusted-Distinct”,则会返回两种模式的分数。默认值为“Expectation-Adjusted-Distinct”。
  • vocab_size(整数):用于计算“Expectation-Adjusted-Distinct”的词汇量大小。要么提供vocab_size,要么提供dataForVocabCal中的数据。默认值为None。
  • dataForVocabCal(字符串列表):用于计算“Expectation-Adjusted-Distinct”的vocab_size的数据。通常应该是包含任务数据集的句子列表。要么提供vocab_size,要么提供dataForVocabCal中的数据。默认值为None。

解释一下,这个函数默认是计算Expectation Adjusted Distinct,如果你要使用Expectation Adjusted Distinct,你需要提供vocab_size(词汇量的大小)或者dataForVocabCal(整个语料库中句子的列表)。

Expectation Adjusted Distinct 计算如下:.

pip install evaluate

import evaluate

# 加载Distinct模型
distinct = evaluate.load("lsy641/distinct")

# 示例1:计算分数,提供了预测句子和vocab_size
results1 = distinct.compute(predictions=["Hi.", "I am sorry to hear that", "I don't know", "Do you know who that person is?"], vocab_size=50257)

# 打印结果
print(results1)

# 示例2:计算分数,提供了dataForVocabCal用于vocab_size的计算
dataset = ["This is my friend jack", "I'm sorry to hear that", "But you know I am the one who always support you", "Welcome to our family","Hi.", "I am sorry to hear that", "I don't know", "Do you know who that person is?"]
results2 = distinct.compute(predictions=["But you know I am the one who always support you", "Hi.", "I am sorry to hear that", "I don't know", "I'm sorry to hear that"], dataForVocabCal=dataset)

# 打印结果
print(results2)

输出:

{'Expectation-Adjusted-Distinct': 0.8422560769738913, 'Distinct-1': 0.8421052631578947, 'Distinct-2': 0.9473684210526315, 'Distinct-3': 0.9473684210526315}

{'Expectation-Adjusted-Distinct': 1.0137389719597278, 'Distinct-1': 0.6666666666666666, 'Distinct-2': 0.8148148148148148, 'Distinct-3': 0.8888888888888888}

Self-BLEU

Self-BLEU Metric出自[1802.01886] Texygen: A Benchmarking Platform for Text Generation Models (arxiv.org)

Self-BLEU也是用于衡量多样性的指标。它利用了机器翻译中常用的BLEU指标,但是不是将生成文本与参考文本进行比较,而是将生成文本与其他生成文本进行比较。 Self-BLEU的计算方法是,对于每个生成文本,计算它与其他生成文本的BLEU分数,然后对所有生成文本的BLEU分数取平均值。 Self-BLEU的取值范围在0到1之间,分数越低表示生成文本的多样性越高,分数越高表示生成文本的相似性越高。 Self-BLEU常用于无条件语言生成、对话生成、文本摘要等任务中。

hugging face没现成的evaluate可用,这里就不展示代码了。