Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate trained spaCy version 3 model?

I want to evaluate my trained spaCy model with the build-in Scorer function with this code:

def evaluate(ner_model, examples):
    scorer = Scorer()
    for input_, annot in examples:
        text = nlp.make_doc(input_)
        gold = Example.from_dict(text, annot)
        pred_value = ner_model(input_)
        scorer.score(gold)
    return scorer.scores

examples = [('Brief discussion about instument replcement and Product ...confirmation', {'entities': [(48, 55, 'PRODUCT')]})('Met with special chem lead. Did not yet move assays from immulite to produc. Follow up with PhD tomorrow.', {'entities': [(57, 68, 'PRODUCT'), (45, 51, 'DATE'), (97, 105, 'DATE')]}), ('Discuss new products for ...', {'entities': [(36, 51, 'PRODUCT')]})]

ner_model = spacy.load(r'D:\temp\model') # for spaCy's pretrained use 'en_core_web_sm'
results = evaluate(ner_model, examples)

When I run the function I'm receiving the following error message:

TypeError: [E978] The Tokenizer.score method takes a list of Example objects, but got: <class 'spacy.training.example.Example'>

I already tried feeding in the annotations like {"entities": annot} and some other versions of it. I checked google but every article seems to be related to version 2.xx of spaCy.

What am I doing wrong? How can I calculate recall, accuracy, and the F1 score with spacy Score()?

like image 811
Malte Avatar asked Oct 16 '25 15:10

Malte


1 Answers

You should use the evaluate command line mode.

spacy evaluate my-model/ test.spacy

Where test.spacy is your test data.

Also, about this error:

TypeError: [E978] The Tokenizer.score method takes a list of Example objects, but got: <class 'spacy.training.example.Example'>

As this error indicates, you are supposed to pass a list of Examples, but you passed just one Example. You should be using scorer.score([gold]), basically.

like image 78
polm23 Avatar answered Oct 18 '25 08:10

polm23



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!