Config & Registry
Config | Config class used to load and create INI-style configs. |
Registry | Function registry for layers, optimizers etc. |
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.
Examplefrom thinc.api import Config
config = Config({"training": {"patience": 10, "dropout": 0.2}})
Argument | Type | Description |
---|---|---|
data | Optional[Union[Dict[str, Any], Config]] | Optional data to initialize the config with. |
keyword-only | ||
section_order | Optional[List[str]] | Top-level section names, in order, used to sort the saved and loaded config. All other sections will be sorted alphabetically. |
is_interpolated | Optional[bool] | Whether the config is interpolated or whether it contains variables. Read from the data if it’s an instance of Config and otherwise defaults to True . |
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}}
Argument | Type | Description |
---|---|---|
text | str | The string config to load. |
keyword-only | ||
interpolate | bool | Whether to interpolate variables like ${section.key} . Defaults to True . |
overrides | Dict[str, Any] | Overrides for values and sections. Keys are provided in dot notation, e.g. "training.dropout" mapped to the value. |
RETURNS | Config | The 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'
Argument | Type | Description |
---|---|---|
interpolate | bool | Whether to interpolate variables like ${section.key} . Defaults to True . |
RETURNS | str | The 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'
Argument | Type | Description |
---|---|---|
keyword-only | ||
interpolate | bool | Whether to interpolate variables like ${section.key} . Defaults to True . |
overrides | Dict[str, Any] | Overrides for values and sections. Keys are provided in dot notation, e.g. "training.dropout" mapped to the value. |
RETURNS | bytes | The 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)
Argument | Type | Description |
---|---|---|
bytes_data | bytes | The data to load. |
keyword-only | ||
interpolate | bool | Whether to interpolate variables like ${section.key} . Defaults to True . |
RETURNS | Config | The 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")
Argument | Type | Description |
---|---|---|
path | Union[Path, str] | The file path. |
keyword-only | ||
interpolate | bool | Whether to interpolate variables like ${section.key} . Defaults to True . |
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")
Argument | Type | Description |
---|---|---|
path | Union[Path, str] | The file path. |
keyword-only | ||
interpolate | bool | Whether to interpolate variables like ${section.key} . Defaults to True . |
overrides | Dict[str, Any] | Overrides for values and sections. Keys are provided in dot notation, e.g. "training.dropout" mapped to the value. |
RETURNS | Config | The loaded config. |
Config.copy method
Deep-copy the config.
Argument | Type | Description |
---|---|---|
RETURNS | Config | The copied config. |
Config.interpolate method
Interpolate variables like
${section.value}
or ${section.subsection}
and return a copy of the config
with interpolated values. Can be used if a config is loaded with
interpolate=False
, e.g. via Config.from_str
.
Examplefrom thinc.api import Config
config_str = """
[hyper_params]
dropout = 0.2
[training]
dropout = ${hyper_params.dropout}
"""
config = Config().from_str(config_str, interpolate=False)
print(config["training"]) # {'dropout': '${hyper_params.dropout}'}}
config = config.interpolate()
print(config["training"]) # {'dropout': 0.2}}
Argument | Type | Description |
---|---|---|
RETURNS | Config | A copy of the config with interpolated values. |
Config.merge method
Deep-merge two config objects, using the current config as the default. Only
merges sections and dictionaries and not other values like lists. Values that
are provided in the updates are overwritten in the base config, and any new
values or sections are added. If a config value is a variable like
${section.key}
(e.g. if the config was loaded with interpolate=False
), the
variable is preferred, even if the updates provide a different value. This
ensures that variable references aren’t destroyed by a merge.
Examplefrom thinc.api import Config
base_config_str = """
[training]
patience = 10
dropout = 0.2
"""
update_config_str = """
[training]
dropout = 0.1
max_epochs = 2000
"""
base_config = Config().from_str(base_config_str)
update_config = Config().from_str(update_config_str)
merged = Config(base_config).merge(update_config)
print(merged["training"]) # {'patience': 10, 'dropout': 1.0, 'max_epochs': 2000}
Argument | Type | Description |
---|---|---|
updates | Union[Dict[str, Any], Config] | The updates to merge into the config. |
RETURNS | Config | A new config instance containing the merged config. |
Config Attributes
Name | Type | Description |
---|---|---|
is_interpolated | bool | Whether the config values have been interpolated. Defaults to True and is set to False if a config is loaded with interpolate=False , e.g. using Config.from_str . |
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 name | Description |
---|---|
optimizers | Registry for functions that create optimizers. |
schedules | Registry for functions that create schedules. |
layers | Registry for functions that create layers. |
losses | Registry for functions that create losses. |
initializers | Registry 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.
Exampleimport thinc
registered_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")
Argument | Type | Description |
---|---|---|
registry_name | str | The name of the registry, e.g. "optimizers" . |
func_name | str | The name of the function. |
RETURNS | Callable | The 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.
Exampleimport thinc
thinc.registry.create("visualizers")
@thinc.registry.visualizers("my_cool_visualizer.v1")
def my_cool_visualizer(format: str = "jpg") -> "MyCoolVisualizer":
return MyCoolVisualizer(format)
Argument | Type | Description |
---|---|---|
registry_name | str | The name of the registry to create, e.g. "visualizers" . |
entry_points | bool | Allow 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.fill 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.
If the provided Config
still includes references to variables, e.g.
if it was loaded with interpolate=False
using a method like
Config.from_str
, a copy of the config is interpolated so
it can be filled, and a filled version with the variables intact is returned.
This means you can auto-fill partial config, without destroying the variables.
Examplefrom thinc.api import Config, registry
cfg = Config().from_disk("./my_config.cfg")
filled_cfg = registry.fill(cfg)
Argument | Type | Description |
---|---|---|
config | Union[Config, Dict[str, Any]] | The config dict to load. |
keyword-only | ||
validate | bool | Whether to validate the config against a base schema and/or type annotations defined on the registered functions. Defaults to True . |
schema | pydantic.BaseModel | Optional 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. |
overrides | Dict[str, Any] | Optional overrides for config values. Should be a dictionary keyed by config properties with dot notation, e.g. {"training.batch_size": 128} . |
RETURNS | Config | The filled config. |
registry.resolve 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, registry
cfg = Config().from_disk("./my_config.cfg")
resolved = registry.resolve(cfg)
Argument | Type | Description |
---|---|---|
config | Union[Config, Dict[str, Any]] | The config dict to load. |
keyword-only | ||
validate | bool | Whether to validate the config against a base schema and/or type annotations defined on the registered functions. Defaults to True . |
schema | pydantic.BaseModel | Optional 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. |
overrides | Dict[str, Any] | Optional overrides for config values. Should be a dictionary keyed by config properties with dot notation, e.g. {"training.batch_size": 128} . |
RETURNS | Dict[str, Any] | The resolved config. |