【Hugging Face】使用本地qwen2模型计算文本的mauve指标

参考官方教程:MAUVE - a Hugging Face Space by evaluate-metric

This metrics is a wrapper around the official implementation of MAUVE: https://github.com/krishnap25/mauve

所以还需要 pip install mauve-text

加载

首先加载 mauve

load需要注意的事项见:这篇文章

from evaluate import load
import pandas as pd

mauve = load('./metrics/mauve.py')

准备数据

构建数据,这个指标分别需要机器生成和人类生成的文字

predictions = ["我有信心完成论文,即使遇到困难也会坚持不懈,同时相信自己能够找到解决问题的方法。",
               "当我遇到学习上的挑战时,我会相信自己能找到解决问题的方法,并能保持冷静应对。",
               "当家里有突发情况时,我有信心妥善处理,因为我相信自己的解决问题能力。"]
references = ["....",
             "...",
              "..."]

计算mauve

mauve_results = mauve.compute(predictions=predictions, references=references,
                              featurize_model_name='/mnt/workspace/model/Qwen2-1.5B',
                              device_id=0)
print(mauve_results.mauve)

mauve.compute的参数

  • device_id: 使用的GPU的id,我设置为0
  • featurize_model_name: 模型名称,原作者只允许使用gpt2: gpt2, gpt2-medium, gpt2-large, gpt2-xl

如果想要使用别的模型,例如我传入了 /mnt/workspace/model/Qwen2-1.5B,就会发生报错

(ques) root@dsw-582560-844c7b5497-wtchp:/mnt/workspace/evaluation# python calculate_mavue.pyLoading tokenizer
Traceback (most recent call last):
  File "/mnt/workspace/evaluation/calculate_mavue.py", line 6, in <module>
    mauve_results = mauve.compute(predictions=predictions, references=references,
  File "/root/anaconda3/envs/ques/lib/python3.9/site-packages/evaluate/module.py", line 467, in compute
    output = self._compute(**inputs, **compute_kwargs)
  File "/root/.cache/huggingface/modules/evaluate_modules/metrics/mauve/b87f25fdd9447734bf0651854506123cb4ecc7820a56cf1eb7b9e41950e9a83b/mauve.py", line 136, in _compute
    out = compute_mauve(
  File "/root/anaconda3/envs/ques/lib/python3.9/site-packages/mauve/compute_mauve.py", line 92, in compute_mauve
    p_features = get_features_from_input(
  File "/root/anaconda3/envs/ques/lib/python3.9/site-packages/mauve/compute_mauve.py", line 172, in get_features_from_input
    TOKENIZER = get_tokenizer(featurize_model_name)
  File "/root/anaconda3/envs/ques/lib/python3.9/site-packages/mauve/utils.py", line 38, in get_tokenizer
    raise ValueError(f'Unknown model: {model_name}')
ValueError: Unknown model: /mnt/workspace/model/Qwen2-1.5B

解决方法如下:

前往报错地点 /root/anaconda3/envs/ques/lib/python3.9/site-packages/mauve/utils.py,在get_modelget_tokenize加入你想要的模型。我把代码改成了下面的样子,加入了 'Qwen2' in model_name 的条件判断,尽量不修改源代码。

def get_model(model_name, tokenizer, device_id):
    device = get_device_from_arg(device_id)
    if 'gpt2' in model_name or "bert" in model_name:
        model = AutoModel.from_pretrained(model_name, pad_token_id=tokenizer.eos_token_id).to(device)
        model = model.eval()
    else:
        if 'Qwen2' in model_name:
            model = AutoModel.from_pretrained(model_name, pad_token_id=151643).to(device)
            model = model.eval()
        else:
            raise ValueError(f'Unknown model: {model_name}')
    return model

def get_tokenizer(model_name='gpt2'):
    if 'gpt2' in model_name or "bert" in model_name or "Qwen2" in model_name:
        tokenizer = AutoTokenizer.from_pretrained(model_name)
    else:
        raise ValueError(f'Unknown model: {model_name}')
    return tokenizer

最终运行成功

在这里插入图片描述

这个指标的计算还需要一些超参数的设定,也需要大量的输入文本,本次实践只是尝试运行,后续还需进一步的调整。详见:krishnap25/mauve: Package to compute Mauve, a similarity score between neural text and human text)

### Mauve 评估指标的相关信息 Mauve 是一种用于衡量神经文本与人类文本之间差异的自动化评估工具[^5]。其核心理念在于通过计算两种文本分布之间的 Kullback-Leibler (KL) 散度,在大型语言模型的嵌入空间中量化二者的相似程度。该方法能够有效捕捉到不同规模的语言模型以及解码算法所产生的质量差距。 #### 工具特点 Mauve 基于 PyTorch 和 HuggingFace Transformers 构建,提供了高度可扩展的功能支持。它的主要优势体现在以下几个方面: - **多维分析能力**:不仅关注单一方面的质量评估,还能综合考虑多个维度上的表现。 - **高质量研究背景**:源自 NeurIPS 2021 杰出论文奖的研究成果,具备坚实的理论基础和技术保障。 #### 计算方式概述 Mauve 的基本原理涉及以下步骤: 1. 将输入文本映射至预定义的嵌入向量表示形式; 2. 利用 KL 散度测量生成文本的概率密度函数相对于真实人类书写样本的变化情况; 3. 输出最终得分以反映二者间的一致性水平。 具体实现上,可以通过安装 `mauve` 库并调用相应 API 接口完成操作: ```python from mauve import compute_mauve # 加载生成文本和参考文本列表 generated_texts = [...] # 替换为实际生成的数据集 reference_texts = [...] # 替换为目标参照数据集 p, q = generated_texts, reference_texts out = compute_mauve(p_text=p, q_text=q, device_id=0, max_text_length=512) print(f"Mauve Score: {out.mauve}") ``` 上述代码片段展示了如何便捷地获取指定条件下的 Mauve 得分值。其中参数设置需依据具体情况调整优化。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值