计算转换器结果指标的最佳方法是什么?

What is the best way to compute metrics for the transformers results?

这是 NER 的拥抱脸变换器的简单示例:

from transformers import AutoTokenizer, AutoModelForTokenClassification
from transformers import pipeline

tokenizer = AutoTokenizer.from_pretrained("dslim/bert-large-NER")
model = AutoModelForTokenClassification.from_pretrained("dslim/bert-large-NER")

nlp = pipeline("ner", model=model, tokenizer=tokenizer)
example = "My name is jonathan davis and I live in Chicago, Illinois"

ner_results = nlp(example)
print(ner_results)

    output: [{'entity': 'B-PER', 'score': 0.95571744, 'index': 4, 'word': 'j', 'start': 11, 'end':
 12}, {'entity': 'B-PER', 'score': 0.6131773, 'index': 5, 'word': '##ona', 'start': 12, 'end': 
15}, {'entity': 'I-PER', 'score': 0.6707376, 'index': 6, 'word': '##than', 'start': 15, 'end':
 19}, {'entity': 'I-PER', 'score': 0.97754997, 'index': 7, 'word': 'da', 'start': 20, 'end': 22},
 {'entity': 'I-PER', 'score': 0.4608973, 'index': 8, 'word': '##vis', 'start': 22, 'end': 25}, 
{'entity': 'B-LOC', 'score': 0.9990302, 'index': 13, 'word': 'Chicago', 'start': 40, 'end': 47}]

例如我有关于我的句子的信息:

jonathan davis - PER
Chicago - LOC
Illinois - LOC (The model did not recognize this entity)

如何正确计算 PrecisionRecall,假设我的数据拆分如下:

j, ##ona, ##than

在此之前,我使用正则表达式并使用了一个度量,其要点在article中有所描述。但是不知道适不适合这个任务

请帮我找到计算指标的正确方法。也许我错过了 hugging-face 中的一些内置功能?

我认为您使用的模型 (dslim/bert-large-NER) 有问题。根据文件,他们引入了一个名为 aggregation_strategy 的参数,目的完全相同 (full explanation)。

但是由于某种原因,这在这里不能正常工作。现在有两个快速修复选项

FIRST:将模型更改为工作正常的模型。

from transformers import AutoTokenizer, AutoModelForTokenClassification
tokenizer = AutoTokenizer.from_pretrained("Jean-Baptiste/camembert-ner")
model = AutoModelForTokenClassification.from_pretrained("Jean-Baptiste/camembert-ner")
from transformers import pipeline
nlp = pipeline('ner', model=model, tokenizer=tokenizer, aggregation_strategy="simple")
sequence = "My name is jonathan davis and I live in Chicago, Illinois"
nlp(sequence)

输出:

[{'end': 25,
  'entity_group': 'PER',
  'score': 0.9983611,
  'start': 10,
  'word': 'jonathan davis'},
 {'end': 47,
  'entity_group': 'LOC',
  'score': 0.9982808,
  'start': 39,
  'word': 'Chicago'},
 {'end': 57,
  'entity_group': 'LOC',
  'score': 0.99840826,
  'start': 48,
  'word': 'Illinois'}]

SECOND 将输出转换为更舒适的格式以完成其余过程(可能借助状态机)。