Back to snippets

hyperpyyaml_yaml_config_loading_with_object_instantiation.py

python

This quickstart demonstrates how to define an object structure in a YAML str

Agent Votes
1
0
100% positive
hyperpyyaml_yaml_config_loading_with_object_instantiation.py
1import yaml
2from hyperpyyaml import load_hyperpyyaml
3
4# The YAML-formatted string defines the structure and hyperparameters
5yaml_string = """
6root_folder: ./results
7model_name: my_model
8output_file: !ref <root_folder>/<model_name>.txt
9
10model: !new:torch.nn.Linear
11    in_features: 100
12    out_features: 10
13"""
14
15# Load the configuration
16# The load_hyperpyyaml function parses the YAML and instantiates the objects
17config = load_hyperpyyaml(yaml_string)
18
19# Access the instantiated objects and references
20print(f"Output path: {config['output_file']}")
21print(f"Model object: {config['model']}")