HParams

class yahp.hparams.Hparams[source]

A collection of hyperparameters with names, types, values, and documentation.

Extends dataclasses.Dataclass.

hparams_registry

This registry maps field names (correspond to abstract types) to the concrete classes that they could be.

See the Registry Example for a walkthrough on how the registry works.

Type

Dict[str, Dict[str, Type[Hparams]]]

classmethod create(f: Union[str, None, TextIO, pathlib.PurePath] = None, data: Optional[Dict[str, JSON]] = None, cli_args: Union[List[str], bool] = True) THparams[source]

Create a instance of Hparams.

Parameters
  • f (Union[str, None, TextIO, pathlib.PurePath], optional) – If specified, load values from a YAML file. Can be either a filepath or file-like object. Cannot be specified with data.

  • data (Optional[Dict[str, JSON]], optional) – If specified, uses this dictionary for instantiating the Hparams. Cannot be specified with f.

  • cli_args (Union[List[str], bool], optional) – CLI argument overrides. If True (the default), load CLI arguments from sys.argv. If False, then do not use any CLI arguments.

Returns

An instance of the class.

Return type

Hparams

classmethod dump(output: TextIO, add_docs: bool = True, typing_column: int = 45, interactive: bool = False) None[source]

Generate a YAML template for Hparams and save the template to a file.

Parameters
  • output (TextIO) – File-like object to which to save the template.

  • add_docs (bool, optional) – Whether to add docs (as comments) to the YAML. Defaults to True.

  • typing_column (int, optional) – Column at which to add documentation. Defaults to 45. Ignored if If add_docs is False.

  • interactive (bool, optional) – Whether to interactively generate the template. Defaults to False.

classmethod dumps(add_docs: bool = False, typing_column: int = 45, interactive: bool = False) str[source]

Generate a YAML template for Hparams and returns the generated YAML as a string.

Parameters
  • add_docs (bool, optional) – Whether to add docs (as comments) to the YAML. Defaults to True.

  • typing_column (int, optional) – Column at which to add documentation. Defaults to 45. Ignored if If add_docs is False.

  • interactive (bool, optional) – Whether to interactively generate the template. Defaults to False.

Returns

The generated YAML, as a string.

initialize_object(*args: Any, **kwargs: Any) Any[source]

Optional method to initialize an associated object from the Hparams.

Returns

The initialized object.

classmethod register_class(field: str, register_class: Type[yahp.hparams.Hparams], class_key: str) None[source]

Dynamically add additional entries into the Hparams.hparams_registry.

For abstract fields whose concrete classes are listed in the Hparams.hparams_registry, this function registers additional fields in the registry.

Parameters
  • field (str) – The field name

  • register_class (Type[Hparams]) – The additional class to register.

  • class_key (str) – The identifier to specify the class in CLI args and YAML.

to_dict() Dict[str, JSON][source]

Convert this object into a dict.

Returns

The instance, as a JSON dictionary.

to_yaml(**yaml_args: Any) str[source]

Serialize the object to a YAML string.

Parameters

yaml_args – Extra arguments to pass into yaml.dump().

Returns

The object, as a yaml string.

validate()[source]

Validate is deprecated

classmethod validate_keys(keys: List[str], *, allow_missing_keys: bool = False, allow_extra_keys: bool = False) None[source]

Validates that keys matches the fields of the Hparams.

Parameters
  • keys (List[str]) – Keys to validate.

  • allow_missing_keys (bool, optional) – Whether to ignore fields that do not have default values and are also not specified in keys. Defaults to False.

  • allow_extra_keys (bool, optional) – Whether to allow extra members of keys that are not present in the Hparams. Defaults to False.

Raises

ValueError – Raised if there are missing or extra keys.