Config & Registry
Function registry and configuration system

RegistryFunction registry for layers, optimizers etc.
ConfigConfig class used to load and create INI-style configs.

Registry class

Thinc’s registry system lets you map string keys to functions. You can register functions to create optimizers, schedules, layers and more, and then refer to them and set their arguments in your config file. Python type hints are used to validate the inputs.

Exampleimport thinc

@thinc.registry.optimizers.register("my_cool_optimizer.v1")
def make_my_optimizer(learn_rate: float, gamma: float):
    return MyCoolOptimizer(learn_rate, gamma)
Valid Config[optimizer]
@optimizers = "my_cool_optimizer.v1"
learn_rate = 0.001
gamma = 1e-8
Invalid Config[optimizer]
@optimizers = "my_cool_optimizer.v1"
learn_rate = 1  # not a float
schedules = null  # unknown argument

Attributes

Registry nameDescription
optimizersRegistry for functions that create optimizers.
schedulesRegistry for functions that create schedules.
layersRegistry for functions that create layers.
lossesRegistry for functions that create losses.
initializersRegistry for functions that create initializers.

registry.get classmethod

Get a registered function from a given registry using string names. Will raise an error if the registry or function doesn’t exist. All individual registries also have a get method to get a registered function.

Exampleregistered_func = thinc.registry.get("optimizers", "my_cool_optimizer.v1")
# The above is the same as:
registered_func = thinc.registry.optimizers.get("my_cool_optimizer.v1")
ArgumentTypeDescription
registry_namestrThe name of the registry, e.g. "optimizers".
func_namestrThe name of the function.
RETURNSCallableThe registered function.

registry.create classmethod

Create a new function registry that will become available as an attribute to registry. Will raise an error if a registry of the name already exists. Under the hood, this calls into catalogue.create using the "thinc" namespace.

Examplethinc.registry.create("visualizers")

@thinc.registry.visualizers("my_cool_visualizer.v1")
def my_cool_visualizer(format: str = "jpg") -> "MyCoolVisualizer":
    return MyCoolVisualizer(format)
ArgumentTypeDescription
registry_namestrThe name of the registry to create, e.g. "visualizers".
entry_pointsboolAllow the registry to be populated with entry points advertised by other packages (e.g. via the "thinc_visualizers" entry point group). Defaults to False.

registry.make_from_config classmethod

Unpack a config dictionary, creating objects from the registry recursively. If a section contains a key beginning with @, the rest of that key will be interpreted as the name of the registry. For instance, "@optimizers": "my_cool_optimizer.v1" will load the function from the optimizers registry and pass in the specified arguments. For more details and examples, see the docs on Thinc’s config system.

Examplefrom thinc.api import Config

cfg = Config().from_disk("./my_config.cfg")
C = registry.make_from_config(cfg)
ArgumentTypeDescription
configUnion[Config, Dict[str, Any]]The config dict to load.
keyword-only
validateboolWhether to validate the config against a base schema and/or type annotations defined on the registered functions. Defaults to True.
schemapydantic.BaseModelOptional pydantic model to validate the config against. See the docs on base schemas for details. Defaults to an EmptySchema with extra properties and arbitrary types allowed.
RETURNSConfigThe filled config.

registry.fill_config classmethod

Unpack a config dictionary, but leave all references to registry functions intact and don’t resolve them. Only use the type annotations and optional base schema to fill in all arguments and their default values. This method is especially useful for getting an existing config up to date with changes in the schema and/or function arguments. If the config is incomplete and contains missing values for required arguments, you can set validate=False to skip validation and only update it. The updated schema should then pass validation.

Examplefrom thinc.api import Config

cfg = Config().from_disk("./my_config.cfg")
filled_cfg = registry.fill_config(cfg)
ArgumentTypeDescription
configUnion[Config, Dict[str, Any]]The config dict to load.
keyword-only
validateboolWhether to validate the config against a base schema and/or type annotations defined on the registered functions. Defaults to True.
schemapydantic.BaseModelOptional pydantic model to validate the config against. See the docs on base schemas for details. Defaults to an EmptySchema with extra properties and arbitrary types allowed.
RETURNSConfigThe filled config.

Config class

This class holds the model and training configuration and can load and save the INI-style configuration format from/to a string, file or bytes. The Config class is a subclass of dict and uses Python’s ConfigParser under the hood.

Config.__init__ method

Initialize a new Config object with optional data.

ArgumentTypeDescription
dataOptional[Union[Dict[str, Any], Config]]Optional data to initialize the config with.

Config.from_str method

Load the config from a string.

Examplefrom thinc.api import Config

config_str = """
[training]
patience = 10
dropout = 0.2
"""
config = Config().from_str(config_str)
print(config["training"])  # {'patience': 10, 'dropout': 0.2}}
ArgumentTypeDescription
textstrThe string config to load.
RETURNSConfigThe loaded config.

Config.to_str method

Write the config to a string.

Examplefrom thinc.api import Config

config = Config({"training": {"patience": 10, "dropout": 0.2}})
print(config.to_str()) # '[training]\npatience = 10\n\ndropout = 0.2'
ArgumentTypeDescription
RETURNSstrThe string config.

Config.to_bytes method

Serialize the config to a byte string.

Examplefrom thinc.api import Config

config = Config({"training": {"patience": 10, "dropout": 0.2}})
config_bytes = config.to_bytes()
print(config_bytes)  # b'[training]\npatience = 10\n\ndropout = 0.2'
ArgumentTypeDescription
RETURNSbytesThe serialized config.

Config.from_bytes method

Load the config from a byte string.

Examplefrom thinc.api import Config

config = Config({"training": {"patience": 10, "dropout": 0.2}})
config_bytes = config.to_bytes()
new_config = Config().from_bytes(config_bytes)
ArgumentTypeDescription
bytes_databytesThe data to load.
RETURNSConfigThe loaded config.

Config.to_disk method

Serialize the config to a file.

Examplefrom thinc.api import Config

config = Config({"training": {"patience": 10, "dropout": 0.2}})
config.to_disk("./config.cfg")
ArgumentTypeDescription
pathUnion[Path, str]The file path.

Config.from_disk method

Load the config from a file.

Examplefrom thinc.api import Config

config = Config({"training": {"patience": 10, "dropout": 0.2}})
config.to_disk("./config.cfg")
new_config = Config().from_disk("./config.cfg")
ArgumentTypeDescription
pathUnion[Path, str]The file path.
RETURNSConfigThe loaded config.