我不再寻找答案,因为我想通了。
以下链接的讨论:
https://github.com/explosion/spaCy/discussions/8178
spacy 3 NER Scorer() 抛出 TypeError: score() 接受 2 个位置参数,但给出了 3 个
非常有用,我能够编写以下代码(以防阅读当前讨论的任何人可能仍然难以进行转换):
# TRAIN_DATA format
TRAIN_DATA = [
("Who was Hamlet?", [(8,14,'PER')]),
("Have you ever met Rome?", [(18,22,'LOC')])
]
import spacy
from spacy.tokens import Span
from spacy import displacy
from spacy.training import *
from spacy.scorer import Scorer
from spacy.util import minibatch, compounding
nlp = spacy.load("./output/model-best")
# evaluate function
def evaluate(ner_model, testing_data):
scorer = Scorer()
examples = []
for input_, annot in testing_data:
doc_gold_text = ner_model.make_doc(input_)
example = Example.from_dict(doc_gold_text, {"entities": annot})
example.predicted = ner_model(input_)
examples.append(example)
return scorer.score(examples)
# print the results
print(evaluate(nlp, TRAIN_DATA))
输出格式如下:
{'token_acc': 1.0, 'token_p': 1.0, 'token_r': 1.0, 'token_f': 1.0, 'sents_p': 1.0, 'sents_r': 1.0, 'sents_f': 1.0, 'tag_acc': None, 'pos_acc': None, 'morph_acc': None, 'morph_per_feat': None, 'dep_uas': None, 'dep_las': None, 'dep_las_per_type': None, 'ents_p': 0.9833333333333333, 'ents_r': 0.9365079365079365, 'ents_f': 0.9593495934959351, 'ents_per_type': {'LAW': {'p': 1.0, 'r': 1.0, 'f': 1.0}, 'LOC': {'p': 1.0, 'r': 0.6666666666666666, 'f': 0.8}, 'ORG': {'p': 0.9565217391304348, 'r': 0.9565217391304348, 'f': 0.9565217391304348}, 'TIME': {'p': 1.0, 'r': 1.0, 'f': 1.0}, 'PER': {'p': 1.0, 'r': 1.0, 'f': 1.0}}, 'cats_score': 0.0, 'cats_score_desc': 'macro F', 'cats_micro_p': 0.0, 'cats_micro_r': 0.0, 'cats_micro_f': 0.0, 'cats_macro_p': 0.0, 'cats_macro_r': 0.0, 'cats_macro_f': 0.0, 'cats_macro_auc': 0.0, 'cats_f_per_type': {}, 'cats_auc_per_type': {}}
如果有任何问题,请随时提及。