【问题标题】:What is the best way to compute metrics for the transformers results?计算变压器结果指标的最佳方法是什么?
【发布时间】:2023-03-23 07:19:02
【问题描述】:

以下是 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)

鉴于我的数据按如下方式拆分,我如何正确计算 Precision 和 Recall:

j, ##ona, ##than

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

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

【问题讨论】:

    标签: python nlp huggingface-transformers


    【解决方案1】:

    在我看来,您使用的模型 (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 将输出转换为更舒适的格式以完成其余过程(可能借助状态机)。

    【讨论】:

      猜你喜欢
      • 2013-03-17
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 2015-08-21
      • 1970-01-01
      • 2010-10-21
      • 1970-01-01
      相关资源
      最近更新 更多