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

文章目录

  • 一、BLEU-N得分(Bilingual Evaluation Understudy)
  • 二、ROUGE-N得分(Recall-Oriented Understudy for Gisting Evaluation)
    • 1. 定义
    • 2. 计算
      • N=1
      • N=2
    • 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)

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

二、ROUGE-N得分(Recall-Oriented Understudy for Gisting Evaluation)

在这里插入图片描述

1. 定义

  设 x \mathbf{x} x 为从模型分布 p θ p_{\theta} pθ 中生成的一个候选序列, s ( 1 ) , ⋯ , s ( K ) \mathbf{s^{(1)}}, ⋯ , \mathbf{s^{(K)}} s(1),,s(K) 为从真实数据分布中采样得到的一组参考序列, W \mathcal{W} W 为从参考序列中提取N元组合的集合,ROUGE-N算法的定义为:

ROUGE-N ( x ) = ∑ k = 1 K ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( k ) ) ) ∑ k = 1 K ∑ w ∈ W c w ( s ( k ) ) \text{ROUGE-N}(\mathbf{x}) = \frac{\sum_{k=1}^{K} \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(k)}))}{\sum_{k=1}^{K} \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(k))}} ROUGE-N(x)=k=1KwWcw(s(k))k=1KwWmin(cw(x),cw(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) 中出现的次数。

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, is, on, mat, bird, sat, bush  \mathcal{W}=\text{ {the, cat, is, on, mat, bird, sat, bush }} W= the, cat, is, on, mat, bird, sat, bush 
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)) min ⁡ ( c w ( x ) , c w ( s ( 1 ) ) \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(1)}) min(cw(x),cw(s(1)) min ⁡ ( c w ( x ) , c w ( s ( 2 ) ) \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(2)}) min(cw(x),cw(s(2))
the22222
cat11010
is01000
on11111
mat11010
bird00100
sat10101
bush00100
  • 分子 ∑ k = 1 K ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( k ) ) ) \sum_{k=1}^{K} \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(k)})) k=1KwWmin(cw(x),cw(s(k)))
    • ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( 1 ) ) = 2 + 1 + 0 + 1 + 1 + 0 + 0 + 0 = 5 \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(1)})=2+1+0+1+1+0+0+0=5 wWmin(cw(x),cw(s(1))=2+1+0+1+1+0+0+0=5
    • ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( 2 ) ) = 2 + 0 + 0 + 1 + 0 + 0 + 1 + 0 = 4 \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(2)})=2+0+0+1+0+0+1+0=4 wWmin(cw(x),cw(s(2))=2+0+0+1+0+0+1+0=4
    • ∑ k = 1 2 ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( k ) ) ) = ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( 1 ) ) ) + ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( 2 ) ) ) = 5 + 4 = 9 \sum_{k=1}^{2} \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(k)}))=\sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(1)}))+\sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(2)}))=5+4=9 k=12wWmin(cw(x),cw(s(k)))=wWmin(cw(x),cw(s(1)))+wWmin(cw(x),cw(s(2)))=5+4=9
  • 分母 ∑ k = 1 K ∑ w ∈ W c w ( s ( k ) ) \sum_{k=1}^{K} \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(k)}) k=1KwWcw(s(k))
    • ∑ w ∈ W c w ( s ( 1 ) ) = 6 \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(1))}=6 wWcw(s(1))=6
    • ∑ w ∈ W c w ( s ( 2 ) ) = 6 \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(2)})=6 wWcw(s(2))=6
    • ∑ k = 1 2 ∑ w ∈ W c w ( s ( k ) ) = ∑ w ∈ W c w ( s ( 1 ) ) + ∑ w ∈ W c w ( s ( 2 ) ) = 12 \sum_{k=1}^{2} \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(k)})= \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(1)})+ \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(2)})=12 k=12wWcw(s(k))=wWcw(s(1))+wWcw(s(2))=12
  • ROUGE-N ( x ) = ∑ k = 1 K ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( k ) ) ) ∑ k = 1 K ∑ w ∈ W c w ( s ( k ) ) = 5 + 4 6 + 6 = 9 12 = 0.75 \text{ROUGE-N}(\mathbf{x}) = \frac{\sum_{k=1}^{K} \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(k)}))}{\sum_{k=1}^{K} \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(k))}}=\frac{5+4}{6+6}=\frac{9}{12}=0.75 ROUGE-N(x)=k=1KwWcw(s(k))k=1KwWmin(cw(x),cw(s(k)))=6+65+4=129=0.75

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 is, is on, on the, the mat, the bird, bird sat, sat on, the bush  \mathcal{W}=\text{ {the cat, cat is, is on, on the, the mat, the bird, bird sat, sat on, the bush }} W= the cat, cat is, is on, on the, the mat, the bird, bird sat, sat on, the bush 
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)) min ⁡ ( c w ( x ) , c w ( s ( 1 ) ) \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(1)}) min(cw(x),cw(s(1)) min ⁡ ( c w ( x ) , c w ( s ( 2 ) ) \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(2)}) min(cw(x),cw(s(2))
the cat11010
cat is01000
is on01000
on the11111
the mat11000
the bird00100
bird sat00100
sat on10111
the bush00100
  • 分子 ∑ k = 1 K ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( k ) ) ) \sum_{k=1}^{K} \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(k)})) k=1KwWmin(cw(x),cw(s(k)))
    • ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( 1 ) ) = 3 \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(1)})=3 wWmin(cw(x),cw(s(1))=3
    • ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( 2 ) ) = 2 \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(2)})=2 wWmin(cw(x),cw(s(2))=2
    • ∑ k = 1 2 ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( k ) ) ) = ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( 1 ) ) ) + ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( 2 ) ) ) = 3 + 2 = 5 \sum_{k=1}^{2} \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(k)}))=\sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(1)}))+\sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(2)}))=3+2=5 k=12wWmin(cw(x),cw(s(k)))=wWmin(cw(x),cw(s(1)))+wWmin(cw(x),cw(s(2)))=3+2=5
  • 分母 ∑ k = 1 K ∑ w ∈ W c w ( s ( k ) ) \sum_{k=1}^{K} \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(k)}) k=1KwWcw(s(k))
    • ∑ w ∈ W c w ( s ( 1 ) ) = 5 \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(1))}=5 wWcw(s(1))=5
    • ∑ w ∈ W c w ( s ( 2 ) ) = 5 \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(2)})=5 wWcw(s(2))=5
    • ∑ k = 1 2 ∑ w ∈ W c w ( s ( k ) ) = ∑ w ∈ W c w ( s ( 1 ) ) + ∑ w ∈ W c w ( s ( 2 ) ) = 10 \sum_{k=1}^{2} \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(k)})= \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(1)})+ \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(2)})=10 k=12wWcw(s(k))=wWcw(s(1))+wWcw(s(2))=10
  • ROUGE-N ( x ) = ∑ k = 1 K ∑ w ∈ W min ⁡ ( c w ( x ) , c w ( s ( k ) ) ) ∑ k = 1 K ∑ w ∈ W c w ( s ( k ) ) = 3 + 2 5 + 5 = 5 10 = 0.5 \text{ROUGE-N}(\mathbf{x}) = \frac{\sum_{k=1}^{K} \sum_{w \in \mathcal{W}} \min(c_w(\mathbf{x}), c_w(\mathbf{s}^{(k)}))}{\sum_{k=1}^{K} \sum_{w \in \mathcal{W}} c_w(\mathbf{s}^{(k))}}=\frac{3+2}{5+5}=\frac{5}{10}=0.5 ROUGE-N(x)=k=1KwWcw(s(k))k=1KwWmin(cw(x),cw(s(k)))=5+53+2=105=0.5

