常见文本生成评价指标——机器学习评估指标

接着常见文本生成评价指标------非训练评价指标写的。

还是看名字,这类指标是基于机器学习模型评估机器生成的文本之间或机器生成的文本自然语言文本之间的相似性。

所谓的基于机器学习模型是一些利用训练好的模型来评估生成文本的质量和相似性的方法。这些指标通常比基于词重叠或距离的指标更能反映语义和结构的复杂性。


bleurt

出自:2004.04696 BLEURT: Learning Robust Metrics for Text Generation (arxiv.org)

它利用预训练的BERT模型来训练一个回归模型,预测人类评分。它使用了大量的合成数据来进行预训练,以提高模型的泛化能力。它还可以适应不同的评价数据集,通过在特定的数据集上进行微调。BLEURT 的优点是它可以捕捉到语义和语法的差异,同时也能处理多样性和长度的问题。

pip install evaluate

pip install datasets==2.10.0

注意这里一定要确保datasets是2.10.0,因为之后的版本DownloadConfig出问题,会报错无法使用bleurt。

pip install git+https://github.com/google-research/bleurt.git

py 复制代码
import evaluate
import datasets
predictions = ["It is a guide to action which ensures that the military always obeys the commands of the party."]
references = ["It is a guide to action that ensures that the military will forever heed Party commands."]
bleurt = evaluate.load("bleurt", module_type="metric")
results = bleurt.compute(predictions=predictions, references=references)
print(results)

输出:

{'scores': 0.5508140325546265}

BERTScore

出自:BERTSCORE: EVALUATING TEXT GENERATION WITH BERT (openreview.net)

BERTScore使用预训练的BERT模型来评估生成文本与参考文本之间的相似性。它是一种广泛用于文本比较任务的评估指标。

BERTScore利用了BERT模型的预训练上下文嵌入,并通过余弦相似度匹配候选句子和参考句子中的单词。首先对生成文本和参考文本进行嵌入表示,然后使用BERT模型计算这些嵌入表示之间的相似性分数。这些分数反映了两个文本之间的语义相似性。

计算结果为:

  • precision(精确度):对于预测和参考列表中的每个句子,其精确度值范围从0.0到1.0。

  • recall(召回率):对于预测和参考列表中的每个句子,其召回率值范围从0.0到1.0。

  • f1(F1分数):对于预测和参考列表中的每个句子,其F1分数值范围从0.0到1.0。

给定参考句子 𝑥 𝑥 x和候选句子 x ^ \hat x x^的上下文嵌入,即 x , x ^ \mathbf x, \hat{\mathbf x} x,x^,可以通过以下方式计算召回率、精确度和F1得分:
R B E R T = 1 ∣ x ∣ ∑ x i ∈ x max ⁡ x ^ j ∈ x ^ x i ⊤ x ^ j , P B E R T = 1 ∣ x ^ ∣ ∑ x ^ j ∈ x ^ max ⁡ x i ∈ x x i ⊤ x ^ j , F B E R T = 2 P B E R T ⋅ R B E R T P B E R T + R B E R T , \begin{aligned} R_{\mathrm{BERT}} & =\frac{1}{|x|} \sum_{x_i \in x} \max _{\hat{x}_j \in \hat{x}} \mathbf{x}i^{\top} \hat{\mathbf{x}}j, \\ P{\mathrm{BERT}} & =\frac{1}{|\hat{x}|} \sum{\hat{x}j \in \hat{x}} \max {\boldsymbol{x}i \in x} \mathbf{x}i^{\top} \hat{\mathbf{x}}j, \\ F{\mathrm{BERT}} & =2 \frac{P{\mathrm{BERT}} \cdot R{\mathrm{BERT}}}{P{\mathrm{BERT}}+R{\mathrm{BERT}}}, \end{aligned} RBERTPBERTFBERT=∣x∣1xi∈x∑x^j∈x^maxxi⊤x^j,=∣x^∣1x^j∈x^∑xi∈xmaxxi⊤x^j,=2PBERT+RBERTPBERT⋅RBERT,

用distilbert-base-uncased计算一下BERTScore,代码如下:

pip install evaluate

pip install bert_score

py 复制代码
import evaluate
predictions = ["It is a guide to action which ensures that the military always obeys the commands of the party."]
references = ["It is a guide to action that ensures that the military will forever heed Party commands."]
bertscore = evaluate.load("bertscore")
results = bertscore.compute(predictions=predictions, references=references, model_type="distilbert-base-uncased")
print(results)

输出:

{'precision': 0.9385131001472473, 'recall': 0.9496503472328186, 'f1': 0.9440488815307617, 'hashcode': 'distilbert-base-uncased_L5_no-idf_version=0.3.12(hug_trans=4.33.2)'}

最后那个hashcode不用于评估文本生成质量,而是用于标识或管理库的版本。

Perplexity

在这里再加一个Perplexity。Perplexity衡量了模型生成输入文本序列的可能性有多高。简单说就是你给他一个模型,再给他一个句子,他会计算出这个模型输出这个句子的可能性。

困惑度定义为序列的指数平均负对数似然。

对于一个序列 X = ( x 0 , x 1 , ... , x t ) X=\left(x_0, x_1, \ldots, x_t\right) X=(x0,x1,...,xt):
PPL ⁡ ( X ) = exp ⁡ { − 1 t ∑ i t log ⁡ p θ ( x i ∣ x < i ) } \operatorname{PPL}(X)=\exp \left\{-\frac{1}{t} \sum_i^t \log p_\theta\left(x_i \mid x_{<i}\right)\right\} PPL(X)=exp{−t1i∑tlogpθ(xi∣x<i)}

其中 log ⁡ p θ ( x i ∣ x < i ) \log p_\theta\left(x_i \mid x_{<i}\right) logpθ(xi∣x<i) 是我们的模型在 x < i x_{<i} x<i基础上生成第i个token的对数似然。

困惑度可以被看作是评估模型在语料库中一组指定的标记中均匀预测能力的指标。注意,不同的分词过程直接影响了模型的困惑度,因此在比较不同模型时,应始终考虑这一点。

pip install evaluate

py 复制代码
import evaluate
predictions = ["It is a guide to action which ensures that the military always obeys the commands of the party."]
references = ["It is a guide to action that ensures that the military will forever heed Party commands."]
ppl = evaluate.load("perplexity", module_type="metric")
results = ppl.compute(predictions=predictions, model_id="gpt2")
print(results)

输出:

{'perplexities': 58.777610778808594, 'mean_perplexity': 58.777610778808594}

相关推荐
回眸&啤酒鸭5 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智5 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
AI的探索之旅5 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein5 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu5 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
美狐美颜SDK开放平台5 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
wukangjupingbb5 天前
智能网联汽车安全能力框架
人工智能
龙亘川5 天前
明月照湾区,智启新赛道:从顶流文旅IP盛会看智慧文旅升级路径
人工智能·智慧城市·开源软件·数据可视化
飞猫的边缘AI5 天前
边缘AI应用:家用AI摄像头怎么做数据训练?
人工智能·边缘计算·ai算法·边缘ai