huggingface evalute 库

Ivan Lv6

学习huggingface evaluate 库, 用于获取各种计算指标的库. 例如 accuracy, f1, 以及其他常用指标等.

1
pip install evaluate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import evaluate

metric = evaluate.load("accuracy")

# first way
result: {"accuracy": float} = metric.compute(references: list = ..., predictions: list = ...)

# second way
predictions: list = ...
references: list = ...

for pred, reference in zip(predictions, references):
metric.add(pred, reference)
result: {"accuracy": float} = metric.compute()

# third way
predictions: list[list] = ... # (batch_size, predictions)
references: list[list] = ... # (batch_size, references)

for preds, refers in zip(predictions, references):
metric.add_batch(preds, refers)
result: {"accuracy": float} = metric.compute()

# especially useful when training/evaluating model in batches
for inputs, labels in evaluation_dataset:
predictions = model(inputs)
metric.add_batch(references=labels, predictions=predictions)
result: {"accuracy": float} = metric.compute()

使用 load 方法返回了一个对象, 其中暴露了 compute() 方法用于计算指标. 有两种方式传入数据

  1. 当作函数使用: 直接将标签与预测分别使用 references 和 predictions 传入方法
  2. 当作对象方法使用: 使用 add() , add_batch() 方法传入数据, 然后调用前一个方法来计算.

猜测实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np

def load(metric_name):
return MetricFactory.build(metric_name)

class MetricFactory():
def __init__(self):
pass

def build(self, metric_name):
# choose a corresponding metric
...
return Metric()

class Metric():
def __init__(self):
self.references = []
self.predictions = []

def compute(self, references = null, predictions = null):
if references is None:
references = self.references
if predictions is None:
predictions = predictions

# calculate metric
...
return res

def add(self, references: list, predictions: list):
self.references = references
self.predictions = predictions

def add_batch(self, references: list[list], predictions: list[list]):
self.references = np.array(references).flatten().item()
self.predictions = np.array(predictions).flatten().item()

huggingface 中的 Tokenizer 对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from transformers import AutoTokenizer
from typing import Union

tokenizer = AutoTokenizer.from_pretrained("bert-base")

sentences: str | list[str] = ... # sequence | batch of str (batch_size, sequence_length)
ids: list[int] = tokenizer.encode(sentences) # 如果传入一个字符串列表, 会输出一个展开的ids, 也就是将字符串列表拼接起来

# 推荐使用 __call__
encoded_inputs = tokenizer(sentences)

encoded_inputs: {
"input_ids": list[int] | list[list[int]],
"token_type_ids": list[int] | list[list[int]], # 用于区分不同句子
"attention_mask": list[int] | list[list[int]] # 有效字符串掩码, 用于去除填充字符
}

tokens: str = tokenzier.decode(token_ids: list[int])
  • Title: huggingface evalute 库
  • Author: Ivan
  • Created at : 2025-05-24 00:00:00
  • Updated at : 2026-10-23 18:02:31
  • Link: https://ivanfan-van.github.io/2025/05/24/note-4-evergreen-006/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
huggingface evalute 库