【深度学习】序列生成模型(五):评价方法计算实例:计算BLEU-N得分【理论到程序】

文章目录

  • 一、BLEU-N得分(Bilingual Evaluation Understudy)
    • 1. 定义
    • 2. 计算
      • N=1
      • N=2
      • BLEU-N 得分
    • 3. 程序

  给定一个生成序列“The cat sat on the mat”和两个参考序列“The cat is on the mat”“The bird sat on the bush”分别计算BLEU-N和ROUGE-N得分(N=1或N =2时).

  • 生成序列 x = the cat sat on the mat \mathbf{x}=\text{the cat sat on the mat} x=the cat sat on the mat
  • 参考序列
    • s ( 1 ) = the cat is on the mat \mathbf{s}^{(1)}=\text{the cat is on the mat} s(1)=the cat is on the mat
    • s ( 2 ) = the bird sat on the bush \mathbf{s}^{(2)}=\text{the bird sat on the bush} s(2)=the bird sat on the bush

一、BLEU-N得分(Bilingual Evaluation Understudy)

在这里插入图片描述
在这里插入图片描述

1. 定义

  设 𝒙 为模型生成的候选序列, s ( 1 ) , ⋯ , s ( K ) \mathbf{s^{(1)}}, ⋯ , \mathbf{s^{(K)}} s(1),,s(K) 为一组参考序列,𝒲 为从生成的候选序列中提取所有N元组合的集合。BLEU算法的精度(Precision)定义如下:

P N ( x ) = ∑ w ∈ W min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) ∑ w ∈ W c w ( x ) P_N(\mathbf{x}) = \frac{\sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))}{\sum_{w \in \mathcal{W}} c_w(\mathbf{x})} PN(x)=wWcw(x)wWmin(cw(x),maxk=1Kcw(s(k)))

其中 c w ( x ) c_w(\mathbf{x}) cw(x) 是N元组合 w w w生成序列 x \mathbf{x} x中出现的次数, c w ( s ( k ) ) c_w(\mathbf{s}^{(k)}) cw(s(k)) 是N元组合 w w w参考序列 s ( k ) \mathbf{s}^{(k)} s(k) 中出现的次数。

  为了处理生成序列长度短于参考序列的情况,引入长度惩罚因子 b ( x ) b(\mathbf{x}) b(x)