3. 程序

main_string = 'the cat sat on the mat'
string1 = 'the cat is on the mat'
string2 = 'the bird sat on the bush'

words = list(set(string1.split(' ')+string2.split(' ')))  # 去除重复元素

total_occurrences, matching_occurrences = 0, 0
for word in words:
    matching_occurrences += min(main_string.count(word), string1.count(word)) + min(main_string.count(word), string2.count(word))
    total_occurrences += string1.count(word) + string2.count(word)

print(matching_occurrences / total_occurrences)

bigrams = []
split1 = string1.split(' ')
for i in range(len(split1) - 1):
    bigrams.append(split1[i] + ' ' + split1[i + 1])

split2 = string2.split(' ')
for i in range(len(split2) - 1):
    bigrams.append(split2[i] + ' ' + split2[i + 1])

bigrams = list(set(bigrams))  # 去除重复元素

total_occurrences, matching_occurrences = 0, 0
for bigram in bigrams:
    matching_occurrences += min(main_string.count(bigram), string1.count(bigram)) + min(main_string.count(bigram), string2.count(bigram))
    total_occurrences += string1.count(bigram) + string2.count(bigram)

print(matching_occurrences / total_occurrences)

输出:

0.75
0.5

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

CSS-计数器 counter-reset、counter-increment、counter-reset

