import json from nltk.translate.bleuscore import corpusbleu, SmoothingFunction from…
- User
import json from nltk.translate.bleu_score import corpus_bleu, SmoothingFunction from rouge import Rouge
读取 JSON 文件
with open('/media/dphw/8330688f-4ea4-420d-90fc-1cfd6109a744/yang_liu/多模态古籍大模型/组会/多模态模型简单评测/LLMs输出结果/模型回答_Qwen2-VL-7B.json', 'r') as f: data = json.load(f)
提取模型回答和正确答案
model_answers = [] ground_truths = [] for item in data: if 140 <= int(item['ID']) <= 205: model_answers.append(item['模型回答']) ground_truths.append(item['答案'])
计算所有数据的指标
rouge = Rouge() scores = rouge.get_scores(model_answers, ground_truths, avg=True)
使用 SmoothingFunction 计算 BLEU
smoothing_function = SmoothingFunction().method1 bleu_score = corpus_bleu([[gt.split()] for gt in ground_truths], [ma.split() for ma in model_answers], smoothing_function=smoothing_function)
print("所有数据:") print(f"BLEU: {bleu_score:.4f}") print(f"ROUGE-1: {scores['rouge-1']['f']:.4f}") print(f"ROUGE-2: {scores['rouge-2']['f']:.4f}") print(f"ROUGE-L: {scores['rouge-l']['f']:.4f}")
计算古物类别的指标
ancient_model_answers = [item['模型回答'] for item in data if 140 <= int(item['ID']) <= 169] ancient_ground_truths = [item['答案'] for item in data if 140 <= int(item['ID']) <= 169]
scores = rouge.get_scores(ancient_model_answers, ancient_ground_truths, avg=True) bleu_score = corpus_bleu([[gt.split()] for gt in ancient_ground_truths], [ma.split() for ma in ancient_model_answers], smoothing_function=smoothing_function)
print("\n古物类别:") print(f"BLEU: {bleu_score:.4f}") print(f"ROUGE-1: {scores['rouge-1']['f']:.4f}") print(f"ROUGE-2: {scores['rouge-2']['f']:.4f}") print(f"ROUGE-L: {scores['rouge-l']['f']:.4f}")
计算名画类别的指标
painting_model_answers = [item['模型回答'] for item in data if 170 <= int(item['ID']) <= 205] painting_ground_truths = [item['答案'] for item in data if 170 <= int(item['ID']) <= 205]
scores = rouge.get_scores(painting_model_answers, painting_ground_truths, avg=True) bleu_score = corpus_bleu([[gt.split()] for gt in painting_ground_truths], [ma.split() for ma in painting_model_answers], smoothing_function=smoothing_function)
print("\n名画类别:") print(f"BLEU: {bleu_score:.4f}") print(f"ROUGE-1: {scores['rouge-1']['f']:.4f}") print(f"ROUGE-2: {scores['rouge-2']['f']:.4f}") print(f"ROUGE-L: {scores['rouge-l']['f']:.4f}")
为什么计算结果全部都是0,哪里出问题了
- Assistant
在你的代码中,如果 BLEU 和 ROUGE 指标的结果全部都是 0,可能是以下几个问题的原因:
-
Tokenization 和分词:BLEU 和 ROUGE 都需要准确的分词。如果你的
ground_truths和model_answers中的内容没有正确地分词(例如,中文文本应该按字分割而不是按词),可能会导致计算结果偏低或为 0。你可以尝试使用适当的中文分词工具,比如jieba来进行分词。 -
特殊字符或格式问题:检查你的输入数据是否含有格式问题,例如多余的空格、换行符、标点符号等,这些可能会干扰分词结果。
-
数据匹配问题:确保
model_answers和ground_truths中的内容确实是对应的。如果有数据错位,计算出的结果将会是 0。 -
JSON 文件内容不匹配:验证你的 JSON 文件中数据是否正确,确保你提取的字段是你期望的格式。
-
BLEU 和 ROUGE 实现细节:这些指标的计算对序列非常敏感,确保实现方法(如
smoothing_function的选择)与期望一致。 -
数据格式错误:有时 JSON 数据提取可能会有问题,确保数据加载后是你期待的格式。
可以通过以下步骤进行逐项排查:
加入分词步骤
对于中文文本,可以尝试加入分词步骤,使用
jieba进行分词:python -
- User
给我更新后的代码
- Assistant
- User
nltk.download('wordnet')
一直卡着
- Assistant
在某些情况下,
nltk.download('wordnet')可能会卡住,尤其是在网络连接不稳定或被防火墙阻碍时。你可以尝试以下几种解决方法:方案1: 手动下载
-
手动下载:
- 打开 NLTK data page。
- 找到
WordNet数据集并下载wordnet.zip文件。
-
解压到指定目录:
- 将下载的
wordnet.zip解压缩到一个目录,例如~/nltk_data/corpora/wordnet。具体路径可以根据你的操作系统和需求调整。
- 将下载的
-
设置 NLTK 数据路径:
-
在你的 Python 脚本中,在导入
nltk之前,设置环境变量来指向你的 NLTK 数据路径:pythonimport os os.environ['NLTK_DATA'] = '/path/to/your/nltk_data'
确保替换
'/path/to/your/nltk_data'为你实际放置 NLTK 数据的位置。 -
方案2: 更换下载源
-