b ( x ) = { 1 if  l x > l s exp ⁡ ( 1 − l s l x ) if  l x ≤ l s b(\mathbf{x}) = \begin{cases} 1 & \text{if } l_x > l_s \\ \exp\left(1 - \frac{l_s}{l_x}\right) & \text{if } l_x \leq l_s \end{cases} b(x)={1exp(1lxls)if lx>lsif lxls

其中 l x l_x lx 是生成序列的长度, l s l_s ls 是参考序列的最短长度。

  BLEU算法通过计算不同长度的N元组合的精度,并进行几何加权平均,得到最终的BLEU分数:

BLEU-N ( x ) = b ( x ) × exp ⁡ ( ∑ N = 1 N ′ α N log ⁡ P N ( x ) ) \text{BLEU-N}(\mathbf{x}) = b(\mathbf{x}) \times \exp\left( \sum_{N=1}^{N'} \alpha_N \log P_N(\mathbf{x})\right) BLEU-N(x)=b(x)×exp N=1NαNlogPN(x)

其中 N ′ N' N 为最长N元组合的长度, α N \alpha_N αN 是不同N元组合的权重,一般设为 1 / N ′ 1/N' 1/N

2. 计算

N=1

  • 生成序列 x = the cat sat on the mat \mathbf{x}=\text{the cat sat on the mat} x=the cat sat on the mat
  • 参考序列
    • s ( 1 ) = the cat is on the mat \mathbf{s}^{(1)}=\text{the cat is on the mat} s(1)=the cat is on the mat
    • s ( 2 ) = the bird sat on the bush \mathbf{s}^{(2)}=\text{the bird sat on the bush} s(2)=the bird sat on the bush
  • W = the, cat, sat, on, mat \mathcal{W}=\text{ {the, cat, sat, on, mat}} W= the, cat, sat, on, mat
    • w = the w=\text{the} w=the
      • c w ( x ) = 2 , c w ( s ( 1 ) ) = 2 , c w ( s ( 2 ) ) = 2 c_w(\mathbf{x})=2, c_w(\mathbf{s^{(1)}})=2,c_w(\mathbf{s^{(2)}})=2 cw(x)=2,cw(s(1))=2,cw(s(2))=2
      • max ⁡ k = 1 K c w ( s ( k ) ) ) = 2 \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=2 maxk=1Kcw(s(k)))=2
      • min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) = 2 \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=2 min(cw(x),maxk=1Kcw(s(k)))=2
    • w = cat w=\text{cat} w=cat
      • c w ( x ) = 1 , c w ( s ( 1 ) ) = 1 , c w ( s ( 2 ) ) = 0 c_w(\mathbf{x})=1, c_w(\mathbf{s^{(1)}})=1,c_w(\mathbf{s^{(2)}})=0 cw(x)=1,cw(s(1))=1,cw(s(2))=0
      • max ⁡ k = 1 K c w ( s ( k ) ) ) = 1 \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=1 maxk=1Kcw(s(k)))=1
      • min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) = 1 \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=1 min(cw(x),maxk=1Kcw(s(k)))=1
    • w = sat w=\text{sat} w=sat
      • c w ( x ) = 1 , c w ( s ( 1 ) ) = 0 , c w ( s ( 2 ) ) = 1 c_w(\mathbf{x})=1, c_w(\mathbf{s^{(1)}})=0, c_w(\mathbf{s^{(2)}})=1 cw(x)=1,cw(s(1))=0,cw(s(2))=1
      • max ⁡ k = 1 K c w ( s ( k ) ) ) = 1 \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=1 maxk=1Kcw(s(k)))=1
      • min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) = 1 \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=1 min(cw(x),maxk=1Kcw(s(k)))=1
    • w = on w=\text{on} w=on
      • c w ( x ) = 1 , c w ( s ( 1 ) ) = 1 , c w ( s ( 2 ) ) = 1 c_w(\mathbf{x})=1, c_w(\mathbf{s^{(1)}})=1,c_w(\mathbf{s^{(2)}})=1 cw(x)=1,cw(s(1))=1,cw(s(2))=1
      • max ⁡ k = 1 K c w ( s ( k ) ) ) = 1 \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=1 maxk=1Kcw(s(k)))=1
      • min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) = 1 \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=1 min(cw(x),maxk=1Kcw(s(k)))=1
    • w = mat w=\text{mat} w=mat
      • c w ( x ) = 1 , c w ( s ( 1 ) ) = 1 , c w ( s ( 2 ) ) = 0 c_w(\mathbf{x})=1, c_w(\mathbf{s^{(1)}})=1,c_w(\mathbf{s^{(2)}})=0 cw(x)=1,cw(s(1))=1,cw(s(2))=0
      • max ⁡ k = 1 K c w ( s ( k ) ) ) = 1 \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=1 maxk=1Kcw(s(k)))=1
      • min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) = 1 \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=1 min(cw(x),maxk=1Kcw(s(k)))=1
  • ∑ w ∈ W min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) = 2 + 1 + 1 + 1 + 1 + 1 = 6 \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=2+1+1+1+1+1=6 wWmin(cw(x),maxk=1Kcw(s(k)))=2+1+1+1+1+1=6
  • ∑ w ∈ W c w ( x ) = 1 + 1 + 1 + 1 + 1 + 1 = 6 \sum_{w \in \mathcal{W}} c_w(\mathbf{x})=1+1+1+1+1+1=6 wWcw(x)=1+1+1+1+1+1=6
  • P 1 ( x ) = ∑ w ∈ W min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) ∑ w ∈ W c w ( x ) = 6 6 = 1 P_1(\mathbf{x}) = \frac{\sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))}{\sum_{w \in \mathcal{W}} c_w(\mathbf{x})}= \frac{6}{6}=1 P1(x)=wWcw(x)wWmin(cw(x),maxk=1Kcw(s(k)))=66=1

