机器翻译评价指标BLEU介绍
最近需要设计一个机器翻译相关的试验, 其中好多东西都不同, 先从基础的评价指标来吧. 本文翻译自Jason Brownlee的博客
[1]
. 可能会简化一部分内容, 如有需要请读者直接读原文.
0. 前言
BLEU (其全称为Bilingual Evaluation Understudy), 其意思是双语评估替补。所谓Understudy (替补),意思是代替人进行翻译结果的评估。尽管这项指标是为翻译而发明的,但它可以用于评估一组自然语言处理任务生成的文本。
本教程中, 你将会学会使用Python中的NLTK库来对待评估的文本求BLEU得分. 在本教程结束之后, 你应该知道:
- 对BLEU的简单介绍以及其计算方式的直觉介绍.
- 如何使用Python中的NLTK库来计算BLEU值.
- 利用 待选文本(candidate text) 和 参考文本(reference text) 的差异来分析其对BLEU值的影响.
让我们开始吧~
1. 概述
本教程分为4个部分:
- ① BLEU
- ② 计算BLEU的得分
- ③ 累积和单独的BLEU分数 (Cumulative and Individual BLEU Scores)
- ④ 例子
2. Bilingual Evaluation Understudy: BLEU
在自然语言处理中的机器翻译任务中, BLEU非常常见, 它是用于评估模型生成的句子(candidate)和实际句子(reference)的差异的指标.
它的取值范围在0.0到1.0之间, 如果两个句子完美匹配(perfect match), 那么BLEU是1.0, 反之, 如果两个句子完美不匹配(perfect mismatch), 那么BLEU为0.0.
虽然这个指标不够完美, 但是它有5个非常引人注目的好处(compelling benefits):
- 计算代价小, 快.
- 容易理解.
- 与语言无关(这意味着你可以使用全世界任意的语言来测试).
- 与人类评价结果高度相关.
- 被学术界和工业界广泛采用.
BLEU值是2002年由IBM 科学家 Kishore Papineni在其论文[2]
中提出的: “BLEU: a Method for Automatic Evaluation of Machine Translation“.
BLEU方法的实现是分别计算candidate句和reference句的N-grams模型[3]
, 然后统计其匹配的个数来计算得到的. 显然, 这种比较方法, 是与语序无关的.
论文对匹配的 N-grams 计数进行了修改,以确保它考虑到reference文本中单词的出现,而非奖励生成大量合理翻译单词的候选结果。本文将其称为修正的 N-grams 精度。
此外, 有一种改进版的通过normalize N-grams的改进版BLEU. 其目的是提升对多个句子组成的**块(block)**提升翻译效果.
在实践中得到完美的分数是不太可能的,因为翻译结果必须与reference句完全匹配。甚至人工翻译都不可能做到这一点。而且, 由于不同的数据集reference句的质量和数量不同, 所以跨数据集计算BLEU值可能带来一些麻烦.
对机器翻译来说, 我们可以使用BLEU得分来评价其它的语言生成任务, 比如:
- 语言生成.
- 图像标题生成.
- 文本总结.
- 语音识别.
及其它.
3. 计算BLEU
得分
Python自然语言工具包库: NLTK
, 提供了BLEU得分计算的实现, 所以如果你只是像译者一样使用的话, 可以先不去了解其内部的实现.
3.1 句子的BLEU值
NLTK提供了sentence_bleu()
来评估一组candidate句和reference句的BLEU得分情况. 其中, 这两个句子都必须用一系列tokens表示:
注: 这个reference的形式有点像图像检索里面的图像gallery.
>>> from nltk.translate.bleu_score import sentence_bleu
>>> reference = [['this', 'is', 'a', 'test'], ['this', 'is' 'test']]
>>> candidate = ['this', 'is', 'a', 'test']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)
1.0
这个例子的结果为1.0, 因为candidate与reference的第1个句子perfect match了.
3.2 Corpus(语料库)的BLEU值
NLTK同样提供了corpus_bleu()
函数, 它用于来对多个句子比如一个段落甚至一篇文章进行得分评价.
参考句必须由一系列documents组成, 每个document都应该由一系列references组成, 同样的, 每个句子都应该拆分为一个个token. 如下, 其实比上一部分多了一个维度.
可能上面的解释还是容易让人困惑, 下面是一篇文档的2个参考句的示例(我的理解: 比Sentence的都多加了一个维度):
# two references for one document
>>> from nltk.translate.bleu_score import corpus_bleu
>>> references = [[['this', 'is', 'a', 'test'], ['this', 'is' 'test']]]
>>> candidates = [['this', 'is', 'a', 'test']]
>>> score = corpus_bleu(references, candidates)
>>> print(score)1.0
4. 累积BLEU
和独立BLEU
在NLTK中, 其允许用户显式指定不同的N-grams的权重以便来计算BLEU的值. 这使得用户可以灵活的计算不同类型的BLEU值, 比如独立的BLEU或者累积的BLEU.
4.1 独立N-Gram
得分
一个独立的N-gram
得分是对gram是否按特定顺序排列的评估方式, 比如1-gram和2-gram (2-gram又成为bigram)[3]
:
# 1-gram individual BLEU
>>> from nltk.translate.bleu_score import sentence_bleu
>>> reference = [['this', 'is', 'small', 'test']]
>>> candidate = ['this', 'is', 'a', 'test']
>>> score = sentence_bleu(reference, candidate, weights=(1, 0, 0, 0))
>>> print(score)0.75>>> candidate = ['this', 'test', 'is', 'a']
>>> score = sentence_bleu(reference, candidate, weights=(1, 0, 0, 0))
>>> score
0.75
可以看到, 1-gram有3/4个词完全对应上, 所以得分为0.75, 那么如果我们试试把candidate的顺序打乱呢? 结果仍为0.75~
下面, 作者测试了1-4gram如下:
>>> from nltk.translate.bleu_score import sentence_bleu
>>> reference = [['this', 'is', 'a', 'test']]
>>> candidate = ['this', 'is', 'a', 'test']
>>> print('Individual 1-gram: %f' % sentence_bleu(reference, candidate, weights=(1, 0, 0, 0)))
>>> print('Individual 2-gram: %f' % sentence_bleu(reference, candidate, weights=(0, 1, 0, 0)))
>>> print('Individual 3-gram: %f' % sentence_bleu(reference, candidate, weights=(0, 0, 1, 0)))
>>> print('Individual 4-gram: %f' % sentence_bleu(reference, candidate, weights=(0, 0, 0, 1)))Individual 1-gram: 1.000000
Individual 2-gram: 1.000000
Individual 3-gram: 1.000000
Individual 4-gram: 1.000000
尽管我们可以计算独立的BLEU值, 但是这种方式不是常规使用的方式, 而且独立BLEU值的含义也比较有限. 因此, 下面我们引出累积BLEU值.
4.2 累积N-Gram
得分
累积N-Gram
得分指的是为各个gram对应的权重加权, 来计算得到一个加权几何平均(weighted geometric mean). 默认情况下, sentence_bleu()
和corpus_bleu()
都是计算累积的4-gram BLEU分数的, 也称之为BLEU-4.
BLEU-4的加权策略如下: 1/4 (25%) 或者 0.25 对 1-gram, 2-gram, 3-gram and 4-gram 的得分.
# 4-gram cumulative BLEU
>>> from nltk.translate.bleu_score import sentence_bleu
>>> reference = [['this', 'is', 'small', 'test']]
>>> candidate = ['this', 'is', 'a', 'test']
>>> score = sentence_bleu(reference, candidate)
>>> score
1.0547686614863434e-154
>>> score = sentence_bleu(reference, candidate, weights=(0.25, 0.25, 0.25, 0.25))
>>> score
1.0547686614863434e-154
下面, 我们分别计算从BLEU-1到BLEU-4的累积N-Gram
得分:
>>> print('Cumulative 1-gram: %f' % sentence_bleu(reference, candidate, weights=(1, 0, 0, 0)))
Cumulative 1-gram: 0.750000
>>> print('Cumulative 2-gram: %f' % sentence_bleu(reference, candidate, weights=(0.5, 0.5, 0, 0)))
Cumulative 2-gram: 0.500000
>>> print('Cumulative 3-gram: %f' % sentence_bleu(reference, candidate, weights=(0.33, 0.33, 0.33, 0)))
Cumulative 3-gram: 0.000000
>>> print('Cumulative 4-gram: %f' % sentence_bleu(reference, candidate, weights=(0.25, 0.25, 0.25, 0.25)))
Cumulative 4-gram: 0.000000
通常情况下, 我们会统计BLEU1
到BLEU4
的累积值作为评估 文本生成系统(text generation system) 的效果.
5. 样例
本部分, 我们将对BLEU值的设计develop further intuition(通过一些例子), 以一个单句的例子为例:
the quick brown fox jumped over the lazy dog
首先, 让我们看看完美匹配的得分:
# prefect match
>>> from nltk.translate.bleu_score import sentence_bleu
>>> reference = [['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']]
>>> candidate = ['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)1.0
接下来, 我们把quick
同意替换为fast
, 看看效果:
# one word different
...
>>> candidate = ['the', 'fast', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)0.7506238537503395
接着在此基础上再改一个单词, 把lazy
改成 sleepy
# two words different
...
>>> candidate = ['the', 'fast', 'brown', 'fox', 'jumped', 'over', 'the', 'sleepy', 'dog']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)0.4854917717073234
可以看到, BLEU值出现了下降, 因为match的对越来越少了. 现在, 我们可以试试这种形式: 将candidate句变短, 再跟reference进行比较
# 删除candidate句子最后两个单词, 即让candidate变短
...
>>> candidate = ['the', 'fast', 'brown', 'fox', 'jumped', 'over', 'the']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)0.7514772930752859
那么, 如果 将candidate句变长, 再跟reference进行比较呢?
...
>>> candidate = ['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog', 'from', 'space']
>>> score = sentence_bleu(reference, candidate)
>>> print(score)0.7860753021519787
我们可以看到, 分数反而越来越高啦~, BLEU中的数学理论[2, 4]
是相当简单的,我也鼓励你阅读这篇论文,并探索自己在电子表格中计算句子水平分数。
6. 总结
在本教程中, 你会了解到BLEU的使用和得到一些基本的intuition关于其设计思路, 通过学习, 如下3点你应该掌握:
- ① A gentle introduction to the BLEU score and an intuition for what is being calculated.
- ② How you can calculate BLEU scores in Python using the NLTK library for sentences and documents.
- ③ How to can use a suite of small examples to develop an intuition for how differences between a candidate and reference text impact the final BLEU score.
参考资料
[1] A Gentle Introduction to Calculating the BLEU Score for Text in Python
[2] BLEU: a Method for Automatic Evaluation of Machine Translation
[3] 蕉叉熵: N-grams说明
[4] BLEU 维基百科
机器翻译评价指标BLEU介绍相关推荐
- 机器翻译评价指标之BLEU原理介绍及代码实现
欢迎关注知乎: 世界是我改变的 知乎上的原文链接 一. 原理介绍 BLEU(Bilingual Evaluation Understudy),即双语评估替补.所谓替补就是代替人类来评估机器翻译的每一个 ...
- 机器翻译, 文本生成等任务评价指标 BLEU, ROUGE, PPL(困惑度)
一:BLEU 1.1 BLEU 的全称是 Bilingual evaluation understudy,BLEU 的分数取值范围是 0-1,分数越接近1,说明翻译的质量越高.BLEU 主要是基于 ...
- 机器翻译评价指标之BLEU详细计算过程
简介 BLEU(Bilingual Evaluation Understudy),相信大家对这个评价指标的概念已经很熟悉,随便百度谷歌就有相关介绍.原论文为BLEU: a Method for Aut ...
- 机器翻译之BLEU值
1. 简介 BLEU(Bilingual Evaluation Understudy),相信大家对这个评价指标的概念已经很熟悉,随便百度谷歌就有相关介绍.原论文为BLEU: a Method for ...
- 机器翻译评测——BLEU算法详解
BLEU算法介绍和如何计算: 我们先举一个例子来说明: 原文:猫站在地上 机器译文 (candidate):the the the the 人工译文 (reference):The cat is st ...
- Rouge | 自动文摘及机器翻译评价指标
tag:评价指标,摘要,nlp Rouge(Recall-Oriented Understudy for Gisting Evaluation),是评估自动文摘以及机器翻译的一组指标.它通过将自动生成 ...
- NMT评价指标-BLEU
前言 BLEU (其全称为Bilingual Evaluation Understudy), 其意思是双语评估替补.所谓Understudy (替补),意思是代替人进行翻译结果的评估.尽管这项指标是为 ...
- 人工智能领域,符号计算、模式识别、专家系统和机器翻译的基本介绍
人工智能是近年来引起人们很大兴趣的一个研究领域:它的研究目标是用机器,通常为电子仪器.电脑等,尽可能地模拟人的精神活动,并且争取在这些方面最终改善并超出人的能力:其研究领域及应用范围十分广泛.例如,自 ...
- 【机器翻译】BLEU学习
BLEU学习 简介 BLEU(bilingual evaluation understudy),是一种翻译结果的评估方法,主要概念来自于这篇Bleu: a method for automatic e ...
- 语义分割之评价指标MIoU介绍
预备知识 我们在进行语义分割结果评价的时候,常常将预测出来的结果分为四个部分:True Positive,False Positive,True Negative,False Negative,其中n ...
最新文章
- FFmpeg中RTSP客户端拉流测试代码
- php 图像函数,PHP图像操作常用函数
- es6之扩展运算符...
- mybatis的优缺点
- Kafka: Connect
- Vue3、TypeScript 实现图片数量及大小随宽度自适应调整
- mybatis中LIKE模糊查询的几种写法以及注意点
- 路飞学城Python-Day108
- 数学建模练习-----基于无线通信基站的室内定位问题
- 空气质量模型:操作指南与案例研究(模型概述)
- 【原创】自定义分页控件WPF
- C# 清除cookies
- DMP 数据管理平台极简教程 ( Data Management Platform )
- 浅谈JavaScript、ES5、ES6 ,,转自http://www.cnblogs.com/lovesong/p/4908871.html
- usb 启动盘制作的几种办法
- 10进制转换16进制C代码实现
- 为什么只有状态转移算法才是真正意义上的智能优化算法,其它的都是“假冒伪劣”?
- easyswoole和mixphp_关于thinkphp5和swoole通过SMTP方式实现异步邮件群发的详解 - easyswoole...
- Vue.js读取本地json文件并分页显示
- 编码电机测速(stm32f1/HAL库/CubeMX/编码器模式/平衡小车/直流减速电机/超详细)
热门文章
- 2018.8.2课堂笔记
- 爬虫小练(刷访问量)(python+requests(headers+proxy)+Queue+threading)
- PayPal 支付接口详解
- 微信小游戏上线发布全流程详解
- 第三方app版本更新 自定义接口协议+自定义对话框+显示进度对话框
- GameFi市值飙升,详解N.Fans目前的发展现状以及未来前景
- 微软所有正版产品下载地址
- 英雄联盟服务器维修中启动游戏失败,电脑登录英雄联盟失败的八种解决方法
- 计算机领域 专利挖掘,浅谈如何进行软件专利的挖掘
- 马太效应和幂律分布是怎么回事?终于有人讲明白了