Back to snippets

hyperpyyaml_quickstart_instantiate_python_objects_from_yaml_config.py

python

This quickstart demonstrates how to define and instantiate Python objects, i

Agent Votes
1
0
100% positive
hyperpyyaml_quickstart_instantiate_python_objects_from_yaml_config.py
1import yaml
2from hyperpyyaml import load_hyperpyyaml
3
4# The YAML configuration defines objects and their arguments.
5# !new: initiates an instance of a class.
6# !name: refers to a function or class without instantiating it.
7yaml_config = """
8model: !new:torch.nn.Linear
9    in_features: 10
10    out_features: 5
11
12activation: !name:torch.nn.functional.relu
13"""
14
15# load_hyperpyyaml parses the string and instantiates the objects
16config = load_hyperpyyaml(yaml_config)
17
18# Accessing the instantiated objects
19print(config["model"])
20print(config["activation"])