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

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

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

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


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}

相关推荐
我登哥MVP几秒前
使用 Reasonix 接入 DeepSeek:从零搭建你的 AI 编程助手
人工智能·agent
实在智能RPA1 分钟前
培训Agent如何根据岗位自动发放计划?——2026企业级智能自动化实战指南
运维·服务器·人工智能·ai·自动化
watersink2 分钟前
Data-Juicer 整体代码解析
人工智能
天青色等烟雨..5 分钟前
AI大语言模型助力高效办公、论文与项目撰写、数据分析、机器学习与深度学习建模等深度科研
人工智能·语言模型·数据分析
ZorChi5 分钟前
AI API 调用优化实战:统一入口与超时处理指南
人工智能·aigc·接口·api·agent·token·中转站
宝贝儿好7 分钟前
【NLP】第八章:项目实操案例:文本情感分析
人工智能·python·深度学习·算法·自然语言处理
智慧景区与市集主理人8 分钟前
巨有科技一机游平台搭建|全域资源整合,打造目的地智慧文旅核心底座
人工智能·科技
jinxindeep8 分钟前
WorldArena 2.0:迈向多模态、交互式与跨平台的世界模型评测新范式
人工智能
美团技术团队9 分钟前
报名|ACL‘26 美团中稿精选:从能力评测到推理优化,构建生成新范式
人工智能
段一凡-华北理工大学11 分钟前
工业领域的Hadoop架构学习~系列文章19:能源行业Hadoop应用实践
大数据·人工智能·hadoop·分布式·学习·架构·高炉炼铁