2020-07-01 14:02:17 +03:00
---
title: What's New in v3.0
teaser: New features, backwards incompatibilities and migration guide
menu:
- ['Summary', 'summary']
- ['New Features', 'features']
- ['Backwards Incompatibilities', 'incompat']
- ['Migrating from v2.x', 'migrating']
---
2020-09-12 18:05:10 +03:00
## Summary {#summary hidden="true"}
2020-07-01 14:02:17 +03:00
2021-02-17 15:42:27 +03:00
> #### 📖 Looking for the old docs?
>
> To help you make the transition from v2.x to v3.0, we've uploaded the old
> website to [**v2.spacy.io**](https://v2.spacy.io/docs).
2021-01-19 11:18:39 +03:00
< Infobox title = "New: Commercial migration support for your spaCy pipelines" variant = "warning" emoji = "📣" >
Want to make the transition from spaCy v2 to spaCy v3 as smooth as possible for
you and your organization? We're now offering commercial **migration support**
for your spaCy pipelines! We've put a lot of work into making it easy to upgrade
your existing code and training workflows – but custom projects may always need
some custom work, especially when it comes to taking advantage of the new
capabilities.
[**Details & application →** ](https://form.typeform.com/to/vMs2zSjM )
< / Infobox >
2020-09-12 18:05:10 +03:00
< Grid cols = {2} gutterBottom = {false} >
2020-08-21 14:22:59 +03:00
< div >
2020-09-12 18:05:10 +03:00
spaCy v3.0 features all new **transformer-based pipelines** that bring spaCy's
accuracy right up to the current **state-of-the-art** . You can use any
pretrained transformer to train your own pipelines, and even share one
transformer between multiple components with **multi-task learning** . Training
is now fully configurable and extensible, and you can define your own custom
models using **PyTorch** , **TensorFlow** and other frameworks. The new spaCy
projects system lets you describe whole **end-to-end workflows** in a single
file, giving you an easy path from prototype to production, and making it easy
to clone and adapt best-practice projects for your own use cases.
2020-08-21 14:22:59 +03:00
< / div >
< Infobox title = "Table of Contents" id = "toc" >
- [Summary ](#summary )
- [New features ](#features )
- [Transformer-based pipelines ](#features-transformers )
2020-09-12 18:05:10 +03:00
- [Training & config system ](#features-training )
2020-08-21 14:22:59 +03:00
- [Custom models ](#features-custom-models )
- [End-to-end project workflows ](#features-projects )
2020-09-13 23:30:33 +03:00
- [Parallel training with Ray ](#features-parallel-training )
2020-08-21 14:22:59 +03:00
- [New built-in components ](#features-pipeline-components )
- [New custom component API ](#features-components )
2020-09-04 17:30:31 +03:00
- [Dependency matching ](#features-dep-matcher )
2020-08-21 14:22:59 +03:00
- [Python type hints ](#features-types )
- [New methods & attributes ](#new-methods )
- [New & updated documentation ](#new-docs )
- [Backwards incompatibilities ](#incompat )
- [Migrating from spaCy v2.x ](#migrating )
< / Infobox >
< / Grid >
2020-07-01 14:02:17 +03:00
## New Features {#features}
2020-09-12 18:05:10 +03:00
This section contains an overview of the most important **new features and
improvements**. The [API docs ](/api ) include additional deprecation notes. New
methods and functions that were introduced in this version are marked with the
tag < Tag variant = "new" > 3< / Tag > .
2020-08-11 21:57:23 +03:00
2021-01-27 04:39:47 +03:00
< YouTube id = "9k_EfV7Cns0" > < / YouTube >
< Grid cols = {2} gutterBottom = {false} narrow >
< YouTube id = "BWhh3r6W-qE" > < / YouTube >
< YouTube id = "8HL-Ap5_Axo" > < / YouTube >
< / Grid >
2020-09-12 18:05:10 +03:00
### Transformer-based pipelines {#features-transformers}
2020-08-11 21:57:23 +03:00
2020-09-12 18:05:10 +03:00
> #### Example
>
> ```cli
> $ python -m spacy download en_core_web_trf
> ```
2020-08-11 21:57:23 +03:00
2020-09-12 18:05:10 +03:00
spaCy v3.0 features all new transformer-based pipelines that bring spaCy's
accuracy right up to the current **state-of-the-art** . You can use any
pretrained transformer to train your own pipelines, and even share one
transformer between multiple components with **multi-task learning** . spaCy's
transformer support interoperates with [PyTorch ](https://pytorch.org ) and the
[HuggingFace `transformers` ](https://huggingface.co/transformers/ ) library,
giving you access to thousands of pretrained models for your pipelines.
2020-08-10 01:01:38 +03:00
2020-08-21 14:22:59 +03:00
![Pipeline components listening to shared embedding component ](../images/tok2vec-listener.svg )
2020-09-12 18:05:10 +03:00
import Benchmarks from 'usage/\_benchmarks-models.md'
< Benchmarks / >
2020-10-15 13:35:30 +03:00
#### New trained transformer-based pipelines {#features-transformers-pipelines}
> #### Notes on model capabilities
>
> The models are each trained with a **single transformer** shared across the
> pipeline, which requires it to be trained on a single corpus. For
> [English](/models/en) and [Chinese](/models/zh), we used the OntoNotes 5
> corpus, which has annotations across several tasks. For [French](/models/fr),
> [Spanish](/models/es) and [German](/models/de), we didn't have a suitable
> corpus that had both syntactic and entity annotations, so the transformer
> models for those languages do not include NER.
| Package | Language | Transformer | Tagger | Parser | NER |
| ------------------------------------------------ | -------- | --------------------------------------------------------------------------------------------- | -----: | -----: | ---: |
2021-02-01 06:17:52 +03:00
| [`en_core_web_trf` ](/models/en#en_core_web_trf ) | English | [`roberta-base` ](https://huggingface.co/roberta-base ) | 97.8 | 95.2 | 89.9 |
2020-10-15 13:35:30 +03:00
| [`de_dep_news_trf` ](/models/de#de_dep_news_trf ) | German | [`bert-base-german-cased` ](https://huggingface.co/bert-base-german-cased ) | 99.0 | 95.8 | - |
| [`es_dep_news_trf` ](/models/es#es_dep_news_trf ) | Spanish | [`bert-base-spanish-wwm-cased` ](https://huggingface.co/dccuchile/bert-base-spanish-wwm-cased ) | 98.2 | 94.6 | - |
2021-02-01 06:17:52 +03:00
| [`fr_dep_news_trf` ](/models/fr#fr_dep_news_trf ) | French | [`camembert-base` ](https://huggingface.co/camembert-base ) | 95.7 | 94.4 | - |
| [`zh_core_web_trf` ](/models/zh#zh_core_news_trf ) | Chinese | [`bert-base-chinese` ](https://huggingface.co/bert-base-chinese ) | 92.5 | 76.6 | 75.4 |
2020-10-15 13:35:30 +03:00
2020-08-11 21:57:23 +03:00
< Infobox title = "Details & Documentation" emoji = "📖" list >
2020-08-18 01:49:19 +03:00
- **Usage:** [Embeddings & Transformers ](/usage/embeddings-transformers ),
2020-09-12 18:05:10 +03:00
[Training pipelines and models ](/usage/training ),
[Benchmarks ](/usage/facts-figures#benchmarks )
2020-08-11 21:57:23 +03:00
- **API:** [`Transformer` ](/api/transformer ),
[`TransformerData` ](/api/transformer#transformerdata ),
[`FullTransformerBatch` ](/api/transformer#fulltransformerbatch )
- **Architectures: ** [TransformerModel ](/api/architectures#TransformerModel ),
2020-08-31 13:41:39 +03:00
[TransformerListener ](/api/architectures#TransformerListener ),
2020-08-11 21:57:23 +03:00
[Tok2VecTransformer ](/api/architectures#Tok2VecTransformer )
- **Implementation:**
[`spacy-transformers` ](https://github.com/explosion/spacy-transformers )
< / Infobox >
2020-09-12 18:05:10 +03:00
### New training workflow and config system {#features-training}
> #### Example
>
> ```ini
> [training]
> accumulate_gradient = 3
>
> [training.optimizer]
> @optimizers = "Adam.v1"
>
> [training.optimizer.learn_rate]
> @schedules = "warmup_linear.v1"
> warmup_steps = 250
> total_steps = 20000
> initial_rate = 0.01
> ```
spaCy v3.0 introduces a comprehensive and extensible system for **configuring
your training runs**. A single configuration file describes every detail of your
training run, with no hidden defaults, making it easy to rerun your experiments
and track changes. You can use the
[quickstart widget ](/usage/training#quickstart ) or the `init config` command to
get started. Instead of providing lots of arguments on the command line, you
2020-09-30 16:16:00 +03:00
only need to pass your `config.cfg` file to [`spacy train` ](/api/cli#train ).
2020-09-12 18:05:10 +03:00
Training config files include all **settings and hyperparameters** for training
your pipeline. Some settings can also be registered **functions** that you can
swap out and customize, making it easy to implement your own custom models and
architectures.
2020-09-30 16:16:00 +03:00
![Illustration of pipeline lifecycle ](../images/lifecycle.svg )
2020-09-12 18:05:10 +03:00
< Infobox title = "Details & Documentation" emoji = "📖" list >
- **Usage:** [Training pipelines and models ](/usage/training )
- **Thinc:** [Thinc's config system ](https://thinc.ai/docs/usage-config ),
[`Config` ](https://thinc.ai/docs/api-config#config )
- **CLI:** [`init config` ](/api/cli#init-config ),
[`init fill-config` ](/api/cli#init-fill-config ), [`train` ](/api/cli#train ),
[`pretrain` ](/api/cli#pretrain ), [`evaluate` ](/api/cli#evaluate )
- **API:** [Config format ](/api/data-formats#config ),
[`registry` ](/api/top-level#registry )
< / Infobox >
2020-08-19 17:54:16 +03:00
### Custom models using any framework {#features-custom-models}
2020-08-10 01:01:38 +03:00
2020-09-12 18:05:10 +03:00
> #### Example
>
> ```python
> from torch import nn
> from thinc.api import PyTorchWrapper
>
> torch_model = nn.Sequential(
> nn.Linear(32, 32),
> nn.ReLU(),
> nn.Softmax(dim=1)
> )
> model = PyTorchWrapper(torch_model)
> ```
spaCy's new configuration system makes it easy to customize the neural network
models used by the different pipeline components. You can also implement your
own architectures via spaCy's machine learning library [Thinc ](https://thinc.ai )
that provides various layers and utilities, as well as thin wrappers around
frameworks like **PyTorch** , **TensorFlow** and **MXNet** . Component models all
follow the same unified [`Model` ](https://thinc.ai/docs/api-model ) API and each
`Model` can also be used as a sublayer of a larger network, allowing you to
freely combine implementations from different frameworks into a single model.
2020-10-06 15:35:17 +03:00
![Illustration of Pipe methods ](../images/trainable_component.svg )
2020-08-21 14:22:59 +03:00
< Infobox title = "Details & Documentation" emoji = "📖" list >
2020-10-06 15:35:17 +03:00
- **Usage: ** [Layers and architectures ](/usage/layers-architectures ),
[Trainable component API ](/usage/processing-pipelines#trainable-components ),
[Trainable components and models ](/usage/layers-architectures#components )
2020-08-21 14:22:59 +03:00
- **Thinc: **
2020-09-12 18:05:10 +03:00
[Wrapping PyTorch, TensorFlow & MXNet ](https://thinc.ai/docs/usage-frameworks ),
[`Model` API ](https://thinc.ai/docs/api-model )
2020-10-08 22:33:49 +03:00
- **API:** [Model architectures ](/api/architectures ),
[`TrainablePipe` ](/api/pipe )
2020-08-21 14:22:59 +03:00
< / Infobox >
2020-08-10 01:01:38 +03:00
### Manage end-to-end workflows with projects {#features-projects}
2020-08-21 14:22:59 +03:00
> #### Example
>
> ```cli
> # Clone a project template
2020-09-20 18:44:58 +03:00
> $ python -m spacy project clone pipelines/tagger_parser_ud
> $ cd tagger_parser_ud
2020-08-21 14:22:59 +03:00
> # Download data assets
> $ python -m spacy project assets
> # Run a workflow
2020-09-20 18:44:58 +03:00
> $ python -m spacy project run all
2020-08-21 14:22:59 +03:00
> ```
spaCy projects let you manage and share **end-to-end spaCy workflows** for
different **use cases and domains** , and orchestrate training, packaging and
2020-09-03 14:13:03 +03:00
serving your custom pipelines. You can start off by cloning a pre-defined
project template, adjust it to fit your needs, load in your data, train a
pipeline, export it as a Python package, upload your outputs to a remote storage
and share your results with your team.
2020-08-23 19:32:09 +03:00
![Illustration of project workflow and commands ](../images/projects.svg )
spaCy projects also make it easy to **integrate with other tools** in the data
science and machine learning ecosystem, including [DVC ](/usage/projects#dvc ) for
data version control, [Prodigy ](/usage/projects#prodigy ) for creating labelled
data, [Streamlit ](/usage/projects#streamlit ) for building interactive apps,
2020-08-21 14:22:59 +03:00
[FastAPI ](/usage/projects#fastapi ) for serving models in production,
[Ray ](/usage/projects#ray ) for parallel training,
[Weights & Biases ](/usage/projects#wandb ) for experiment tracking, and more!
2020-08-11 21:57:23 +03:00
< Infobox title = "Details & Documentation" emoji = "📖" list >
- **Usage:** [spaCy projects ](/usage/projects ),
2020-09-03 14:13:03 +03:00
[Training pipelines and models ](/usage/training )
2020-08-11 21:57:23 +03:00
- **CLI:** [`project` ](/api/cli#project ), [`train` ](/api/cli#train )
- **Templates:** [`projects` ](https://github.com/explosion/projects )
< / Infobox >
2020-09-20 18:44:58 +03:00
< Project id = "pipelines/tagger_parser_ud" >
The easiest way to get started is to clone a [project template ](/usage/projects )
and run it – for example, this end-to-end template that lets you train a
**part-of-speech tagger** and **dependency parser** on a Universal Dependencies
treebank.
< / Project >
2020-09-13 23:30:33 +03:00
### Parallel and distributed training with Ray {#features-parallel-training}
> #### Example
>
> ```cli
2020-10-08 11:58:50 +03:00
> $ pip install -U %%SPACY_PKG_NAME[ray]%%SPACY_PKG_FLAGS
2020-09-13 23:30:33 +03:00
> # Check that the CLI is registered
> $ python -m spacy ray --help
> # Train a pipeline
> $ python -m spacy ray train config.cfg --n-workers 2
> ```
[Ray ](https://ray.io/ ) is a fast and simple framework for building and running
**distributed applications**. You can use Ray to train spaCy on one or more
remote machines, potentially speeding up your training process. The Ray
integration is powered by a lightweight extension package,
[`spacy-ray` ](https://github.com/explosion/spacy-ray ), that automatically adds
the [`ray` ](/api/cli#ray ) command to your spaCy CLI if it's installed in the
same environment. You can then run [`spacy ray train` ](/api/cli#ray-train ) for
parallel training.
![Illustration of setup ](../images/spacy-ray.svg )
< Infobox title = "Details & Documentation" emoji = "📖" list >
- **Usage: **
[Parallel and distributed training ](/usage/training#parallel-training ),
[spaCy Projects integration ](/usage/projects#ray )
- **CLI:** [`ray` ](/api/cli#ray ), [`ray train` ](/api/cli#ray-train )
- **Implementation:** [`spacy-ray` ](https://github.com/explosion/spacy-ray )
< / Infobox >
2020-08-10 01:01:38 +03:00
### New built-in pipeline components {#features-pipeline-components}
2020-08-21 14:22:59 +03:00
spaCy v3.0 includes several new trainable and rule-based components that you can
add to your pipeline and customize for your use case:
> #### Example
>
> ```python
2020-10-08 11:58:50 +03:00
> # pip install -U %%SPACY_PKG_NAME[lookups]%%SPACY_PKG_FLAGS
2020-08-21 14:22:59 +03:00
> nlp = spacy.blank("en")
> nlp.add_pipe("lemmatizer")
> ```
2021-03-09 15:04:22 +03:00
| Name | Description |
| ----------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`SentenceRecognizer` ](/api/sentencerecognizer ) | Trainable component for sentence segmentation. |
| [`Morphologizer` ](/api/morphologizer ) | Trainable component to predict morphological features. |
| [`Lemmatizer` ](/api/lemmatizer ) | Standalone component for rule-based and lookup lemmatization. |
| [`AttributeRuler` ](/api/attributeruler ) | Component for setting token attributes using match patterns. |
| [`Transformer` ](/api/transformer ) | Component for using [transformer models ](/usage/embeddings-transformers ) in your pipeline, accessing outputs and aligning tokens. Provided via [`spacy-transformers` ](https://github.com/explosion/spacy-transformers ). |
| [`TrainablePipe` ](/api/pipe ) | Base class for trainable pipeline components. |
| [`Multi-label TextCategorizer` ](/api/textcategorizer ) | Trainable component for multi-label text classification. |
2020-08-10 01:01:38 +03:00
2020-08-11 21:57:23 +03:00
< Infobox title = "Details & Documentation" emoji = "📖" list >
- **Usage:** [Processing pipelines ](/usage/processing-pipelines )
- **API:** [Built-in pipeline components ](/api#architecture-pipeline )
2020-09-12 18:05:10 +03:00
- **Implementation:** [`spacy/pipeline` ](%%GITHUB_SPACY/spacy/pipeline )
2020-08-11 21:57:23 +03:00
< / Infobox >
2020-08-10 01:01:38 +03:00
### New and improved pipeline component APIs {#features-components}
2020-08-21 14:22:59 +03:00
> #### Example
>
> ```python
> @Language.component("my_component")
> def my_component(doc):
> return doc
>
> nlp.add_pipe("my_component")
> nlp.add_pipe("ner", source=other_nlp)
> nlp.analyze_pipes(pretty=True)
> ```
Defining, configuring, reusing, training and analyzing pipeline components is
now easier and more convenient. The `@Language.component` and
`@Language.factory` decorators let you register your component, define its
default configuration and meta data, like the attribute values it assigns and
requires. Any custom component can be included during training, and sourcing
2020-09-03 14:13:03 +03:00
components from existing trained pipelines lets you **mix and match custom
2020-08-21 14:22:59 +03:00
pipelines**. The `nlp.analyze_pipes` method outputs structured information about
the current pipeline and its components, including the attributes they assign,
the scores they compute during training and whether any required attributes
aren't set.
2020-08-10 01:01:38 +03:00
2020-08-11 21:57:23 +03:00
< Infobox title = "Details & Documentation" emoji = "📖" list >
- **Usage:** [Custom components ](/usage/processing-pipelines#custom_components ),
2020-08-21 14:22:59 +03:00
[Defining components for training ](/usage/training#config-components )
- **API:** [`@Language.component` ](/api/language#component ),
[`@Language.factory` ](/api/language#factory ),
[`Language.add_pipe` ](/api/language#add_pipe ),
[`Language.analyze_pipes` ](/api/language#analyze_pipes )
2020-09-12 18:05:10 +03:00
- **Implementation:** [`spacy/language.py` ](%%GITHUB_SPACY/spacy/language.py )
2020-08-11 21:57:23 +03:00
< / Infobox >
2020-09-04 17:30:31 +03:00
### Dependency matching {#features-dep-matcher}
> #### Example
>
> ```python
2020-09-04 21:58:36 +03:00
> from spacy.matcher import DependencyMatcher
>
> matcher = DependencyMatcher(nlp.vocab)
> pattern = [
> {"RIGHT_ID": "anchor_founded", "RIGHT_ATTRS": {"ORTH": "founded"}},
> {"LEFT_ID": "anchor_founded", "REL_OP": ">", "RIGHT_ID": "subject", "RIGHT_ATTRS": {"DEP": "nsubj"}}
> ]
> matcher.add("FOUNDED", [pattern])
2020-09-04 17:30:31 +03:00
> ```
2020-09-04 21:58:36 +03:00
The new [`DependencyMatcher` ](/api/dependencymatcher ) lets you match patterns
within the dependency parse using
2020-09-04 17:30:31 +03:00
[Semgrex ](https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/semgraph/semgrex/SemgrexPattern.html )
operators. It follows the same API as the token-based [`Matcher` ](/api/matcher ).
A pattern added to the dependency matcher consists of a **list of
dictionaries**, with each dictionary describing a **token to match** and its
**relation to an existing token** in the pattern.
2020-09-15 13:44:03 +03:00
![Dependency matcher pattern ](../images/dep-match-diagram.svg )
2020-09-04 17:30:31 +03:00
< Infobox title = "Details & Documentation" emoji = "📖" list >
- **Usage:**
[Dependency matching ](/usage/rule-based-matching#dependencymatcher ),
- **API:** [`DependencyMatcher` ](/api/dependencymatcher ),
- **Implementation:**
2020-09-12 18:05:10 +03:00
[`spacy/matcher/dependencymatcher.pyx` ](%%GITHUB_SPACY/spacy/matcher/dependencymatcher.pyx )
2020-09-04 17:30:31 +03:00
< / Infobox >
2020-08-10 01:01:38 +03:00
### Type hints and type-based data validation {#features-types}
2020-08-11 21:57:23 +03:00
> #### Example
>
> ```python
> from spacy.language import Language
> from pydantic import StrictBool
>
> @Language.factory("my_component")
> def create_my_component(
> nlp: Language,
> name: str,
> custom: StrictBool
> ):
> ...
> ```
2020-08-10 01:01:38 +03:00
spaCy v3.0 officially drops support for Python 2 and now requires **Python
3.6+**. This also means that the code base can take full advantage of
[type hints ](https://docs.python.org/3/library/typing.html ). spaCy's user-facing
API that's implemented in pure Python (as opposed to Cython) now comes with type
hints. The new version of spaCy's machine learning library
[Thinc ](https://thinc.ai ) also features extensive
[type support ](https://thinc.ai/docs/usage-type-checking/ ), including custom
types for models and arrays, and a custom `mypy` plugin that can be used to
type-check model definitions.
2020-09-15 01:32:49 +03:00
For data validation, spaCy v3.0 adopts
2020-08-10 01:01:38 +03:00
[`pydantic` ](https://github.com/samuelcolvin/pydantic ). It also powers the data
validation of Thinc's [config system ](https://thinc.ai/docs/usage-config ), which
2020-10-09 13:04:52 +03:00
lets you register **custom functions with typed arguments** , reference them in
your config and see validation errors if the argument values don't match.
2020-08-10 01:01:38 +03:00
2020-08-11 21:57:23 +03:00
< Infobox title = "Details & Documentation" emoji = "📖" list >
- **Usage: **
[Component type hints and validation ](/usage/processing-pipelines#type-hints ),
[Training with custom code ](/usage/training#custom-code )
- **Thinc: **
[Type checking in Thinc ](https://thinc.ai/docs/usage-type-checking ),
[Thinc's config system ](https://thinc.ai/docs/usage-config )
< / Infobox >
2020-08-21 14:22:59 +03:00
### New methods, attributes and commands {#new-methods}
2020-08-11 21:57:23 +03:00
The following methods, attributes and commands are new in spaCy v3.0.
2020-08-29 19:43:19 +03:00
| Name | Description |
| ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [`Token.lex` ](/api/token#attributes ) | Access a token's [`Lexeme` ](/api/lexeme ). |
2020-11-03 20:10:06 +03:00
| [`Token.morph` ](/api/token#attributes ) | Access a token's morphological analysis. |
2021-01-31 08:11:24 +03:00
| [`Doc.spans` ](/api/doc#spans ) | Named span groups to store and access collections of potentially overlapping spans. Uses the new [`SpanGroup` ](/api/spangroup ) data structure. |
2020-09-17 01:14:01 +03:00
| [`Doc.has_annotation` ](/api/doc#has_annotation ) | Check whether a doc has annotation on a token attribute. |
2020-08-29 19:43:19 +03:00
| [`Language.select_pipes` ](/api/language#select_pipes ) | Context manager for enabling or disabling specific pipeline components for a block. |
| [`Language.disable_pipe` ](/api/language#disable_pipe ), [`Language.enable_pipe` ](/api/language#enable_pipe ) | Disable or enable a loaded pipeline component (but don't remove it). |
| [`Language.analyze_pipes` ](/api/language#analyze_pipes ) | [Analyze ](/usage/processing-pipelines#analysis ) components and their interdependencies. |
2020-09-03 14:13:03 +03:00
| [`Language.resume_training` ](/api/language#resume_training ) | Experimental: continue training a trained pipeline and initialize "rehearsal" for components that implement a `rehearse` method to prevent catastrophic forgetting. |
2020-08-29 19:43:19 +03:00
| [`@Language.factory` ](/api/language#factory ), [`@Language.component` ](/api/language#component ) | Decorators for [registering ](/usage/processing-pipelines#custom-components ) pipeline component factories and simple stateless component functions. |
2020-11-03 20:10:06 +03:00
| [`Language.has_factory` ](/api/language#has_factory ) | Check whether a component factory is registered on a language class. |
2020-08-29 19:43:19 +03:00
| [`Language.get_factory_meta` ](/api/language#get_factory_meta ), [`Language.get_pipe_meta` ](/api/language#get_factory_meta ) | Get the [`FactoryMeta` ](/api/language#factorymeta ) with component metadata for a factory or instance name. |
| [`Language.config` ](/api/language#config ) | The [config ](/usage/training#config ) used to create the current `nlp` object. An instance of [`Config` ](https://thinc.ai/docs/api-config#config ) and can be saved to disk and used for training. |
| [`Language.components` ](/api/language#attributes ), [`Language.component_names` ](/api/language#attributes ) | All available components and component names, including disabled components that are not run as part of the pipeline. |
| [`Language.disabled` ](/api/language#attributes ) | Names of disabled components that are not run as part of the pipeline. |
2020-10-08 22:33:49 +03:00
| [`TrainablePipe.score` ](/api/pipe#score ) | Method on pipeline components that returns a dictionary of evaluation scores. |
2020-08-29 19:43:19 +03:00
| [`registry` ](/api/top-level#registry ) | Function registry to map functions to string names that can be referenced in [configs ](/usage/training#config ). |
2020-09-03 14:13:03 +03:00
| [`util.load_meta` ](/api/top-level#util.load_meta ), [`util.load_config` ](/api/top-level#util.load_config ) | Updated helpers for loading a pipeline's [`meta.json` ](/api/data-formats#meta ) and [`config.cfg` ](/api/data-formats#config ). |
| [`util.get_installed_models` ](/api/top-level#util.get_installed_models ) | Names of all pipeline packages installed in the environment. |
2020-08-29 19:43:19 +03:00
| [`init config` ](/api/cli#init-config ), [`init fill-config` ](/api/cli#init-fill-config ), [`debug config` ](/api/cli#debug-config ) | CLI commands for initializing, auto-filling and debugging [training configs ](/usage/training ). |
2020-10-01 18:45:29 +03:00
| [`init vectors` ](/api/cli#init-vectors ) | Convert word vectors for use with spaCy. |
| [`init labels` ](/api/cli#init-labels ) | Generate JSON files for the labels in the data to speed up training. |
2020-08-29 19:43:19 +03:00
| [`project` ](/api/cli#project ) | Suite of CLI commands for cloning, running and managing [spaCy projects ](/usage/projects ). |
2020-09-13 23:30:33 +03:00
| [`ray` ](/api/cli#ray ) | Suite of CLI commands for parallel training with [Ray ](https://ray.io/ ), provided by the [`spacy-ray` ](https://github.com/explosion/spacy-ray ) extension package. |
2020-08-10 01:01:38 +03:00
2020-08-21 14:22:59 +03:00
### New and updated documentation {#new-docs}
< Grid cols = {2} gutterBottom = {false} >
< div >
To help you get started with spaCy v3.0 and the new features, we've added
several new or rewritten documentation pages, including a new usage guide on
[embeddings, transformers and transfer learning ](/usage/embeddings-transformers ),
2020-09-03 14:13:03 +03:00
a guide on [training pipelines and models ](/usage/training ) rewritten from
scratch, a page explaining the new [spaCy projects ](/usage/projects ) and updated
usage documentation on
2020-08-21 14:22:59 +03:00
[custom pipeline components ](/usage/processing-pipelines#custom-components ).
We've also added a bunch of new illustrations and new API reference pages
documenting spaCy's machine learning [model architectures ](/api/architectures )
and the expected [data formats ](/api/data-formats ). API pages about
[pipeline components ](/api/#architecture-pipeline ) now include more information,
like the default config and implementation, and we've adopted a more detailed
format for documenting argument and return types.
< / div >
[![Library architecture ](../images/architecture.svg )](/api)
< / Grid >
< Infobox title = "New or reworked documentation" emoji = "📖" list >
- **Usage: ** [Embeddings & Transformers ](/usage/embeddings-transformers ),
2020-08-21 17:21:55 +03:00
[Training models ](/usage/training ),
[Layers & Architectures ](/usage/layers-architectures ),
[Projects ](/usage/projects ),
2020-08-21 14:49:18 +03:00
[Custom pipeline components ](/usage/processing-pipelines#custom-components ),
2020-08-29 19:43:19 +03:00
[Custom tokenizers ](/usage/linguistic-features#custom-tokenizer ),
[Morphology ](/usage/linguistic-features#morphology ),
[Lemmatization ](/usage/linguistic-features#lemmatization ),
2020-09-05 14:41:10 +03:00
[Mapping & Exceptions ](/usage/linguistic-features#mappings-exceptions ),
[Dependency matching ](/usage/rule-based-matching#dependencymatcher )
2020-08-21 14:22:59 +03:00
- **API Reference: ** [Library architecture ](/api ),
[Model architectures ](/api/architectures ), [Data formats ](/api/data-formats )
- **New Classes: ** [`Example` ](/api/example ), [`Tok2Vec` ](/api/tok2vec ),
[`Transformer` ](/api/transformer ), [`Lemmatizer` ](/api/lemmatizer ),
[`Morphologizer` ](/api/morphologizer ),
[`AttributeRuler` ](/api/attributeruler ),
2020-08-31 21:04:26 +03:00
[`SentenceRecognizer` ](/api/sentencerecognizer ),
2020-10-08 22:33:49 +03:00
[`DependencyMatcher` ](/api/dependencymatcher ), [`TrainablePipe` ](/api/pipe ),
2021-01-14 09:30:41 +03:00
[`Corpus` ](/api/corpus ), [`SpanGroup` ](/api/spangroup ),
2020-08-21 14:22:59 +03:00
< / Infobox >
2020-07-01 14:02:17 +03:00
## Backwards Incompatibilities {#incompat}
2020-08-10 01:01:38 +03:00
As always, we've tried to keep the breaking changes to a minimum and focus on
changes that were necessary to support the new features, fix problems or improve
usability. The following section lists the relevant changes to the user-facing
API. For specific examples of how to rewrite your code, check out the
[migration guide ](#migrating ).
2020-08-11 21:57:23 +03:00
< Infobox variant = "warning" >
Note that spaCy v3.0 now requires **Python 3.6+** .
2020-08-10 01:01:38 +03:00
2020-08-11 21:57:23 +03:00
< / Infobox >
2020-07-25 19:51:12 +03:00
2020-08-10 01:01:38 +03:00
### API changes {#incompat-api}
2020-07-27 19:11:45 +03:00
2020-09-03 14:13:03 +03:00
- Pipeline package symlinks, the `link` command and shortcut names are now
deprecated. There can be many [different trained pipelines ](/models ) and not
just one "English model", so you should always use the full package name like
2020-10-06 15:35:17 +03:00
`en_core_web_sm` explicitly.
- A pipeline's `meta.json` is now only used to provide meta information like the
package name, author, license and labels. It's **not** used to construct the
processing pipeline anymore. This is all defined in the
[`config.cfg` ](/api/data-formats#config ), which also includes all settings
used to train the pipeline.
- The `train` , `pretrain` and `debug data` commands now only take a
`config.cfg` .
2020-08-10 01:01:38 +03:00
- [`Language.add_pipe` ](/api/language#add_pipe ) now takes the **string name** of
the component factory instead of the component function.
2020-08-21 19:34:02 +03:00
- **Custom pipeline components** now need to be decorated with the
2020-08-10 01:01:38 +03:00
[`@Language.component` ](/api/language#component ) or
[`@Language.factory` ](/api/language#factory ) decorator.
2020-08-11 21:57:23 +03:00
- The [`Language.update` ](/api/language#update ),
[`Language.evaluate` ](/api/language#evaluate ) and
2020-10-08 22:33:49 +03:00
[`TrainablePipe.update` ](/api/pipe#update ) methods now all take batches of
2020-08-11 21:57:23 +03:00
[`Example` ](/api/example ) objects instead of `Doc` and `GoldParse` objects, or
raw text and a dictionary of annotations.
2020-10-06 15:35:17 +03:00
- The `begin_training` methods have been renamed to `initialize` and now take a
function that returns a sequence of `Example` objects to initialize the model
instead of a list of tuples.
2020-09-12 18:05:10 +03:00
- [`Matcher.add` ](/api/matcher#add ) and
[`PhraseMatcher.add` ](/api/phrasematcher#add ) now only accept a list of
patterns as the second argument (instead of a variable number of arguments).
The `on_match` callback becomes an optional keyword argument.
2020-09-21 21:43:54 +03:00
- The `Doc` flags like `Doc.is_parsed` or `Doc.is_tagged` have been replaced by
[`Doc.has_annotation` ](/api/doc#has_annotation ).
2020-09-12 18:05:10 +03:00
- The `spacy.gold` module has been renamed to
[`spacy.training` ](%%GITHUB_SPACY/spacy/training ).
2020-08-25 12:54:37 +03:00
- The `PRON_LEMMA` symbol and `-PRON-` as an indicator for pronoun lemmas has
been removed.
2020-08-29 19:43:19 +03:00
- The `TAG_MAP` and `MORPH_RULES` in the language data have been replaced by the
more flexible [`AttributeRuler` ](/api/attributeruler ).
- The [`Lemmatizer` ](/api/lemmatizer ) is now a standalone pipeline component and
doesn't provide lemmas by default or switch automatically between lookup and
rule-based lemmas. You can now add it to your pipeline explicitly and set its
mode on initialization.
2020-09-15 13:44:03 +03:00
- Various keyword arguments across functions and methods are now explicitly
2021-01-31 08:11:24 +03:00
declared as **keyword-only** arguments. Those arguments are documented
accordingly across the API reference using the < Tag > keyword-only< / Tag > tag.
2021-03-09 15:04:22 +03:00
- The `textcat` pipeline component is now only applicable for classification of
mutually exclusives classes - i.e. one predicted class per input sentence or
document. To perform multi-label classification, use the new
`textcat_multilabel` component instead.
2020-08-10 01:01:38 +03:00
### Removed or renamed API {#incompat-removed}
2020-09-23 11:14:27 +03:00
| Removed | Replacement |
| -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
2020-10-06 15:35:17 +03:00
| `Language.disable_pipes` | [`Language.select_pipes` ](/api/language#select_pipes ), [`Language.disable_pipe` ](/api/language#disable_pipe ), [`Language.enable_pipe` ](/api/language#enable_pipe ) |
2020-09-28 22:35:09 +03:00
| `Language.begin_training` , `Pipe.begin_training` , ... | [`Language.initialize` ](/api/language#initialize ), [`Pipe.initialize` ](/api/pipe#initialize ), ... |
2020-09-23 11:14:27 +03:00
| `Doc.is_tagged` , `Doc.is_parsed` , ... | [`Doc.has_annotation` ](/api/doc#has_annotation ) |
| `GoldParse` | [`Example` ](/api/example ) |
| `GoldCorpus` | [`Corpus` ](/api/corpus ) |
| `KnowledgeBase.load_bulk` , `KnowledgeBase.dump` | [`KnowledgeBase.from_disk` ](/api/kb#from_disk ), [`KnowledgeBase.to_disk` ](/api/kb#to_disk ) |
2021-02-25 22:09:36 +03:00
| `KnowledgeBase.get_candidates` | [`KnowledgeBase.get_alias_candidates` ](/api/kb#get_alias_candidates ) |
2020-09-23 11:14:27 +03:00
| `Matcher.pipe` , `PhraseMatcher.pipe` | not needed |
| `gold.offsets_from_biluo_tags` , `gold.spans_from_biluo_tags` , `gold.biluo_tags_from_offsets` | [`training.biluo_tags_to_offsets` ](/api/top-level#biluo_tags_to_offsets ), [`training.biluo_tags_to_spans` ](/api/top-level#biluo_tags_to_spans ), [`training.offsets_to_biluo_tags` ](/api/top-level#offsets_to_biluo_tags ) |
2020-10-01 13:15:53 +03:00
| `spacy init-model` | [`spacy init vectors` ](/api/cli#init-vectors ) |
2020-09-23 11:14:27 +03:00
| `spacy debug-data` | [`spacy debug data` ](/api/cli#debug-data ) |
| `spacy profile` | [`spacy debug profile` ](/api/cli#debug-profile ) |
| `spacy link` , `util.set_data_path` , `util.get_data_path` | not needed, symlinks are deprecated |
2020-07-25 19:51:12 +03:00
2021-04-12 15:37:00 +03:00
The following methods, attributes and arguments were removed in v3.0. Most of
them have been **deprecated for a while** and many would previously raise
errors. Many of them were also mostly internals. If you've been working with
more recent versions of spaCy v2.x, it's **unlikely** that your code relied on
them.
2020-07-25 19:51:12 +03:00
2020-07-29 12:36:42 +03:00
| Removed | Replacement |
| ----------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Doc.tokens_from_list` | [`Doc.__init__` ](/api/doc#init ) |
| `Doc.merge` , `Span.merge` | [`Doc.retokenize` ](/api/doc#retokenize ) |
| `Token.string` , `Span.string` , `Span.upper` , `Span.lower` | [`Span.text` ](/api/span#attributes ), [`Token.text` ](/api/token#attributes ) |
| `Language.tagger` , `Language.parser` , `Language.entity` | [`Language.get_pipe` ](/api/language#get_pipe ) |
| keyword-arguments like `vocab=False` on `to_disk` , `from_disk` , `to_bytes` , `from_bytes` | `exclude=["vocab"]` |
| `n_threads` argument on [`Tokenizer` ](/api/tokenizer ), [`Matcher` ](/api/matcher ), [`PhraseMatcher` ](/api/phrasematcher ) | `n_process` |
2020-08-21 14:22:59 +03:00
| `verbose` argument on [`Language.evaluate` ](/api/language#evaluate ) | logging (`DEBUG`) |
2020-08-29 16:56:50 +03:00
| `SentenceSegmenter` hook, `SimilarityHook` | [user hooks ](/usage/processing-pipelines#custom-components-user-hooks ), [`Sentencizer` ](/api/sentencizer ), [`SentenceRecognizer` ](/api/sentencerecognizer ) |
2020-07-25 19:51:12 +03:00
2020-07-01 14:02:17 +03:00
## Migrating from v2.x {#migrating}
2020-07-27 01:29:45 +03:00
2020-09-03 14:13:03 +03:00
### Downloading and loading trained pipelines {#migrating-downloading-models}
2020-07-29 12:36:42 +03:00
2021-04-12 15:37:00 +03:00
Symlinks and shortcuts like `en` have been deprecated for a while, and are now
not supported anymore. There are [many different trained pipelines ](/models )
with different capabilities and not just one "English model". In order to
download and load a package, you should always use its full name – for instance,
2020-09-03 14:13:03 +03:00
[`en_core_web_sm` ](/models/en#en_core_web_sm ).
2020-07-29 12:36:42 +03:00
```diff
- python -m spacy download en
+ python -m spacy download en_core_web_sm
```
```diff
- nlp = spacy.load("en")
+ nlp = spacy.load("en_core_web_sm")
```
2020-07-27 19:11:45 +03:00
### Custom pipeline components and factories {#migrating-pipeline-components}
Custom pipeline components now have to be registered explicitly using the
[`@Language.component` ](/api/language#component ) or
[`@Language.factory` ](/api/language#factory ) decorator. For simple functions
that take a `Doc` and return it, all you have to do is add the
`@Language.component` decorator to it and assign it a name:
```diff
### Stateless function components
+ from spacy.language import Language
+ @Language .component("my_component")
def my_component(doc):
return doc
```
For class components that are initialized with settings and/or the shared `nlp`
object, you can use the `@Language.factory` decorator. Also make sure that that
the method used to initialize the factory has **two named arguments** : `nlp`
(the current `nlp` object) and `name` (the string name of the component
instance).
```diff
### Stateful class components
+ from spacy.language import Language
+ @Language .factory("my_component")
class MyComponent:
- def __init__ (self, nlp):
+ def __init__ (self, nlp, name):
self.nlp = nlp
def __call__ (self, doc):
return doc
```
Instead of decorating your class, you could also add a factory function that
takes the arguments `nlp` and `name` and returns an instance of your component:
```diff
### Stateful class components with factory function
+ from spacy.language import Language
+ @Language .factory("my_component")
+ def create_my_component(nlp, name):
+ return MyComponent(nlp)
class MyComponent:
def __init__ (self, nlp):
self.nlp = nlp
def __call__ (self, doc):
return doc
```
The `@Language.component` and `@Language.factory` decorators now take care of
adding an entry to the component factories, so spaCy knows how to load a
component back in from its string name. You won't have to write to
`Language.factories` manually anymore.
```diff
- Language.factories["my_component"] = lambda nlp, **cfg: MyComponent(nlp)
```
#### Adding components to the pipeline {#migrating-add-pipe}
The [`nlp.add_pipe` ](/api/language#add_pipe ) method now takes the **string
name** of the component factory instead of a callable component. This allows
spaCy to track and serialize components that have been added and their settings.
```diff
+ @Language .component("my_component")
def my_component(doc):
return doc
- nlp.add_pipe(my_component)
+ nlp.add_pipe("my_component")
```
[`nlp.add_pipe` ](/api/language#add_pipe ) now also returns the pipeline component
itself, so you can access its attributes. The
[`nlp.create_pipe` ](/api/language#create_pipe ) method is now mostly internals
and you typically shouldn't have to use it in your code.
```diff
- parser = nlp.create_pipe("parser")
- nlp.add_pipe(parser)
+ parser = nlp.add_pipe("parser")
```
2020-09-03 14:13:03 +03:00
If you need to add a component from an existing trained pipeline, you can now
2020-08-11 21:57:23 +03:00
use the `source` argument on [`nlp.add_pipe` ](/api/language#add_pipe ). This will
check that the component is compatible, and take care of porting over all
2020-09-03 14:13:03 +03:00
config. During training, you can also reference existing trained components in
your [config ](/usage/training#config-components ) and decide whether or not they
should be updated with more data.
2020-08-11 21:57:23 +03:00
> #### config.cfg (excerpt)
>
> ```ini
> [components.ner]
> source = "en_core_web_sm"
> component = "ner"
> ```
```diff
source_nlp = spacy.load("en_core_web_sm")
nlp = spacy.blank("en")
- ner = source_nlp.get_pipe("ner")
- nlp.add_pipe(ner)
+ nlp.add_pipe("ner", source=source_nlp)
```
2020-09-17 13:34:11 +03:00
#### Configuring pipeline components with settings {#migrating-configure-pipe}
Because pipeline components are now added using their string names, you won't
have to instantiate the [component classes ](/api/#architecture-pipeline )
2020-09-30 16:16:00 +03:00
directly anymore. To configure the component, you can now use the `config`
2020-09-17 13:34:11 +03:00
argument on [`nlp.add_pipe` ](/api/language#add_pipe ).
> #### config.cfg (excerpt)
>
> ```ini
> [components.sentencizer]
> factory = "sentencizer"
> punct_chars = ["!", ".", "?"]
> ```
```diff
punct_chars = ["!", ".", "?"]
- sentencizer = Sentencizer(punct_chars=punct_chars)
+ sentencizer = nlp.add_pipe("sentencizer", config={"punct_chars": punct_chars})
```
The `config` corresponds to the component settings in the
[`config.cfg` ](/usage/training#config-components ) and will overwrite the default
config defined by the components.
< Infobox variant = "warning" title = "Important note on config values" >
Config values you pass to components **need to be JSON-serializable** and can't
be arbitrary Python objects. Otherwise, the settings you provide can't be
represented in the `config.cfg` and spaCy has no way of knowing how to re-create
your component with the same settings when you load the pipeline back in. If you
need to pass arbitrary objects to a component, use a
[registered function ](/usage/processing-pipelines#example-stateful-components ):
```diff
- config = {"model": MyTaggerModel()}
+ config= {"model": {"@architectures": "MyTaggerModel"}}
tagger = nlp.add_pipe("tagger", config=config)
```
< / Infobox >
2020-08-11 21:57:23 +03:00
### Adding match patterns {#migrating-matcher}
The [`Matcher.add` ](/api/matcher#add ),
[`PhraseMatcher.add` ](/api/phrasematcher#add ) and
[`DependencyMatcher.add` ](/api/dependencymatcher#add ) methods now only accept a
**list of patterns** as the second argument (instead of a variable number of
arguments). The `on_match` callback becomes an optional keyword argument.
```diff
matcher = Matcher(nlp.vocab)
patterns = [[{"TEXT": "Google"}, {"TEXT": "Now"}], [{"TEXT": "GoogleNow"}]]
- matcher.add("GoogleNow", on_match, *patterns)
+ matcher.add("GoogleNow", patterns, on_match=on_match)
```
```diff
matcher = PhraseMatcher(nlp.vocab)
patterns = [nlp("health care reform"), nlp("healthcare reform")]
- matcher.add("HEALTH", on_match, *patterns)
+ matcher.add("HEALTH", patterns, on_match=on_match)
```
2020-08-31 10:42:06 +03:00
### Migrating attributes in tokenizer exceptions {#migrating-tokenizer-exceptions}
Tokenizer exceptions are now only allowed to set `ORTH` and `NORM` values as
part of the token attributes. Exceptions for other attributes such as `TAG` and
`LEMMA` should be moved to an [`AttributeRuler` ](/api/attributeruler ) component:
```diff
nlp = spacy.blank("en")
- nlp.tokenizer.add_special_case("don't", [{"ORTH": "do"}, {"ORTH": "n't", "LEMMA": "not"}])
+ nlp.tokenizer.add_special_case("don't", [{"ORTH": "do"}, {"ORTH": "n't"}])
+ ruler = nlp.add_pipe("attribute_ruler")
+ ruler.add(patterns=[[{"ORTH": "n't"}]], attrs={"LEMMA": "not"})
```
2020-08-29 19:43:19 +03:00
### Migrating tag maps and morph rules {#migrating-training-mappings-exceptions}
Instead of defining a `tag_map` and `morph_rules` in the language data, spaCy
v3.0 now manages mappings and exceptions with a separate and more flexible
pipeline component, the [`AttributeRuler` ](/api/attributeruler ). See the
2020-10-04 14:54:05 +03:00
[usage guide ](/usage/linguistic-features#mappings-exceptions ) for examples. If
you have tag maps and morph rules in the v2.x format, you can load them into the
attribute ruler before training using the `[initialize]` block of your config.
> #### What does the initialization do?
>
> The `[initialize]` block is used when
> [`nlp.initialize`](/api/language#initialize) is called (usually right before
> training). It lets you define data resources for initializing the pipeline in
> your `config.cfg`. After training, the rules are saved to disk with the
> exported pipeline, so your runtime model doesn't depend on local data. For
> details see the [config lifecycle](/usage/training/#config-lifecycle) and
> [initialization](/usage/training/#initialization) docs.
```ini
### config.cfg (excerpt)
[initialize.components.attribute_ruler]
[initialize.components.attribute_ruler.tag_map]
@readers = "srsly.read_json.v1"
path = "./corpus/tag_map.json"
```
The `AttributeRuler` also provides two handy helper methods
2020-08-29 19:43:19 +03:00
[`load_from_tag_map` ](/api/attributeruler#load_from_tag_map ) and
[`load_from_morph_rules` ](/api/attributeruler#load_from_morph_rules ) that let
you load in your existing tag map or morph rules:
```diff
nlp = spacy.blank("en")
- nlp.vocab.morphology.load_tag_map(YOUR_TAG_MAP)
+ ruler = nlp.add_pipe("attribute_ruler")
+ ruler.load_from_tag_map(YOUR_TAG_MAP)
```
2020-09-17 01:14:01 +03:00
### Migrating Doc flags {#migrating-doc-flags}
2020-09-21 21:43:54 +03:00
The [`Doc` ](/api/doc ) flags `Doc.is_tagged` , `Doc.is_parsed` , `Doc.is_nered` and
`Doc.is_sentenced` are deprecated in v3.0 and replaced by
2020-09-17 01:14:01 +03:00
[`Doc.has_annotation` ](/api/doc#has_annotation ) method, which refers to the
2020-09-21 21:43:54 +03:00
token attribute symbols (the same symbols used in [`Matcher` ](/api/matcher )
patterns):
2020-09-17 01:14:01 +03:00
```diff
doc = nlp(text)
- doc.is_parsed
+ doc.has_annotation("DEP")
- doc.is_tagged
+ doc.has_annotation("TAG")
- doc.is_sentenced
+ doc.has_annotation("SENT_START")
- doc.is_nered
+ doc.has_annotation("ENT_IOB")
```
2020-09-03 14:13:03 +03:00
### Training pipelines and models {#migrating-training}
2020-07-27 19:11:45 +03:00
2020-09-03 14:13:03 +03:00
To train your pipelines, you should now pretty much always use the
2020-07-27 19:11:45 +03:00
[`spacy train` ](/api/cli#train ) CLI. You shouldn't have to put together your own
training scripts anymore, unless you _really_ want to. The training commands now
use a [flexible config file ](/usage/training#config ) that describes all training
2020-09-03 14:13:03 +03:00
settings and hyperparameters, as well as your pipeline, components and
2020-07-27 19:11:45 +03:00
architectures to use. The `--code` argument lets you pass in code containing
[custom registered functions ](/usage/training#custom-code ) that you can
2020-08-11 21:57:23 +03:00
reference in your config. To get started, check out the
[quickstart widget ](/usage/training#quickstart ).
2020-07-27 19:11:45 +03:00
#### Binary .spacy training data format {#migrating-training-format}
2020-08-11 21:57:23 +03:00
spaCy v3.0 uses a new
[binary training data format ](/api/data-formats#binary-training ) created by
serializing a [`DocBin` ](/api/docbin ), which represents a collection of `Doc`
2020-09-03 14:13:03 +03:00
objects. This means that you can train spaCy pipelines using the same format it
2020-08-11 21:57:23 +03:00
outputs: annotated `Doc` objects. The binary format is extremely **efficient in
2020-08-19 01:28:37 +03:00
storage**, especially when packing multiple documents together. You can convert
your existing JSON-formatted data using the [`spacy convert` ](/api/cli#convert )
command, which outputs `.spacy` files:
2020-08-11 21:57:23 +03:00
2020-08-19 01:28:37 +03:00
```cli
2020-07-27 19:11:45 +03:00
$ python -m spacy convert ./training.json ./output
```
#### Training config {#migrating-training-config}
2020-08-10 02:20:10 +03:00
The easiest way to get started with a training config is to use the
2020-08-18 02:29:34 +03:00
[`init config` ](/api/cli#init-config ) command or the
[quickstart widget ](/usage/training#quickstart ). You can define your
requirements, and it will auto-generate a starter config with the best-matching
default settings.
2020-08-10 02:20:10 +03:00
2020-08-19 01:28:37 +03:00
```cli
2020-08-18 02:29:34 +03:00
$ python -m spacy init config ./config.cfg --lang en --pipeline tagger,parser
2020-08-10 02:20:10 +03:00
```
2020-07-27 19:11:45 +03:00
2020-08-18 02:29:34 +03:00
If you've exported a starter config from our
[quickstart widget ](/usage/training#quickstart ), you can use the
[`init fill-config` ](/api/cli#init-fill-config ) to fill it with all default
values. You can then use the auto-generated `config.cfg` for training:
2020-07-27 19:11:45 +03:00
```diff
2020-08-29 19:43:19 +03:00
- python -m spacy train en ./output ./train.json ./dev.json
--pipeline tagger,parser --cnn-window 1 --bilstm-depth 0
2020-08-06 20:30:43 +03:00
+ python -m spacy train ./config.cfg --output ./output
2020-07-27 19:11:45 +03:00
```
2020-09-20 18:44:58 +03:00
< Project id = "pipelines/tagger_parser_ud" >
The easiest way to get started is to clone a [project template ](/usage/projects )
and run it – for example, this end-to-end template that lets you train a
**part-of-speech tagger** and **dependency parser** on a Universal Dependencies
treebank.
< / Project >
2020-08-19 13:14:41 +03:00
2021-01-12 13:29:31 +03:00
#### Modifying tokenizer settings
If you were using a base model with `spacy train` to customize the tokenizer
settings in v2, your modifications can be provided in the
`[initialize.before_init]` callback.
Write a registered callback that modifies the tokenizer settings and specify
this callback in your config:
> #### config.cfg
>
> ```ini
> [initialize]
>
> [initialize.before_init]
> @callbacks = "customize_tokenizer"
> ```
```python
### functions.py
from spacy.util import registry, compile_suffix_regex
@registry .callbacks("customize_tokenizer")
def make_customize_tokenizer():
def customize_tokenizer(nlp):
# remove a suffix
suffixes = list(nlp.Defaults.suffixes)
suffixes.remove("\\[")
suffix_regex = compile_suffix_regex(suffixes)
nlp.tokenizer.suffix_search = suffix_regex.search
# add a special case
nlp.tokenizer.add_special_case("_SPECIAL_", [{"ORTH": "_SPECIAL_"}])
return customize_tokenizer
```
When training, provide the function above with the `--code` option:
```cli
$ python -m spacy train config.cfg --code ./functions.py
```
The train step requires the `--code` option with your registered functions from
the `[initialize]` block, but since those callbacks are only required during the
initialization step, you don't need to provide them with the final pipeline
package. However, to make it easier for others to replicate your training setup,
you can choose to package the initialization callbacks with the pipeline package
or to publish them separately.
2020-08-11 21:57:23 +03:00
#### Training via the Python API {#migrating-training-python}
2020-07-27 19:11:45 +03:00
2020-08-11 21:57:23 +03:00
For most use cases, you **shouldn't** have to write your own training scripts
anymore. Instead, you can use [`spacy train` ](/api/cli#train ) with a
[config file ](/usage/training#config ) and custom
[registered functions ](/usage/training#custom-code ) if needed. You can even
register callbacks that can modify the `nlp` object at different stages of its
lifecycle to fully customize it before training.
2020-07-27 19:11:45 +03:00
2020-08-11 21:57:23 +03:00
If you do decide to use the [internal training API ](/usage/training#api ) from
Python, you should only need a few small modifications to convert your scripts
from spaCy v2.x to v3.x. The [`Example.from_dict` ](/api/example#from_dict )
classmethod takes a reference `Doc` and a
[dictionary of annotations ](/api/data-formats#dict-input ), similar to the
"simple training style" in spaCy v2.x:
2020-07-29 12:36:42 +03:00
2020-08-11 21:57:23 +03:00
```diff
### Migrating Doc and GoldParse
doc = nlp.make_doc("Mark Zuckerberg is the CEO of Facebook")
entities = [(0, 15, "PERSON"), (30, 38, "ORG")]
- gold = GoldParse(doc, entities=entities)
+ example = Example.from_dict(doc, {"entities": entities})
```
```diff
### Migrating simple training style
text = "Mark Zuckerberg is the CEO of Facebook"
annotations = {"entities": [(0, 15, "PERSON"), (30, 38, "ORG")]}
+ doc = nlp.make_doc(text)
+ example = Example.from_dict(doc, annotations)
```
The [`Language.update` ](/api/language#update ),
[`Language.evaluate` ](/api/language#evaluate ) and
2020-10-08 22:33:49 +03:00
[`TrainablePipe.update` ](/api/pipe#update ) methods now all take batches of
2020-08-11 21:57:23 +03:00
[`Example` ](/api/example ) objects instead of `Doc` and `GoldParse` objects, or
raw text and a dictionary of annotations.
```python
2020-11-30 04:39:45 +03:00
### Training loop {highlight="5-8,12"}
2020-08-11 21:57:23 +03:00
TRAIN_DATA = [
("Who is Shaka Khan?", {"entities": [(7, 17, "PERSON")]}),
("I like London.", {"entities": [(7, 13, "LOC")]}),
]
2020-11-30 04:39:45 +03:00
examples = []
for text, annots in TRAIN_DATA:
examples.append(Example.from_dict(nlp.make_doc(text), annots))
nlp.initialize(lambda: examples)
2020-08-11 21:57:23 +03:00
for i in range(20):
2020-11-30 04:39:45 +03:00
random.shuffle(examples)
for batch in minibatch(examples, size=8):
2021-02-18 11:44:22 +03:00
nlp.update(batch)
2020-08-11 21:57:23 +03:00
```
2020-10-08 22:33:49 +03:00
`Language.begin_training` and `TrainablePipe.begin_training` have been renamed
to [`Language.initialize` ](/api/language#initialize ) and
[`TrainablePipe.initialize` ](/api/pipe#initialize ), and the methods now take a
function that returns a sequence of `Example` objects to initialize the model
instead of a list of tuples. The data examples are used to **initialize the
models** of trainable pipeline components, which includes validating the
network,
2020-08-11 21:57:23 +03:00
[inferring missing shapes ](https://thinc.ai/docs/usage-models#validation ) and
setting up the label scheme.
```diff
2020-11-30 04:39:45 +03:00
- nlp.begin_training()
2020-09-28 22:35:09 +03:00
+ nlp.initialize(lambda: examples)
2020-08-11 21:57:23 +03:00
```
2020-07-29 12:36:42 +03:00
2020-09-03 14:13:03 +03:00
#### Packaging trained pipelines {#migrating-training-packaging}
2020-07-27 19:11:45 +03:00
The [`spacy package` ](/api/cli#package ) command now automatically builds the
installable `.tar.gz` sdist of the Python package, so you don't have to run this
2021-01-30 05:11:36 +03:00
step manually anymore. To disable the behavior, you can set `--build none` . You
can also choose to build a binary wheel (which installs more efficiently) by
setting `--build wheel` , or to build both the sdist and wheel by setting
`--build sdist,wheel` .
2020-07-27 19:11:45 +03:00
```diff
2020-09-03 14:13:03 +03:00
python -m spacy package ./output ./packages
- cd /output/en_pipeline-0.0.0
2020-07-27 19:11:45 +03:00
- python setup.py sdist
```
2020-09-12 18:05:10 +03:00
#### Data utilities and gold module {#migrating-gold}
2020-09-23 11:14:27 +03:00
The `spacy.gold` module has been renamed to `spacy.training` and the conversion
utilities now follow the naming format of `x_to_y` . This mostly affects
internals, but if you've been using the span offset conversion utilities
2020-09-22 12:50:19 +03:00
[`offsets_to_biluo_tags` ](/api/top-level#offsets_to_biluo_tags ),
[`biluo_tags_to_offsets` ](/api/top-level#biluo_tags_to_offsets ) or
[`biluo_tags_to_spans` ](/api/top-level#biluo_tags_to_spans ), you'll have to
change your names and imports:
2020-09-12 18:05:10 +03:00
```diff
2020-09-22 12:50:19 +03:00
- from spacy.gold import biluo_tags_from_offsets, offsets_from_biluo_tags, spans_from_biluo_tags
+ from spacy.training import offsets_to_biluo_tags, biluo_tags_to_offsets, biluo_tags_to_spans
2020-09-12 18:05:10 +03:00
```
2020-08-10 01:01:38 +03:00
#### Migration notes for plugin maintainers {#migrating-plugins}
2020-07-27 01:29:45 +03:00
Thanks to everyone who's been contributing to the spaCy ecosystem by developing
and maintaining one of the many awesome [plugins and extensions ](/universe ).
2020-08-10 01:01:38 +03:00
We've tried to make it as easy as possible for you to upgrade your packages for
2020-11-03 20:10:06 +03:00
spaCy v3.0. The most common use case for plugins is providing pipeline
components and extension attributes. When migrating your plugin, double-check
the following:
2020-07-27 01:29:45 +03:00
- Use the [`@Language.factory` ](/api/language#factory ) decorator to register
your component and assign it a name. This allows users to refer to your
components by name and serialize pipelines referencing them. Remove all manual
entries to the `Language.factories` .
- Make sure your component factories take at least two **named arguments** :
`nlp` (the current `nlp` object) and `name` (the instance name of the added
component so you can identify multiple instances of the same component).
- Update all references to [`nlp.add_pipe` ](/api/language#add_pipe ) in your docs
to use **string names** instead of the component functions.
```python
### {highlight="1-5"}
from spacy.language import Language
@Language .factory("my_component", default_config={"some_setting": False})
def create_component(nlp: Language, name: str, some_setting: bool):
return MyCoolComponent(some_setting=some_setting)
class MyCoolComponent:
def __init__ (self, some_setting):
self.some_setting = some_setting
def __call__ (self, doc):
# Do something to the doc
return doc
```
> #### Result in config.cfg
>
> ```ini
> [components.my_component]
> factory = "my_component"
> some_setting = true
> ```
```diff
import spacy
from your_plugin import MyCoolComponent
nlp = spacy.load("en_core_web_sm")
- component = MyCoolComponent(some_setting=True)
- nlp.add_pipe(component)
+ nlp.add_pipe("my_component", config={"some_setting": True})
```
< Infobox title = "Important note on registering factories" variant = "warning" >
The [`@Language.factory` ](/api/language#factory ) decorator takes care of letting
spaCy know that a component of that name is available. This means that your
users can add it to the pipeline using its **string name** . However, this
requires the decorator to be executed – so users will still have to **import
your plugin**. Alternatively, your plugin could expose an
[entry point ](/usage/saving-loading#entry-points ), which spaCy can read from.
This means that spaCy knows how to initialize `my_component` , even if your
package isn't imported.
< / Infobox >
2021-03-09 17:35:21 +03:00
#### Using GPUs in Jupyter notebooks {#jupyter-notebook-gpu}
In Jupyter notebooks, run [`prefer_gpu` ](/api/top-level#spacy.prefer_gpu ),
[`require_gpu` ](/api/top-level#spacy.require_gpu ) or
[`require_cpu` ](/api/top-level#spacy.require_cpu ) in the same cell as
2021-04-12 15:37:00 +03:00
[`spacy.load` ](/api/top-level#spacy.load ) to ensure that the model is loaded on
the correct device.
2021-03-09 17:35:21 +03:00
2021-04-12 15:37:00 +03:00
Due to a bug related to `contextvars` (see the
[bug report ](https://github.com/ipython/ipython/issues/11565 )), the GPU settings
may not be preserved correctly across cells, resulting in models being loaded on
2021-03-09 17:35:21 +03:00
the wrong device or only partially on GPU.