HyperPyYAML
HyperPyYAML is a Python library that extends the YAML syntax for enhanced interaction with Python objects and better hyperparameter definition. It is actively maintained, with a recent version 1.2.3, and typically releases updates as needed for bug fixes and new features.
Warnings
- breaking HyperPyYAML allows arbitrary code execution through its custom tags, which is a feature for flexible configuration. Treat all YAML files loaded with HyperPyYAML as you would Python code. Loading untrusted YAML files can lead to security vulnerabilities.
- breaking Compatibility issues with `ruamel.yaml` versions have occurred. Specifically, versions of `ruamel.yaml` 0.19.0 and above can cause breakage due to API changes.
- gotcha The `!include` tag in older versions could break due to incorrect handling of overrides, particularly when no overrides were provided or the structure was unexpected.
- gotcha The `!ref` tag creates a shallow reference to the original YAML node, meaning changes to the referenced object will affect all references. If a deep copy is desired, use the `!copy` tag instead.
- deprecated Older versions of HyperPyYAML had issues with Python 3.8+ compatibility, particularly concerning `ast.Constant`.
Install
-
pip install hyperpyyaml
Imports
- load_hyperpyyaml
from hyperpyyaml import load_hyperpyyaml
Quickstart
import torch from hyperpyyaml import load_hyperpyyaml example_hyperparams_yaml = """ base_channels: 32 kernel_size: 11 padding: !ref <kernel_size> // 2 layer1: !new:torch.nn.Conv1d in_channels: 1 out_channels: !ref <base_channels> kernel_size: !ref <kernel_size> padding: !ref <padding> model: !new:torch.nn.Sequential - !ref <layer1> - !new:torch.nn.LeakyReLU """ # Using load_hyperpyyaml to parse the YAML string loaded_hparams = load_hyperpyyaml(example_hyperparams_yaml) print(loaded_hparams['base_channels']) print(loaded_hparams['padding']) print(loaded_hparams['model'])