espnet2.asr.state_spaces.utils.instantiate
espnet2.asr.state_spaces.utils.instantiate
espnet2.asr.state_spaces.utils.instantiate(registry, config, *args, partial=False, wrap=None, **kwargs)
Instantiate a registered module from the given configuration.
This function retrieves a callable from the provided registry using the configuration dictionary. It can either directly instantiate the object or return a partial function that can be called later. The configuration dictionary should contain a key ‘name’ which indicates the target callable to instantiate.
- Parameters:
- registry (dict) – A dictionary mapping names to functions or target paths (e.g. {‘model’: ‘models.SequenceModel’}).
- config (dict or str) – A configuration dictionary that must contain a ‘name’ key to specify which element of the registry to use, along with any additional keyword arguments for the target constructor.
- *args – Additional positional arguments to override the config and be passed to the target constructor.
- partial (bool , optional) – If True, returns a partial object instead of instantiating it. Defaults to False.
- wrap (Callable , optional) – A function to wrap the target class, e.g., an EMA optimizer or a task wrapper. Defaults to None.
- **kwargs – Additional keyword arguments to override the config and be passed to the target constructor.
- Returns: The instantiated object if partial is False, : otherwise a partial object.
- Return type: object
- Raises:NotImplementedError – If the target retrieved from the registry is neither a string nor a callable.
Examples
Example 1: Instantiating a model
model_config = {
‘name’: ‘model_name’, ‘param1’: value1, ‘param2’: value2
} model = instantiate(registry, model_config)
Example 2: Creating a partial function
optimizer_config = {
‘name’: ‘optimizer_name’, ‘lr’: 0.01
} partial_optimizer = instantiate(registry, optimizer_config, partial=True)
NOTE
Ensure that the ‘name’ key in the config is correctly set to match the keys in the registry.