N=2

  • 生成序列 x = the cat sat on the mat \mathbf{x}=\text{the cat sat on the mat} x=the cat sat on the mat
  • 参考序列
    • s ( 1 ) = the cat is on the mat \mathbf{s}^{(1)}=\text{the cat is on the mat} s(1)=the cat is on the mat
    • s ( 2 ) = the bird sat on the bush \mathbf{s}^{(2)}=\text{the bird sat on the bush} s(2)=the bird sat on the bush
  • W = the cat, cat sat, sat on, on the, the mat  \mathcal{W}=\text{{the cat, cat sat, sat on, on the, the mat} } W=the cat, cat sat, sat on, on the, the mat 
w w w c w ( x ) c_w(\mathbf{x}) cw(x) c w ( s ( 1 ) ) c_w(\mathbf{s^{(1)}}) cw(s(1)) c w ( s ( 2 ) ) c_w(\mathbf{s^{(2)}}) cw(s(2)) max ⁡ k = 1 K c w ( s ( k ) ) ) \max_{k=1}^{K} c_w(\mathbf{s}^{(k)})) maxk=1Kcw(s(k))) min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)})) min(cw(x),maxk=1Kcw(s(k)))
the cat11011
cat sat10000
sat on10111
on the11111
the mat11011
  • ∑ w ∈ W min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) = 1 + 0 + 1 + 1 + 1 = 4 \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))=1+0+1+1+1=4 wWmin(cw(x),maxk=1Kcw(s(k)))=1+0+1+1+1=4
  • ∑ w ∈ W c w ( x ) = 1 + 1 + 1 + 1 + 1 = 5 \sum_{w \in \mathcal{W}} c_w(\mathbf{x})=1+1+1+1+1=5 wWcw(x)=1+1+1+1+1=5
  • P 2 ( x ) = ∑ w ∈ W min ⁡ ( c w ( x ) , max ⁡ k = 1 K c w ( s ( k ) ) ) ∑ w ∈ W c w ( x ) = 4 5 P_2(\mathbf{x}) = \frac{\sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), \max_{k=1}^{K} c_w(\mathbf{s}^{(k)}))}{\sum_{w \in \mathcal{W}} c_w(\mathbf{x})}= \frac{4}{5} P2(x)=wWcw(x)wWmin(cw(x),maxk=1Kcw(s(k)))=54

