spaCy/website/docs/usage/saving-loading.jade

118 lines
4.4 KiB
Plaintext
Raw Normal View History

2017-04-16 18:36:09 +00:00
include ../../_includes/_mixins
2017-05-24 09:59:08 +00:00
+h(2, "models") Saving models
2017-04-16 18:36:09 +00:00
p
| After training your model, you'll usually want to save its state, and load
2017-04-16 23:41:46 +00:00
| it back later. You can do this with the
2017-05-23 21:15:31 +00:00
| #[+api("language#to_disk") #[code Language.to_disk()]]
2017-04-16 18:36:09 +00:00
| method:
+code.
2017-05-23 21:15:31 +00:00
nlp.to_disk('/home/me/data/en_example_model')
2017-04-16 18:36:09 +00:00
p
| The directory will be created if it doesn't exist, and the whole pipeline
| will be written out. To make the model more convenient to deploy, we
| recommend wrapping it as a Python package.
2017-05-24 09:59:08 +00:00
+h(3, "models-generating") Generating a model package
2017-04-16 18:36:09 +00:00
+infobox("Important note")
| The model packages are #[strong not suitable] for the public
| #[+a("https://pypi.python.org") pypi.python.org] directory, which is not
| designed for binary data and files over 50 MB. However, if your company
2017-05-24 09:59:08 +00:00
| is running an #[strong internal installation] of PyPi, publishing your
| models on there can be a convenient way to share them with your team.
2017-04-16 18:36:09 +00:00
p
2017-04-16 23:45:00 +00:00
| spaCy comes with a handy CLI command that will create all required files,
2017-04-16 18:36:09 +00:00
| and walk you through generating the meta data. You can also create the
| meta.json manually and place it in the model data directory, or supply a
2017-05-24 09:59:08 +00:00
| path to it using the #[code --meta] flag. For more info on this, see
| the #[+api("cli#package") #[code package]] docs.
2017-04-16 18:36:09 +00:00
+aside-code("meta.json", "json").
{
"name": "example_model",
"lang": "en",
"version": "1.0.0",
2017-05-24 09:59:08 +00:00
"spacy_version": ">=2.0.0,<3.0.0",
2017-04-16 18:36:09 +00:00
"description": "Example model for spaCy",
"author": "You",
"email": "you@example.com",
"license": "CC BY-SA 3.0"
}
+code(false, "bash").
python -m spacy package /home/me/data/en_example_model /home/me/my_models
p This command will create a model package directory that should look like this:
+code("Directory structure", "yaml").
└── /
├── MANIFEST.in # to include meta.json
├── meta.json # model meta data
├── setup.py # setup file for pip installation
└── en_example_model # model directory
├── __init__.py # init for pip installation
└── en_example_model-1.0.0 # model data
p
| You can also find templates for all files in our
2017-05-24 09:59:08 +00:00
| #[+src(gh("spacy-dev-resouces", "templates/model")) spaCy dev resources].
2017-04-16 18:36:09 +00:00
| If you're creating the package manually, keep in mind that the directories
| need to be named according to the naming conventions of
2017-04-26 12:00:40 +00:00
| #[code [language]_[name]] and #[code [language]_[name]-[version]]. The
2017-04-16 18:36:09 +00:00
| #[code lang] setting in the meta.json is also used to create the
| respective #[code Language] class in spaCy, which will later be returned
| by the model's #[code load()] method.
p
2017-05-24 09:59:08 +00:00
| To #[strong build the package], run the following command from within the
2017-04-16 18:36:09 +00:00
| directory. This will create a #[code .tar.gz] archive in a directory
2017-05-24 09:59:08 +00:00
| #[code /dist]. For more information on building Python packages, see the
| #[+a("https://setuptools.readthedocs.io/en/latest/") Python Setuptools documentation].
2017-04-16 18:36:09 +00:00
+code(false, "bash").
python setup.py sdist
2017-05-24 09:59:08 +00:00
+h(2, "loading") Loading a custom model package
2017-04-16 18:36:09 +00:00
2017-05-24 09:59:08 +00:00
p
| To load a model from a data directory, you can use
| #[+api("spacy#load") #[code spacy.load()]] with the local path:
2017-04-16 18:36:09 +00:00
2017-05-24 09:59:08 +00:00
+code.
nlp = spacy.load('/path/to/model')
2017-04-16 18:36:09 +00:00
p
2017-05-24 09:59:08 +00:00
| If you have generated a model package, you can also install it by
| pointing pip to the model's #[code .tar.gz] archive this is pretty
| much exactly what spaCy's #[+api("cli#download") #[code download]]
| command does under the hood.
2017-04-16 18:36:09 +00:00
+code(false, "bash").
pip install /path/to/en_example_model-1.0.0.tar.gz
2017-05-24 09:59:08 +00:00
+aside-code("Custom model names", "bash").
# optional: assign custom name to model
python -m spacy link en_example_model my_cool_model
p
| You'll then be able to load the model via spaCy's loader, or by importing
| it as a module. For larger code bases, we usually recommend native
| imports, as this will make it easier to integrate models with your
| existing build process, continuous integration workflow and testing
| framework.
2017-04-16 18:36:09 +00:00
+code.
2017-05-24 09:59:08 +00:00
# option 1: import model as module
2017-04-16 18:36:09 +00:00
import en_example_model
nlp = en_example_model.load()
2017-05-24 09:59:08 +00:00
# option 2: use spacy.load()
nlp = spacy.load('en_example_model')