![](https://pypi-camo.freetls.fastly.net/657dbf4006ad44b146bd59acc7f61d67177af1a5/68747470733a2f2f6578706c6f73696f6e2e61692f6173736574732f696d672f6c6f676f2e737667)
Thinc: A refreshing functional take on deep learning, compatible with your favorite libraries
Thinc is a lightweight deep learning library that offers an elegant,
type-checked, functional-programming API for composing models, with support
for layers defined in other frameworks such as PyTorch and TensorFlow. You
can use Thinc as an interface layer, a standalone toolkit or a flexible way to
develop new models. Previous versions of Thinc have been running quietly in
production in thousands of companies, via both spaCy and
Prodigy. We wrote the new version to let users compose,
configure and deploy custom models built with their favorite framework.
🔮 Version 8 out now! Read the release notes here.
![Open demo in Colab](https://pypi-camo.freetls.fastly.net/05d76fb4a3c2fd3d69b37354816bff244418ad65/68747470733a2f2f6769737463646e2e6769746861636b2e636f6d2f696e65732f64636633353461613731613736363561653139383731643766643134613465302f7261772f343631666331663631613762633538363066393433636434623662636661626238633839303665372f636f6c61622d62616467652e737667)
🔥 Features
- Type-check your model definitions with custom types and
mypy
plugin.
- Wrap PyTorch and TensorFlow models for use in your network.
- Concise functional-programming approach to model definition, using composition rather than inheritance.
- Optional custom infix notation via operator overloading.
- Integrated config system to describe trees of objects and hyperparameters.
- Choice of extensible backends, including JAX support (experimental).
- Read more →
🚀 Quickstart
Thinc is compatible with Python 3.6+ and runs on Linux,
macOS and Windows. The latest releases are available from
pip and
conda. Both installations should come
with binary wheels.
pip install thinc
conda install -c conda-forge thinc
See the extended installation docs for details on optional dependencies for different backends and GPU. You might also want to set up static type checking to take advantage of Thinc's type system.
📓 Selected examples and notebooks
Also see the /examples
directory and usage documentation for more examples. Most examples are Jupyter notebooks – to launch them on Google Colab (with GPU support!) click on the button next to the notebook name.
Notebook |
Description |
intro_to_thinc
![Open in Colab](https://pypi-camo.freetls.fastly.net/05d76fb4a3c2fd3d69b37354816bff244418ad65/68747470733a2f2f6769737463646e2e6769746861636b2e636f6d2f696e65732f64636633353461613731613736363561653139383731643766643134613465302f7261772f343631666331663631613762633538363066393433636434623662636661626238633839303665372f636f6c61622d62616467652e737667) |
Everything you need to know to get started. Composing and training a model on the MNIST data, using config files, registering custom functions and wrapping PyTorch and TensorFlow models. |
transformers_tagger_bert
![Open in Colab](https://pypi-camo.freetls.fastly.net/05d76fb4a3c2fd3d69b37354816bff244418ad65/68747470733a2f2f6769737463646e2e6769746861636b2e636f6d2f696e65732f64636633353461613731613736363561653139383731643766643134613465302f7261772f343631666331663631613762633538363066393433636434623662636661626238633839303665372f636f6c61622d62616467652e737667) |
How to use Thinc, transformers and PyTorch to train a part-of-speech tagger. From model definition and config to the training loop. |
pos_tagger_basic_cnn
![Open in Colab](https://pypi-camo.freetls.fastly.net/05d76fb4a3c2fd3d69b37354816bff244418ad65/68747470733a2f2f6769737463646e2e6769746861636b2e636f6d2f696e65732f64636633353461613731613736363561653139383731643766643134613465302f7261772f343631666331663631613762633538363066393433636434623662636661626238633839303665372f636f6c61622d62616467652e737667) |
Implementing and training a basic CNN for part-of-speech tagging model without external dependencies and using different levels of Thinc's config system. |
parallel_training_ray
![Open in Colab](https://pypi-camo.freetls.fastly.net/05d76fb4a3c2fd3d69b37354816bff244418ad65/68747470733a2f2f6769737463646e2e6769746861636b2e636f6d2f696e65732f64636633353461613731613736363561653139383731643766643134613465302f7261772f343631666331663631613762633538363066393433636434623662636661626238633839303665372f636f6c61622d62616467652e737667) |
How to set up synchronous and asynchronous parameter server training with Thinc and Ray. |
View more →
📖 Documentation & usage guides
🗺 What's where
Module |
Description |
thinc.api |
User-facing API. All classes and functions should be imported from here. |
thinc.types |
Custom types and dataclasses. |
thinc.model |
The Model class. All Thinc models are an instance (not a subclass) of Model . |
thinc.layers |
The layers. Each layer is implemented in its own module. |
thinc.shims |
Interface for external models implemented in PyTorch, TensorFlow etc. |
thinc.loss |
Functions to calculate losses. |
thinc.optimizers |
Functions to create optimizers. Currently supports "vanilla" SGD, Adam and RAdam. |
thinc.schedules |
Generators for different rates, schedules, decays or series. |
thinc.backends |
Backends for numpy , cupy and jax . |
thinc.config |
Config parsing and validation and function registry system. |
thinc.util |
Utilities and helper functions. |
🐍 Development notes
Thinc uses black
for auto-formatting, flake8
for linting and mypy
for type checking. All code is written compatible with Python 3.6+, with type hints wherever possible. See the type reference for more details on Thinc's custom types.
👷♀️ Building Thinc from source
Building Thinc from source requires the full dependencies listed in requirements.txt
to be installed. You'll also need a compiler to build the C extensions.
git clone https://github.com/explosion/thinc
cd thinc
python -m venv .env
source .env/bin/activate
export PYTHONPATH=`pwd`
pip install -r requirements.txt
python setup.py build_ext --inplace
🚦 Running tests
Thinc comes with an extensive test suite. The following should all pass and not report any warnings or errors:
python -m pytest thinc # test suite
python -m mypy thinc # type checks
python -m flake8 thinc # linting
To view test coverage, you can run python -m pytest thinc --cov=thinc
. We aim for a 100% test coverage. This doesn't mean that we meticulously write tests for every single line – we ignore blocks that are not relevant or difficult to test and make sure that the tests execute all code paths.