user47467
user47467

Reputation: 1093

spacy 3 NER Scorer() throws TypeError: score() takes 2 positional arguments but 3 were given

Running into the following error when trying to get scores on my test set using Scorer

TypeError: score() takes 2 positional arguments but 3 were given

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

def evaluate(ner_model, testing_data):
    scorer = Scorer()

    for input_, annot in testing_data:
        doc_gold_text = ner_model.make_doc(input_)
        example = Example.from_dict(doc_gold_text, {"entities": annot})
        pred_value = ner_model(input_)
        
    return scorer.score(pred_value, example)

print(evaluate(nlp_updated, testing_tagged))

Where testing_tagged:

testing_tagged = [
    ("Who was Hamlet?", [(8,14,'PERSON')]),
    ("Have you ever met Rome?", [(18,22,'LOC')])
]

Expected output is where p, r and f are not 0:

{'uas': 0.0, 'las': 0.0, 'las_per_type': {'': {'p': 0.0, 'r': 0.0, 'f': 0.0}}, 'ents_p': 0.0, 'ents_r': 0.0, 'ents_f': 0.0, 'ents_per_type': {'PERSON': {'p': 0.0, 'r': 0.0, 'f': 0.0}, 'LOC': {'p': 0.0, 'r': 0.0, 'f': 0.0}}, 'tags_acc': 0.0, 'token_acc': 100.0, 'textcat_score': 0.0, 'textcats_per_cat': {}}

I initially had this working with the GoldParse function instead of Example.from_dict - but I upgraded to Spacy 3.0.5 and I can't understand why this error is occurring.

Upvotes: 2

Views: 986

Answers (1)

Sofie VL
Sofie VL

Reputation: 3096

Since spaCy v3, scorer.score just takes a list of examples. Each Exampleobject holds two Doc objects:

  • one reference doc with the gold-standard annotations, created for example from the given annot dictionary
  • one predicted doc with the predictions. The scorer will then compare the two.

So you want something like this:

from spacy.training import Example
examples = []
for ...:
    example = Example.from_dict(text, {"entities": annot})
    example.predicted = ner_model(example.predicted)
    examples.append(example)
scorer.score(examples)

Upvotes: 3

Related Questions