计数器 CSS的计数器通过在 content 上应用 counter() 或 counters()函数来显示计数的。其中计数器是由counter-reset和counter-increment 来进行操作的。 counter-increment 语法 counter-increment参数1:计算器名称 该标识符由不区分大小写的字母 a 到 z&#xf…

Ubuntu 常用命令之 top 命令用法介绍

📑Linux/Ubuntu 常用命令归类整理 top命令是Linux下常用的性能分析工具,可以实时动态地查看系统中各个进程的资源占用状况,类似于Windows的任务管理器。它可以显示系统总的和分区的CPU使用率、内存使用率、交换区使用率、系统负载、进程数、…

微搭低代码密码加密存储

目录 1 加密密码2 密码存入用户表中总结 我们在用户登录注册部分,给出了用户注册的例子。但是有一个问题,密码是在数据库中明文存储的,很容易就被破解。为了提高登录的安全性,我们本次讲解一下如何解决密码的加密存储。 1 加密密码…

FISCO BCOS(十六)多机部署及相关操作

多机部署通常指的是在多台计算机或虚拟机之间搭建一个联合系统,以实现分布式计算或服务的目的。这可以提高系统的可靠性、性能和扩展性。确保虚拟机之间能够互相通信,可以使用静态IP或者动态分配IP的方式,接下来一步步带大家进行操作搭建一条…

限量25台,川崎亮相Ninja ZX-10RR冬季限量款

最近川崎发布了自家ZX-10RR的超级限量版,官方称之为冬季测试版,之前也有一些车型推出过冬季测试版,通常是在年底推出,因为这个时候北半球都是非常寒冷的冬天。 不过这台ZX-10RR冬季测试版,并不仅仅只是限量那么简单&am…

前后端分离下的鸿鹄电子招投标系统:使用Spring Boot、Mybatis、Redis和Layui实现源码与立项流程

在数字化时代,采购管理也正经历着前所未有的变革。全过程数字化采购管理成为了企业追求高效、透明和规范的关键。该系统通过Spring Cloud、Spring Boot2、Mybatis等先进技术,打造了从供应商管理到采购招投标、采购合同、采购执行的全过程数字化管理。通过…

Ubuntu 常用命令之 df 命令用法介绍

📑Linux/Ubuntu 常用命令归类整理 在Ubuntu系统下,df命令是用来查看文件系统的磁盘空间占用情况的。df是disk free的缩写,这个命令可以获取硬盘被占用了多少空间,还有多少空间是可用的,硬盘的挂载点等信息。 df命令的…

AI中的强化学习是怎么做的呢?

