Hugging Face evaluate
库的常用方法和属性
evaluate
是 Hugging Face 提供的 独立评估库,用于 计算 NLP 任务的标准评估指标,如 准确率(accuracy)、F1 分数(f1)、BLEU、ROUGE,适用于 文本分类、机器翻译、摘要、问答等任务。
1. evaluate
的常见方法
在 evaluate
中,常见方法包括:
方法 | 作用 |
---|---|
evaluate.list_evaluation_modules() | 获取所有支持的评估指标 |
evaluate.load(metric_name) | 加载评估指标 |
metric.compute(predictions, references) | 计算评估指标 |
metric.add(prediction, reference) | 向指标累积新数据 |
metric.add_batch(predictions, references) | 向指标批量累积数据 |
metric.save(path) | 保存评估结果 |
metric.load(path) | 加载保存的评估结果 |
2. evaluate
的常见属性
属性 | 作用 |
---|---|
metric.name | 评估指标名称 |
metric.description | 评估指标描述 |
metric.inputs_description | 输入参数说明 |
3. evaluate
详细用法
3.1. 获取所有支持的评估指标
import evaluate
metrics_list = evaluate.list_evaluation_modules()
print(metrics_list)
示例输出
['accuracy', 'bleu', 'rouge', 'f1', 'precision', 'recall', 'perplexity']
3.2. 加载 accuracy
(准确率)
metric = evaluate.load("accuracy")
print(metric.name) # 'accuracy'
print(metric.description) # 'Accuracy is the fraction of predictions that are correct.'
3.3. 计算 accuracy
predictions = [1, 0, 1, 1, 0]
references = [1, 0, 0, 1, 0]
result = metric.compute(predictions=predictions, references=references)
print(result)
输出
{'accuracy': 0.8}
0.8
代表 80%
的预测正确。
3.4. 计算 f1
(F1 分数)
f1_metric = evaluate.load("f1")
predictions = [1, 0, 1, 1, 0]
references = [1, 0, 0, 1, 0]
result = f1_metric.compute(predictions=predictions, references=references)
print(result)
示例输出
{'f1': 0.75}
3.5. 计算 bleu
(机器翻译 BLEU 分数)
bleu_metric = evaluate.load("bleu")
predictions = [["the", "cat", "is", "on", "the", "mat"]]
references = [[["the", "cat", "is", "on", "mat"]]]
result = bleu_metric.compute(predictions=predictions, references=references)
print(result)
示例输出
{'bleu': 0.759}
3.6. 计算 rouge
(摘要 ROUGE 分数)
rouge_metric = evaluate.load("rouge")
predictions = ["The cat is on the mat."]
references = ["The cat is sitting on the mat."]
result = rouge_metric.compute(predictions=predictions, references=references)
print(result)
示例输出
{'rouge1': 0.8, 'rouge2': 0.5, 'rougeL': 0.75}
3.7. 累积数据计算(add
和 add_batch
)
如果数据量较大,可以逐步累积数据:
metric = evaluate.load("accuracy")
metric.add(prediction=1, reference=1)
metric.add(prediction=0, reference=1)
result = metric.compute()
print(result) # {'accuracy': 0.5}
批量添加
metric.add_batch(predictions=[1, 0, 1], references=[1, 0, 0])
result = metric.compute()
print(result) # {'accuracy': 0.75}
3.8. 保存和加载评估结果
保存
metric.save("accuracy_results.json")
加载
loaded_metric = evaluate.load("accuracy_results.json")
print(loaded_metric.compute())
4. evaluate
在 Trainer
训练中的应用
如果使用 Trainer
训练,可以定义 compute_metrics
函数:
import numpy as np
from transformers import Trainer
metric = evaluate.load("accuracy")
def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=-1)
return metric.compute(predictions=predictions, references=labels)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
compute_metrics=compute_metrics
)
5. 总结
evaluate
是 Hugging Face 提供的 独立评估库,适用于 文本分类、翻译、摘要等任务。
常见方法:
evaluate.list_evaluation_modules()
获取所有指标evaluate.load(metric_name)
加载指标metric.compute(predictions, references)
计算指标metric.add(prediction, reference)
累积数据metric.save(path)
保存结果
常见指标:
accuracy
准确率f1
F1 分数bleu
机器翻译rouge
摘要perplexity
语言模型