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

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

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

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


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。

给定参考句子 <math xmlns="http://www.w3.org/1998/Math/MathML"> 𝑥 𝑥 </math>x和候选句子 <math xmlns="http://www.w3.org/1998/Math/MathML"> x ^ \hat x </math>x^的上下文嵌入,即 <math xmlns="http://www.w3.org/1998/Math/MathML"> x , x ^ \mathbf x, \hat{\mathbf x} </math>x,x^,可以通过以下方式计算召回率、精确度和F1得分:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> 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} </math>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衡量了模型生成输入文本序列的可能性有多高。简单说就是你给他一个模型,再给他一个句子,他会计算出这个模型输出这个句子的可能性。

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

对于一个序列 <math xmlns="http://www.w3.org/1998/Math/MathML"> X = ( x 0 , x 1 , ... , x t ) X=\left(x_0, x_1, \ldots, x_t\right) </math>X=(x0,x1,...,xt):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> 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\} </math>PPL(X)=exp{−t1i∑tlogpθ(xi∣x<i)}

其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> log ⁡ p θ ( x i ∣ x < i ) \log p_\theta\left(x_i \mid x_{<i}\right) </math>logpθ(xi∣x<i) 是我们的模型在 <math xmlns="http://www.w3.org/1998/Math/MathML"> x < i x_{<i} </math>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}

相关推荐
政安晨19 分钟前
政安晨【零基础玩转开源AI项目】- AutoGPT:全球首个自主AI Agent从入门到实战(致敬OpenClaw的小回顾)
人工智能·ai·autogpt·全球首个agent框架·致敬openclaw之作·参考价值·ai开源agent框架
Shawn_Shawn5 小时前
mcp学习笔记(一)-mcp核心概念梳理
人工智能·llm·mcp
33三 三like7 小时前
《基于知识图谱和智能推荐的养老志愿服务系统》开发日志
人工智能·知识图谱
芝士爱知识a7 小时前
【工具推荐】2026公考App横向评测:粉笔、华图与智蛙面试App功能对比
人工智能·软件推荐·ai教育·结构化面试·公考app·智蛙面试app·公考上岸
腾讯云开发者8 小时前
港科大熊辉|AI时代的职场新坐标——为什么你应该去“数据稀疏“的地方?
人工智能
工程师老罗8 小时前
YoloV1数据集格式转换,VOC XML→YOLOv1张量
xml·人工智能·yolo
Coder_Boy_9 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
啊森要自信9 小时前
CANN ops-cv:面向计算机视觉的 AI 硬件端高效算子库核心架构与开发逻辑
人工智能·计算机视觉·架构·cann
2401_836235869 小时前
中安未来SDK15:以AI之眼,解锁企业档案的数字化基因
人工智能·科技·深度学习·ocr·生活