1. 什么是强化学习 其他许多机器学习算法中学习器都是学得怎样做,而强化学习(Reinforcement Learning, RL)是在尝试的过程中学习到在特定的情境下选择哪种行动可以得到最大的回报。在很多场景中,当前的行动不仅会影响当前的rewar…

Peter算法小课堂—贪心与二分

太戈编程655题 题目描述: 有n辆车大甩卖,第i辆车售价a[i]元。有m个人带着现金来申请购买,第i个到现场的人带的现金为b[i]元,只能买价格不超过其现金额的车子。你是大卖场总经理,希望将车和买家尽量多地进行一对一配对…

亚马逊推出 Graviton4:具有 536.7 GBps 内存带宽的 96 核 ARM CPU

如今,许多云服务提供商都设计自己的芯片,但亚马逊网络服务 (AWS) 开始领先于竞争对手,目前其子公司 Annapurna Labs 开发的处理器可以与 AMD 和英特尔的处理器竞争。本周,AWS 推出了 Graviton4 SoC,这是一款基于 ARM 的…

通过生成表征的自条件图像生成

文章目录 摘要1、简介2、相关工作3、方法4、结果4.1、设置4.2、无条件类别的生成4.3、无分类器指导4.4、消融实验4.5、计算成本4.6、定性结果 5、讨论 摘要 https://arxiv.org/pdf/2312.03701.pdf 本文提出了表示条件图像生成(Representation-Conditioned Image Ge…

【前缀和】【单调栈】LeetCode2281:巫师的总力量和

作者推荐 map|动态规划|单调栈|LeetCode975:奇偶跳 涉及知识点 单调栈 C算法:前缀和、前缀乘积、前缀异或的原理、源码及测试用例 包括课程视频 题目 作为国王的统治者,你有一支巫师军队听你指挥。 给你一个下标从 0 开始的整数数组 strength &…

LIGA-Stereo:为基于立体 3D 检测器的学习 LiDAR 几何感知表示

论文地址:https://openaccess.thecvf.com/content/ICCV2021/papers/Guo_LIGA-Stereo_Learning_LiDAR_Geometry_Aware_Representations_for_Stereo-Based_3D_Detector_ICCV_2021_paper.pdf 论文代码:https://github.com/xy-guo/LIGA-Stereo 摘要 基于立…

基于MybatisPlus批量高效插入百万条数据

引言 在JAVA程序开发中,对数据库进行大量数据插入是一个常见的操作,作为一个软件开发工程师,大批量的数据处理是日常工作,如何优化插入性能,提升数据处理效率是对大多数工程师的一个重要考验。本文将围绕逐条插入和批…

一分钟学会“沉浸式翻译”插件的安装与使用

一、安装 安装地址:https://immersivetranslate.com/ 选择对应的浏览器进入安装即可 二、简单的翻译使用方法 第一次安装需要先刷新界面才可以达到翻译效果 核心需要修改的地方在以下三个: 第一处:设置翻译服务,免费版的可以直接…

重温经典struts1之自定义类型转换器及注册的两种方式(Servlet,PlugIn)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 前言 Struts的ActionServlet接收用户在浏览器发送的请求,并将用户输入的数据,按照FormBean中定义的数据类型,赋值给FormBean中每个变量&a…

八:爬虫-MySQL基础

一:MySQL数据库基础 1.MySQL数据库介绍 MySQL是一个[关系型数据库管理系统],由瑞典MySQL AB 公司开发,属于 Oracle 旗下产品。MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Rela…

Kafka大数据框架学习攻略:精选Kafka学习资源,助你迅速掌握分布式消息系统!

介绍:Kafka是一个分布式、支持分区、多副本的基于zookeeper协调的分布式消息系统,可以实时处理大量数据和满足各种需求场景。它是一个基于发布/订阅模式的消息队列,主要应用于大数据实时处理领域,支持消息的发布、订阅、消费和处理…