Sub topic 9
Transformer-Based spaCy Pipelines
This chapter assumes you know Python, spaCy Doc and Span objects, and the basic idea of token classification. You should also understand that a transformer can create context-aware representations for each token.
What a Transformer Adds to spaCy
A standard spaCy pipeline can use a statistical named entity recognizer trained on word and linguistic features. A transformer-based pipeline replaces or supplements those representations with output from a pretrained transformer encoder.
A transformer encoder reads the surrounding text when representing each token. For example, it can represent “Apple” differently in these sentences:
- “Apple released a new phone.”
- “I ate an apple.”
The encoder produces contextual vectors. spaCy passes those vectors to the ner component, which predicts entity labels and spans. The rest of the spaCy interface stays familiar: you still process text into a Doc and inspect doc.ents.
The transformer and NER components must agree about token alignment. spaCy maps transformer subword outputs back to spaCy tokens so that the NER component can make predictions on the correct text units.
Installing and Running the Transformer Pipeline
The English transformer pipeline is distributed separately from spaCy. Install the pipeline with:
python -m pip install spacy spacy-transformers
python -m spacy download en_core_web_trfThe following example uses the same Doc and Span interface as a smaller pretrained model:
import spacy
nlp = spacy.load("en_core_web_trf")
doc = nlp("Satya Nadella leads Microsoft from Redmond.")
for ent in doc.ents:
print(ent.text, ent.label_, ent.start_char, ent.end_char)A typical result includes Satya Nadella as a PERSON, Microsoft as an ORG, and Redmond as a GPE. Exact predictions depend on the pipeline version.
The pipeline contains a transformer component and an entity recognizer. You can inspect the component names:
print(nlp.pipe_names)You can also use the transformer pipeline for other spaCy components, such as part-of-speech tagging or dependency parsing, when the selected model provides those components.
Trade-Offs and Failure Modes
Transformer pipelines usually need more memory and take longer per document than smaller statistical pipelines. They can also be harder to deploy in a low-latency service. In return, they often perform better on difficult language, longer context, and domain-specific phrasing.
| Choice | Strength | Cost |
|---|---|---|
| Small statistical spaCy model | Fast and lightweight | Less contextual understanding |
| Transformer-based spaCy model | Stronger contextual representations | More memory and slower inference |
| Custom transformer pipeline | Adaptable to your labels and domain | Requires labelled data and training |
A transformer does not guarantee correct entities. It may still miss an unfamiliar product name, confuse an organization with a location, or fail on text unlike its training data. Always evaluate it on examples from your own application.
If latency matters, process texts in batches with nlp.pipe rather than calling nlp repeatedly:
texts = [
"Microsoft opened an office in Dublin.",
"Ada Lovelace worked in London.",
]
for doc in nlp.pipe(texts, batch_size=2):
print([(ent.text, ent.label_) for ent in doc.ents])Exercise
Why might you choose a smaller spaCy model instead of en_core_web_trf for a production service?
Show answer
The smaller model generally uses less memory and runs faster, which can matter more than the transformer's accuracy when latency or hardware is limited.