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

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

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

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


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}

相关推荐
jndingxin30 分钟前
OpenCV运动分析和目标跟踪(2)累积操作函数accumulateSquare()的使用
人工智能·opencv·目标跟踪
火眼998841 分钟前
《使用 LangChain 进行大模型应用开发》学习笔记(四)
人工智能·笔记·langchain·ai编程
小草cys1 小时前
GPT-4o在matlab编程中性能较好,与智谱清言相比
人工智能
罗小罗同学1 小时前
困扰霍金和蔡磊等人的渐冻症,能否在医学AI领域寻找到下一个解决方案?|个人观点·24-09-22
数据库·人工智能·医学图像处理·医学人工智能·渐冻症
kolaseen1 小时前
tensorflow同步机制
人工智能·python·深度学习·机器学习·tensorflow
waterHBO1 小时前
TensorFlow 笔记
人工智能·笔记·tensorflow
橙子小哥的代码世界1 小时前
深度学习03-神经网络02-激活函数
人工智能·深度学习·神经网络
专家大圣2 小时前
YOLOv10:深度剖析与应用前景展望
人工智能·yolo·目标跟踪
lizi888882 小时前
AiAutoPrediction足球网与泊松分布足球预测比赛模型介绍
人工智能
明志刘明2 小时前
昇思量子计算系列教程-Grover搜索算法
人工智能·深度学习·量子计算