experiment_runner.lookups
Shared resolution of a config string to the library object it names.
The package is typed against the library types - a torch.nn.Module loss, a
torch.optim.Optimizer subclass, an sklearn-style target encoder - so the
only gap is that YAML cannot hold a Python object. That is a serialization problem
rather than an abstraction, which is why each family is a table plus a dotted-path
escape hatch instead of a registry: there is nothing to curate, and a loss that
does not exist yet still works.
This module owns what the three families share: the type check, the case-folded
table lookup, the retired-name message, the dotted-path import, and the wording of
the error that names the available keys. Each family then constructs in the way
that suits it - an encoder takes no arguments, a loss is built with its parameters,
an optimizer is deferred into a functools.partial() because it needs
model.parameters(), which only exists inside train_model.
Attributes
Classes
One config-string-to-object family. |
Module Contents
- experiment_runner.lookups.logger
- class experiment_runner.lookups.Lookup
One config-string-to-object family.
- Parameters:
table (dict) – Config name (lower case) -> the class or factory it denotes.
key (str) – The config key this family is configured by, used in every message so an error names the YAML key the user wrote (
"encode","loss_func").label (str) – The thing being named, article included (
"an encoder","a loss"), so the sentences read correctly.example (str) – A dotted path to show in the “available” line.
retired (dict, optional) – Names that used to work, mapped to the reason they no longer do. A retired name must say what to use instead: a config carrying one stops running.
- table: dict[str, collections.abc.Callable]
- key: str
- label: str
- example: str
- retired: dict[str, str]
- resolve(name: str) collections.abc.Callable
Return the class or factory that name denotes.
Table lookups are case-folded, because configs in the wild spell a loss
"CrossEntropy"; a dotted path is left alone, sincetorch.nn.BCEWithLogitsLosscannot resolve lower-cased.- Parameters:
name (str) – A key of
table, or a dotted import path to a class.- Returns:
The class or factory. Constructing it is the caller’s business.
- Return type:
Callable
- Raises:
ValueError – If name is not a string, is retired, or resolves to nothing. Falling back to a default instead would run a different experiment and report plausible numbers for it.
- construct(name: str, params: dict | None = None) object
Resolve name and build it with params.
- Parameters:
name (str) – As
resolve().params (dict, optional) – Constructor keyword arguments.
- Returns:
The constructed instance.
- Return type:
object
- Raises:
ValueError – If construction fails. The parameters come from YAML, so a wrong name or a wrong shape is a config error and is reported as one.