How to create a custom spaPcy pipeline component using the Thinc model

Viewed 81

I'd like to create a custom pipeline component in spaCy which uses a pre-trained Thinc model. I'd like to modify the output prediction from Thinc and then pass the modified value back into the pipeline i.e. effectively modifying the ner pipeline component.

SpaCy pipeline

I was thinking of doing this via a custom pipeline component, something like:

from spacy.language import Language

@Language.component("my_ner")
def my_ner(doc):

    class_probabilities = thinc_do_something(data, model, num_samples)
    class_value = np.argmax(class_probabilities, axis=1)
    
    return doc

nlp = spacy.load("en_core_web_sm", exclude=["ner"])
nlp.add_pipe("my_ner", after="parser")  # Insert after the parser
print(nlp.pipe_names)  # ['tagger', 'parser', 'my_ner']
doc = nlp("This is a sentence.")

My aim is for the pipe to run as per the original ner component, but with my custom ner component modifying the class probabilities. Unfortunately I don't understand from the spaCy documentation:

  • How to access the pre trained model from inside the pipeline?
  • How to access the data used for the model prediction within the pipeline?
  • Where I need to write the model predicted value back to as part of my modified ner pipline?
  • Is there a better way of doing this?
1 Answers

I have not heard of anyone doing something like that before, and while it is possible, it is not as simple as you suggest. The example component you have is for simple stateless components that are just a function. In order to modify how a trainable pipe works you'd have to make your own pipe, by subclassing an existing one or otherwise.

You should look at existing pipes for reference, the textcat is probably one of the simpler ones. For trainable pipes, when used in a pipeline they basically use predict and set_annotations, as shown in the TrainablePipe implementation.

Rather than subclassing, it might also be easier to just copy the component you want to use, modify a few bits, and give it a new name.

Related