Evaluate a trained NER pipline on a new Dataset (Label mapping) #10094
-
Hello everyone, Currently, I do: nlp = get_nlp_model(config.language) # load the right mode
docs = read(config.path, config.format) # read the dataset as List[Doc]
texts = [doc.text for doc in docs]
predictions = list(nlp.pipe(texts)) # predict based on text
# map the labels to a common name space by replacing the ents with a list of ents where the .label_ is mapped to the new label
remapped_predictions = list(
map_document(doc, config.backward_label_mapping)
for doc in predictions
)
remapped_docs = list(
map_document(doc, config.forward_label_mapping)
for doc in docs
)
# create examples
examples = [
Example(pred, ref) for pred, ref in zip(remapped_predictions, remapped_docs)
]
spacy_report = language.evaluate(examples) Is this the correct way of doing this? I have the issue that I get this error:
More Contextdef map_span_labels(
spans: List[Span], label_mapping: Dict[str, str]
) -> List[Span]:
"""Re-maps the labels of the spans, according to the label_mapping.
Args:
spans (List[Span]): spans to re-map
label_mapping (Dict[str, str]): mapping from the old or new labels
Returns:
List[Span]: new span
"""
new_spans = []
for span in spans:
new_span = Span(
span.doc, span.start, span.end, label_mapping[span.label_]
)
new_spans.append(new_span)
return new_spans
def map_document(doc: Doc, label_mapping: Dict[str, str]) -> Doc:
"""Changes the labels for each span in the document based on the label_mapping.
Args:
doc (Doc):
label_mapping (Dict[str, str]): mappg from the old or new labels
Returns:
Doc: same document object, but with the labels changed
"""
mapped_ents = map_span_labels(doc.ents, label_mapping)
doc.ents = mapped_ents
return doc |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
Hello, |
Beta Was this translation helpful? Give feedback.
Hello,
Unfortunately, your code doesn't show how the Language object is created and is likely the cause of the error.
You can try to run the scorer directly on the list of examples instead of running
language.evaluate
because this reruns the whole pipeline on the data before scoring. An example of calling the scorer is here: #10056 (comment)