BLEU-N 得分

  为了处理生成序列长度短于参考序列的情况,引入长度惩罚因子 b ( x ) b(\mathbf{x}) b(x) b ( x ) = { 1 if  l x > l s exp ⁡ ( 1 − l s l x ) if  l x ≤ l s b(\mathbf{x}) = \begin{cases} 1 & \text{if } l_x > l_s \\ \exp\left(1 - \frac{l_s}{l_x}\right) & \text{if } l_x \leq l_s \end{cases} b(x)={1exp(1lxls)if lx>lsif lxls其中 l x l_x lx 是生成序列的长度, l s l_s ls 是参考序列的最短长度。

  这里 l x = l s ( 1 ) = l s ( 2 ) = 6 l_x=l_{s^{(1)}}=l_{s^{(2)}}=6 lx=ls(1)=ls(2)=6,因此 b ( x ) = e ( 1 − l s l x ) = e 0 = 1 b(\mathbf{x}) =e^{\left( 1 - \frac{l_s}{l_x} \right)}=e^0=1 b(x)=e(1lxls)=e0=1

  BLEU算法通过计算不同长度的N元组合的精度,并进行几何加权平均,得到最终的BLEU分数:
BLEU-N ( x ) = b ( x ) × exp ⁡ ( 1 N ′ ∑ N = 1 N ′ α N log ⁡ P N ( x ) ) \text{BLEU-N}(\mathbf{x}) = b(\mathbf{x}) \times \exp\left(\frac{1}{N'} \sum_{N=1}^{N'} \alpha_N \log P_N(\mathbf{x})\right) BLEU-N(x)=b(x)×exp N1N=1NαNlogPN(x) 其中 N ′ N' N 为最长N元组合的长度, α N \alpha_N αN 是不同N元组合的权重,一般设为 1 / N ′ 1/N' 1/N
BLEU-N ( x ) = 1 × exp ⁡ ( ∑ N = 1 2 1 2 log ⁡ P N ( x ) ) = exp ⁡ ( 1 2 log ⁡ P 1 ( x ) + 1 2 log ⁡ P 2 ( x ) ) = exp ⁡ ( 1 2 log ⁡ 1 + 1 2 log ⁡ 4 5 ) = exp ⁡ ( 0 + log ⁡ 4 5 ) = 4 5 \text{BLEU-N}(\mathbf{x}) = 1 \times\exp\left( \sum_{N=1}^{2} \frac{1}{2} \log P_N(\mathbf{x})\right)\\ =\exp\left(\frac{1}{2}\log P_1(\mathbf{x})+\frac{1}{2}\log P_2(\mathbf{x)}\right)\\ =\exp\left(\frac{1}{2}\log 1+\frac{1}{2}\log \frac{4}{5}\right)\\ =\exp\left(0+\log \sqrt\frac{4}{5}\right)\\ =\sqrt\frac{4}{5} BLEU-N(x)=1×exp(N=1221logPN(x))=exp(21logP1(x)+21logP2(x))=exp(21log1+21log54)=exp(0+log54 )=54

3. 程序

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, 0for word in unique_words:count_main_string = main_string.count(word)total_occurrences += count_main_stringmatching_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, 0for bigram in bigrams:count_main_string = main_string.count(bigram)total_occurrences += count_main_stringmatching_occurrences += min(count_main_string, max(string1.count(bigram), string2.count(bigram)))similarity_bigram = matching_occurrences / total_occurrences
print(f"N=2: {similarity_bigram}")

输出:

N=1: 1.0
N=2: 0.8

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/235896.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WEB渗透—PHP反序列化(六)

Web渗透—PHP反序列化 课程学习分享(课程非本人制作,仅提供学习分享) 靶场下载地址:GitHub - mcc0624/php_ser_Class: php反序列化靶场课程,基于课程制作的靶场 课程地址:PHP反序列化漏洞学习_哔哩…

Ubuntu 22.04 禁用(彻底移除)Snap

什么是Snaps Snaps 是 Ubuntu 的母公司 Canonical 于 2016 年 4 月发布 Ubuntu 16.04 LTS(Long Term Support,长期支持版)时引入的一种容器化的软件包格式。自 Ubuntu 16.04 LTS 起,Ubuntu 操作系统可以同时支持 Snap 及 Debian …

3dsmax渲染太慢,用云渲染农场多少钱?

对于许多从事计算机图形设计的创作者来说,渲染速度慢是一个常见问题,尤其是对于那些追求极致出图效果的室内设计师和建筑可视化师,他们通常使用3ds Max这样的工具,而高质量的渲染经常意味着长时间的等待。场景复杂、细节丰富&…

v-model 的原理

v-model是Vue.js框架中的一个指令,用于在表单元素和组件之间实现双向数据绑定。它提供了一种简洁的方式来将表单输入的值与Vue实例的属性进行关联。 当使用v-model指令时,Vue会根据表单元素的类型(如input、select、textarea等)自…

公共淋浴废水处理工艺流程及设备

公共淋浴废水处理工艺流程及设备 公共场所的淋浴废水含有有机物、污垢、清洁剂残留等污染物,如果直接排放到自然环境中,将对环境造成严重污染。为了有效处理这种废水,保护水资源和环境,制定一套完整的公共淋浴废水处理工艺流程&am…

APView500PV电能质量在线监测装置——安科瑞 顾烊宇

概述 APView500PV电能质量在线监测装置采用了高性能多核平台和嵌入式操作系统,遵照IEC61000-4-30《测试和测量技术-电能质量测量方法》中规定的各电能质量指标的测量方法进行测量,集谐波分析、波形采样、电压暂降/暂升/中断、闪变监测、电压不平衡度监测…

CentOS操作学习(二)

上一篇学习了CentOS的常用指令CentOS指令学习-CSDN博客 现在我们接着学习 一、Vi编辑器 这是CentOS中自带的编辑器 三种模式 进入编辑模式后 i:在光标所在字符前开始插入a:在光标所在字符串后开始插入o:在光标所在行的下面另起一新行插入…

命令执行 [SWPUCTF 2021 新生赛]easyrce

打开题目 提示要用url传参,但实际是用url进行一些系统命令执行 那我们就用whoami命令来查看用户和权限 那我们直接用ls / 去查看当下根目录下有哪些文件 我们看到根目录下有flag 直接cat读取就行 知识点: system system是一个函数 用来运行外部的程序…

4.CentOS7开启ssh

Centos7开启ssh 通过命令查看是否安装了ssh服务 rpm -qa | grep openssh 修改主配置文件 vim /etc/ssh/sshd_config 将PermitRootLogin,RSAAuthentication,PubkeyAuthentication的设置打开 RSAAuthentication yes# 启用 RSA 认证PubkeyAuthenticatio…

19_20-Golang中的切片

**Golang **中的切片 主讲教师:(大地) 合作网站:www.itying.com** **(IT 营) 我的专栏:https://www.itying.com/category-79-b0.html 1、为什么要使用切片 因为数组的长度是固定的并且数组长…

yolov5-7.0训练实力分割报错

File “train.py”, line 666, in main(opt) File “train.py”, line 557, in main train(opt.hyp, opt, device, callbacks) File “train.py”, line 349, in train logger.log_images(files, ‘Mosaics’, epoch) File “/root/autodl-tmp/yolov5-master/utils/loggers/in…

【.NET后端工具系列】MediatR实现进程内消息通讯

阅读本文你的收获 学习MediatR工具,实现进程内消息发送和处理过程的解耦学习MediatR的两种消息处理模式了解中介者模式和其好处 一、什么是MediatR? MediatR是一款基于中介者模式的思想而实现的.NET库,支持.NET Framework和跨平台 的.NET C…

502无效网关错误是什么以及如何修复?这里提供详细解释及可能的解决办法

502无效网关错误是一个HTTP状态代码,这意味着互联网上的一台服务器收到了来自另一台服务器的无效响应。这些错误完全独立于你的特定设置,这意味着你可以在任何浏览器、任何操作系统和任何设备上看到错误。 502无效网关错误显示在互联网浏览器窗口中,就像网页一样。 502无效…

aws配置以及下载 spaceNet6 数据集

一:注册亚马逊账号 注册的时候,唯一需要注意的是信用卡绑定,这个可以去淘宝买,搜索aws匿名卡。 注册完记得点击登录,记录一下自己的账户ID哦! 二:登录自己的aws账号 2.1 首先创建一个用户 首…

从YOLOv1到YOLOv8的YOLO系列最新综述【2023年4月】

作者:Juan R. Terven 、Diana M. Cordova-Esparaza 摘要:YOLO已经成为机器人、无人驾驶汽车和视频监控应用的核心实时物体检测系统。我们对YOLO的演变进行了全面的分析,研究了从最初的YOLO到YOLOv8每次迭代的创新和贡献。我们首先描述了标准…

研发管理-代码管理篇

前言: 工作了这些年,工作了三家公司,也用过主流的代码管理平台,比如SVN,git系列(gitlib,gitee),各有优点,我个人比较喜欢SVN,多人协作的代码管理难免会有代码冲突&#…

flutter学习-day16-自定义组件

📚 目录 介绍组合多个组件自绘组件 Custompaint绘制边界RepaintBoundaryCustomPainter与Canvas画笔Paint绘制组件例子 本文学习和引用自《Flutter实战第二版》:作者:杜文 1. 介绍 当Flutter提供的现有组件无法满足我们的需求,或…

LeetCode 每日一题 Day 18 || 简单模拟

2828. 判别首字母缩略词 给你一个字符串数组 words 和一个字符串 s ,请你判断 s 是不是 words 的 首字母缩略词 。 如果可以按顺序串联 words 中每个字符串的第一个字符形成字符串 s ,则认为 s 是 words 的首字母缩略词。例如,“ab” 可以由…

2024年【北京市安全员-B证】考试试卷及北京市安全员-B证复审模拟考试

题库来源:安全生产模拟考试一点通公众号小程序 北京市安全员-B证考试试卷根据新北京市安全员-B证考试大纲要求,安全生产模拟考试一点通将北京市安全员-B证模拟考试试题进行汇编,组成一套北京市安全员-B证全真模拟考试试题,学员可…