I am working on training a NER, and am interested in getting confidence scores for the predicted entities.
From what I understand, this is only possible by training a spancat component instead of a NER component.
Is this true?
Can I use my training data I annotated using ner recipes in prodigy in order to train a spancat? Do I need to change it in any way?
Also, I read that the default spancat scorer uses the LinearLogistic layer. Since I do not have overlapping entities, how do I change this to softmax?
Actually, you should be fine. Check out this helpful past post that describes.
Yep - that makes sense. For questions like this, the SpaCy discussion forum can be helpful. For example, I found a similar post that suggested the same. As referenced, you could use the softmax from thinc in a custom config. If you have questions, I would suggest replying to that post as the spaCy developer team can help you (this forum is more for Prodigy-specific questions).
Hope this helps and let me know if you have any further questions!
About the "confidence scores" - I am trying to get the predicted scores of the predicted entities so that I will be able to use a custom threshold on the positive predictions of my model (with my trained ner component).
Whenever you iterate on a Doc object (i.e., the output of nlp(eg["text"])), you're iterating on set of Token objects. What you may want to do is something like:
spans_key = "sc" # usually the case unless you set something different
for eg in stream:
prediction = nlp(eg["text"])
for span in predictions.spans[spans_key]:
# do something
From the span variable, you can access all Span attributes. However, If you want to access the scores, you might want to do something like:
spans_key = "sc" # usually the case unless you set something different
scores = prediction.spans[spans_key].attrs["scores"]
Thank you, but unfortunately this still isn't working for me.
I can't seem to find "scores" in the span attributes.
predictions.spans returns an empty dictionary. The recognised entities can be found in prediction.ents, but not the scores.
What am I missing here?
After loading my trained model (custom ner) I am trying to use it on new sentences, and get a list of the entities it recognises in each sentence, with their predicted scores - the softmax activation output of each entity. Is this possible?
predictions.spans returns an empty dictionary. The recognised entities can be found in prediction.ents, but not the scores.
This partially explains why there are no spans detected in your Doc. To clarify, as of now there's no straightforward way to obtain confidence scores in ner. You can only do so via spancat. This means you have to train your model using spancat, not ner.