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

news2024/9/23 3:24:22

文章目录

  • 一、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, 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}")

输出:

N=1: 1.0
N=2: 0.8

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

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

相关文章

uniapp运行到手机模拟器

第一步,下载MUMU模拟器 下载地址:MuMu模拟器官网_安卓12模拟器_网易手游模拟器 (163.com) 第二步,运行mumu模拟器 第三步,运行mumu多开器 第三步,查看abs 端口 第四步,打开HBuilder,如下图,将…

探索 Vuex 的世界:状态管理的新视角(下)

🤍 前端开发工程师(主业)、技术博主(副业)、已过CET6 🍨 阿珊和她的猫_CSDN个人主页 🕠 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 🍚 蓝桥云课签约作者、已在蓝桥云…

大模型赋能“AI+电商”,景联文科技提供高质量电商场景数据

据新闻报道,阿里巴巴旗下淘天集团和国际数字商业集团都已建立完整的AI团队。 淘天集团已经推出模特图智能生成、官方客服机器人、万相台无界版等AI工具,训练出了自己的大模型产品 “星辰”; 阿里国际商业集团已成立AI Business,…

亚马逊云科技-如何缩容/减小您的AWS EC2根卷大小-简明教程

一、背景 Amazon EBS提供了块级存储卷以用于 EC2 实例,EBS具备弹性的特点,可以动态的增加容量、更改卷类型以及修改预配置的IOPS值。但是EBS不能动态的减少容量,在实际使用中,用户也许会存在此类场景: 在创建AWS EC2…

mac电脑安装虚拟机教程

1、准备一台虚拟机,安装CentOS7 常用的虚拟化软件有两种: VirtualBoxVMware 这里我们使用VirtualBox来安装虚拟机,下载地址:Downloads – Oracle VM VirtualBox 001 点击安装 002 报错:he installer has detected an…

Linux shell编程学习笔记37:readarray命令和mapfile命令

目录 0 前言1 readarray命令的格式和功能 1.1 命令格式1.2 命令功能1.3 注意事项2 命令应用实例 2.1 从标准输入读取数据时不指定数组名,则数据会保存到MAPFILE数组中2.2 从标准输入读取数据并存储到指定的数组2.3 使用 -O 选项指定起始下标2.4 用-n指定有效行数…

【Filament】绘制圆形

1 前言 Filament环境搭建中介绍了 Filament 的 Windows 和 Android 环境搭,绘制三角形中介绍了绘制纯色和彩色三角形,绘制矩形中介绍了绘制纯色和彩色矩形,本文将使用 Filament 绘制圆形。 2 绘制圆形 本文项目结构如下,完整代码…

Apache Flink(十七):Flink On Standalone任务提交-Standalone Application模式

🏡 个人主页:IT贫道_大数据OLAP体系技术栈,Apache Doris,Clickhouse 技术-CSDN博客 🚩 私聊博主:加入大数据技术讨论群聊,获取更多大数据资料。 🔔 博主个人B栈地址:豹哥教你大数据的个人空间-豹哥教你大数据个人主页-哔哩哔哩视频 目录

从零开始的神经网络

先决条件 在本文中,我将解释如何通过实现前向和后向传递(反向传播)来构建基本的深度神经网络。这需要一些关于神经网络功能的具体知识。 了解线性代数的基础知识也很重要,这样才能理解我为什么要在本文中执行某些运算。我最好的…

【FLV】文件解析源码分析:视频解析为可解码的nalu单元

https flv 拉到的数据是flv宏观看 : 每一部分都是 A+ Prev 的模式 A 可以是header :9个字节可以是TAG :大小可变而每个TAG 都有个固定的部分: TAG HEADER ,大小9个字节 ,里面是类型、大小、时间戳、扩展时间戳、流ID 因此,可以直接去掉9+4 个字节的第一部分:FLV HEADER…

凯斯西储大学轴承数据解读

文章目录 一、凯斯西储大学轴承数据基础知识?1.1 故障种类1.2 故障点尺寸(单点故障)1.3 载荷和转速 二、数据解读2.1 文件2.2 以12k Drive End Bearing Fault Data为例2.3 以(0.007,inner race)为例。 3 Normal Baseli…

vivado 关于时钟

关于时钟 在数字设计中,时钟代表了从寄存器可靠传输数据的时间基准注册。AMD Vivado™集成设计环境(IDE)计时引擎使用时钟计算时序路径要求并通过以下方式报告设计时序裕度的特性松弛计算的方法有关更多信息,请参阅Vivado Design…

杰发科技AC7840——在Eclipse环境下使用Jlink调试

序 杰发给的代码里面已经做代码相关配置,搭建好eclipse环境即可运行,搭建步骤还是比较简单的。 参考文章 如何使用Eclipse搭配JLink来调试HelloWold应用程序?-电子发烧友网 软件链接 杰发科技Eclipse的sample代码里面的doc文章&#xff…

C# .Net学习笔记—— Expression 表达式目录树

一、什么是表达式目录树 (1)Expression我们称为是表达式树,是一种数据结构体,用于存储需要计算,运算的一种结构,这种结构可以只是存储,而不进行运算。通常表达式目录树是配合Lambda一起来使用的…

关于“Python”的核心知识点整理大全32

目录 12.6.4 调整飞船的速度 settings.py ship.py alien_invasion.py 12.6.5 限制飞船的活动范围 ship.py 12.6.6 重构 check_events() game_functions.py 12.7 简单回顾 12.7.1 alien_invasion.py 12.7.2 settings.py 12.7.3 game_functions.py 12.7.4 ship.py …

Hive入门+部署

看黑马视频做的笔记 目录 概念 1.基本概述 2.基础架构 总架构 部署 1.安装MySQL 2.配置Hadoop 3.下载解压Hive 4.下载MySQL Driver包 注意! 5.配置Hive 6.初始化元数据库 7.启动Hive(使用Hadoop用户) 实例 查看HDFS上表中存…

网络基础【网线的制作、OSI七层模型、集线器、交换机介绍、路由器的配置】

目录 一.网线的制作 1.1.网线的标准 1.2.水晶头的做法 二.OSI七层模型、集线器、交换机介绍 集线器(Hub): 交换机(Switch): 三.路由器的配置 3.1.使用 3.2.常用的功能介绍 1、如何管理路由器 2、家…

CW32单片机在智能马桶的应用介绍

智能科技的迅速发展使得我们的日常生活变得更加便捷和舒适。智能马桶作为其中一种智能家居产品,通过单片机接受和处理来自传感器的数据,然后通过控制模块对智能马桶的各项功能进行控制,实现对智能马桶的全面控制和调节。本文将介绍CW32单片机…

苹果发布iOS 17.2.1版本更新

12月20日,苹果向iPhone用户推送了iOS 17.2.1更新。苹果公司在更新日志中称:“本更新包含了重要的错误修复,并解决了某些情况下电池电量较预期更快耗尽的问题。” 据报道,iOS 17系统在发布初期便出现了一系列问题,如发…

PSP - 结构生物学中的机器学习 (NIPS MLSB Workshop 2023.12)

欢迎关注我的CSDN:https://spike.blog.csdn.net/ 本文地址:https://spike.blog.csdn.net/article/details/135120094 Machine Learning in Structural Biology (机器学习在结构生物学中) 网址:https://www.mlsb.io/ Workshop at the 37th Co…