机器翻译评价指标之BLEU详细计算过程
原文连接 https://blog.csdn.net/guolindonggld/article/details/56966200
1. 简介
BLEU(Bilingual Evaluation Understudy),相信大家对这个评价指标的概念已经很熟悉,随便百度谷歌就有相关介绍。原论文为BLEU: a Method for Automatic Evaluation of Machine Translation,IBM出品。
本文通过一个例子详细介绍BLEU是如何计算以及NLTKnltk.align.bleu_score模块的源码。
首先祭出公式:
其中,
注意这里的BLEU值是针对一条翻译(一个样本)来说的。
NLTKnltk.align.bleu_score模块实现了这里的公式,主要包括三个函数,两个私有函数分别计算P和BP,一个函数整合计算BLEU值。
# 计算BLEU值
def bleu(candidate, references, weights)
# (1)私有函数,计算修正的n元精确率(Modified n-gram Precision)
def _modified_precision(candidate, references, n)
# (2)私有函数,计算BP惩罚因子
def _brevity_penalty(candidate, references)
例子:
候选译文(Predicted):
It is a guide to action which ensures that the military always obeys the commands of the party
参考译文(Gold Standard)
1:It is a guide to action that ensures that the military will forever heed Party commands
2:It is the guiding principle which guarantees the military forces always being under the command of the Party
3:It is the practical guide for the army always to heed the directions of the party
2. Modified n-gram Precision计算(也即是PnPn)
def _modified_precision(candidate, references, n):
counts = Counter(ngrams(candidate, n))
if not counts:
return 0
max_counts = {}
for reference in references:
reference_counts = Counter(ngrams(reference, n))
for ngram in counts:
max_counts[ngram] = max(max_counts.get(ngram, 0), reference_counts[ngram])
clipped_counts = dict((ngram, min(count, max_counts[ngram])) for ngram, count in counts.items())
return sum(clipped_counts.values()) / sum(counts.values())
我们这里nn取值为4,也就是从1-gram计算到4-gram。
Modified 1-gram precision:
首先统计候选译文里每个词出现的次数,然后统计每个词在参考译文中出现的次数,Max表示3个参考译文中的最大值,Min表示候选译文和Max两个的最小值。
词 | 候选译文 | 参考译文1 | 参考译文2 | 参考译文3 | Max | Min |
---|---|---|---|---|---|---|
the | 3 | 1 | 4 | 4 | 4 | 3 |
obeys | 1 | 0 | 0 | 0 | 0 | 0 |
a | 1 | 1 | 0 | 0 | 1 | 1 |
which | 1 | 0 | 1 | 0 | 1 | 1 |
ensures | 1 | 1 | 0 | 0 | 1 | 1 |
guide | 1 | 1 | 0 | 1 | 1 | 1 |
always | 1 | 0 | 1 | 1 | 1 | 1 |
is | 1 | 1 | 1 | 1 | 1 | 1 |
of | 1 | 0 | 1 | 1 | 1 | 1 |
to | 1 | 1 | 0 | 1 | 1 | 1 |
commands | 1 | 1 | 0 | 0 | 1 | 1 |
that | 1 | 2 | 0 | 0 | 2 | 1 |
It | 1 | 1 | 1 | 1 | 1 | 1 |
action | 1 | 1 | 0 | 0 | 1 | 1 |
party | 1 | 0 | 0 | 1 | 1 | 1 |
military | 1 | 1 | 1 | 0 | 1 | 1 |
然后将每个词的Min值相加,将候选译文每个词出现的次数相加,然后两值相除即得P1=3+0+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+13+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1=0.95P1=3+0+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+13+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1=0.95。
类似可得:
Modified 2-gram precision:
词 | 候选译文 | 参考译文1 | 参考译文2 | 参考译文3 | Max | Min |
---|---|---|---|---|---|---|
ensures that | 1 | 1 | 0 | 0 | 1 | 1 |
guide to | 1 | 1 | 0 | 0 | 1 | 1 |
which ensures | 1 | 0 | 0 | 0 | 0 | 0 |
obeys the | 1 | 0 | 0 | 0 | 0 | 0 |
commands of | 1 | 0 | 0 | 0 | 0 | 0 |
that the | 1 | 1 | 0 | 0 | 1 | 1 |
a guide | 1 | 1 | 0 | 0 | 1 | 1 |
of the | 1 | 0 | 1 | 1 | 1 | 1 |
always obeys | 1 | 0 | 0 | 0 | 0 | 0 |
the commands | 1 | 0 | 0 | 0 | 0 | 0 |
to action | 1 | 1 | 0 | 0 | 1 | 1 |
the party | 1 | 0 | 0 | 1 | 1 | 1 |
is a | 1 | 1 | 0 | 0 | 1 | 1 |
action which | 1 | 0 | 0 | 0 | 0 | 0 |
It is | 1 | 1 | 1 | 1 | 1 | 1 |
military always | 1 | 0 | 0 | 0 | 0 | 0 |
the military | 1 | 1 | 1 | 0 | 1 | 1 |
P2=1017=0.588235294P2=1017=0.588235294
Modified 3-gram precision:
词 | 候选译文 | 参考译文1 | 参考译文2 | 参考译文3 | Max | Min |
---|---|---|---|---|---|---|
ensures that the | 1 | 1 | 0 | 0 | 1 | 1 |
which ensures that | 1 | 0 | 0 | 0 | 0 | 0 |
action which ensures | 1 | 0 | 0 | 0 | 0 | 0 |
a guide to | 1 | 1 | 0 | 0 | 1 | 1 |
military always obeys | 1 | 0 | 0 | 0 | 0 | 0 |
the commands of | 1 | 0 | 0 | 0 | 0 | 0 |
commands of the | 1 | 0 | 0 | 0 | 0 | 0 |
to action which | 1 | 0 | 0 | 0 | 0 | 0 |
the military always | 1 | 0 | 0 | 0 | 0 | 0 |
obeys the commands | 1 | 0 | 0 | 0 | 0 | 0 |
It is a | 1 | 1 | 0 | 0 | 1 | 1 |
of the party | 1 | 0 | 0 | 1 | 1 | 1 |
is a guide | 1 | 1 | 0 | 0 | 1 | 1 |
that the military | 1 | 1 | 0 | 0 | 1 | 1 |
always obeys the | 1 | 0 | 0 | 0 | 0 | 0 |
guide to action | 1 | 1 | 0 | 0 | 1 | 1 |
P3=716=0.4375P3=716=0.4375
Modified 4-gram precision:
词 | 候选译文 | 参考译文1 | 参考译文2 | 参考译文3 | Max | Min |
---|---|---|---|---|---|---|
to action which ensures | 1 | 0 | 0 | 0 | 0 | 0 |
action which ensures that | 1 | 0 | 0 | 0 | 0 | 0 |
guide to action which | 1 | 0 | 0 | 0 | 0 | 0 |
obeys the commands of | 1 | 0 | 0 | 0 | 0 | 0 |
which ensures that the | 1 | 0 | 0 | 0 | 0 | 0 |
commands of the party | 1 | 0 | 0 | 0 | 0 | 0 |
ensures that the military | 1 | 1 | 0 | 0 | 1 | 1 |
a guide to action | 1 | 1 | 0 | 0 | 1 | 1 |
always obeys the commands | 1 | 0 | 0 | 0 | 0 | 0 |
that the military always | 1 | 0 | 0 | 0 | 0 | 0 |
the commands of the | 1 | 0 | 0 | 0 | 0 | 0 |
the military always obeys | 1 | 0 | 0 | 0 | 0 | 0 |
military always obeys the | 1 | 0 | 0 | 0 | 0 | 0 |
is a guide to | 1 | 1 | 0 | 0 | 1 | 1 |
It is a guide | 1 | 1 | 0 | 0 | 1 | 1 |
P4=415=0.266666667P4=415=0.266666667
然后我们取w1=w2=w3=w4=0.25w1=w2=w3=w4=0.25,也就是Uniform Weights。
所以:
∑Ni=1wnlogPn=0.25∗logP1+0.25∗logP2+0.25∗logP3+0.25∗logP4=−0.684055269517∑i=1NwnlogPn=0.25∗logP1+0.25∗logP2+0.25∗logP3+0.25∗logP4=−0.684055269517
3. Brevity Penalty 计算
def _brevity_penalty(candidate, references):
c = len(candidate)
ref_lens = (len(reference) for reference in references)
#这里有个知识点是Python中元组是可以比较的,如(0,1)>(1,0)返回False,这里利用元组比较实现了选取参考翻译中长度最接近候选翻译的句子,当最接近的参考翻译有多个时,选取最短的。例如候选翻译长度是10,两个参考翻译长度分别为9和11,则r=9.
r = min(ref_lens, key=lambda ref_len: (abs(ref_len - c), ref_len))
print 'r:',r
if c > r:
return 1
else:
return math.exp(1 - r / c)
下面计算BP(Brevity Penalty),翻译过来就是“过短惩罚”。由BP的公式可知取值范围是(0,1],候选句子越短,越接近0。
候选翻译句子长度为18,参考翻译分别为:16,18,16。
所以c=18c=18,r=18r=18(参考翻译中选取长度最接近候选翻译的作为rr)
所以BP=e0=1BP=e0=1
4. 整合
最终BLEU=1⋅exp(−0.684055269517)=0.504566684006BLEU=1⋅exp(−0.684055269517)=0.504566684006。
BLEU的取值范围是[0,1],0最差,1最好。
通过计算过程,我们可以看到,BLEU值其实也就是“改进版的n-gram”加上“过短惩罚因子”。
机器翻译评价指标之BLEU详细计算过程的更多相关文章
- 机器翻译评价指标 — BLEU算法
1,概述 机器翻译中常用的自动评价指标是 $BLEU$ 算法,除了在机器翻译中的应用,在其他的 $seq2seq$ 任务中也会使用,例如对话系统. 2 $BLEU$算法详解 假定人工给出的译文为$re ...
- linux主机load average的概念&&计算过程&&注意事项
最近开发的一个模块需要根据机房各节点的负载情况(如网卡IO.load average等指标)做任务调度,刚开始对Linux机器load average这项指标不是很清楚,经过调研,终于搞清楚了其计算方 ...
- 串口调试助手vc源程序及其详细编写过程
串口调试助手vc源程序及其详细编写过程 目次: 1.建立项目 2.在项目中插入MSComm控件 3.利用ClassWizard定义CMSComm类控制变量 4.在对话框中添加控件 5.添加串口事件 ...
- HTTPS详解二:SSL / TLS 工作原理和详细握手过程
HTTPS 详解一:附带最精美详尽的 HTTPS 原理图 HTTPS详解二:SSL / TLS 工作原理和详细握手过程 在上篇文章HTTPS详解一中,我已经为大家介绍了 HTTPS 的详细原理和通信流 ...
- 主动模式下FTP的详细工作过程(转) 挺详细
主动模式下FTP的详细工作过程 PORT FTP是常用的FTP工作方式,当客户端的连接请求到来时,FTP服务器会利用默认的21端口与客户端建立连接,该连接属于命令通道,利用该通道来下达控 制指令: ...
- Tomcat7.0.22在Windows下详细配置过程
Tomcat7.0.22在Windows下详细配置过程 一.JDK1.7安装 1.下载jdk,下载地址:http://www.oracle.com/technetwork/java/javase/do ...
- zabbix3.0.4使用percona-monitoring-plugins插件来监控mysql5.6的详细实现过程
zabbix3.0.4使用percona-monitoring-plugins插件来监控mysql5.6的详细实现过程 因为Zabbix自带的MySQL监控没有提供可以直接使用的Key,所以一般不采用 ...
- Webmin详细安装过程及问题解决
管理系统是件艰巨的任务,创建用户账户,配置服务,检查日志,还有系统管理员必须面对的所有其他的职责,都使系统管理工作成为一个不小的负担.下面介绍一个叫webmin的软件,webmin软件安装后能让读者从 ...
- 一个DOS攻击木马的详细分析过程
一个DOS攻击木马的详细分析过程 0×01 起因 网路流量里发现了大量的的1.exe的文件,而且一直在持续,第一感觉就像是一个木马程序,而且每个1.exe的MD5都不一样,对比发现只有几个字节不一样( ...
随机推荐
- java 报错英文
—————————— ASP.Net+Android+IOS开发..Net培训.期待与您交流! —————————— 第一章:JDK(Java Development Kit) java开发工具包 J ...
- 7-11Zombie's Treasure Chest uva12325
题意 你有一个体积为N的箱子和两种数量无限的宝物 宝物1的体积为s1 价值为v1 宝物2同理 输入均为32位带符号整数 你的任务是计算最多能带走多少价值的宝物 暴力枚举: 首先明白一点 ...
- 22.python中的面向对象和类的基本语法
当我发现要写python的面向对象的时候,我是踌躇满面,坐立不安呀.我一直在想:这个坑应该怎么爬?因为python中关于面向对象的内容很多,如果要讲透,最好是用面向对象的思想重新学一遍前面的内容.这个 ...
- LOJ.121.[离线可过]动态图连通性(线段树分治 按秩合并)
题目链接 以时间为下标建线段树.线段树每个节点开个vector. 对每条边在其出现时间内加入线段树,即,把这条边按时间放在线段树的对应区间上,会影响\(O(\log n)\)个节点. 询问就放在线段树 ...
- [HDU2874]Connections between cities
思路:LCA裸题.本来是帮pechpo调错,结果自己写了半天… 设$dis_x$是点$x$到根结点距离,不难想到两点$u$.$v$之间最短距离等于$dis_u+dis_v-dis_{LCA(u,v)} ...
- OpenStack Juno 版本发布——支持Spark和NFV[转]
作者:郑晨,OpenStack中国社区,转载请注明出处 美国时间2014年10月16日,OpenStack Juno版本正式发布,这是OpenStack开源云计算项目自2010年创立以来的第10个版本 ...
- Node.js开源应用OSN发布初始V1.0版本-见面版本
Nodejs开源应用OSN初始版本V1.0发布,请参考本操作说明文档,有任何问题请留言 Nodejs开源应用OSN发布V1.0版本: OSChina收录地址: OSC收录地址:http://www.o ...
- 【转载】VC IME 通信
文本输入框作为一个最基本的UI控件,被众多UI框架默认支持.Windows下最简单的就是CEdit(WTL封装),也有更为复杂的CRichEdit(WTL封装).文本输入框是基本控件中最难实现的控件之 ...
- 如何利用Reveal神器查看各大APP UI搭建层级
作者 乔同X2016.08.22 19:45 写了3195字,被42人关注,获得了73个喜欢 如何利用Reveal神器查看各大APP UI搭建层级 字数413 阅读110 评论0 喜欢5 title: ...
- 重读JavaScript高级程序设计
不断更新中--- 第三章 基本概念 1.变量声明但未初始化值是undefined,而未声明的变量只能执行typeof操作,并且未初始化和未声明用typeof都同样返回undefined 2.Numbe ...