Context: using hydra
to run experiments with several settings in yaml
files.
Assume workd_dir
is defined in the inference_root.yaml
file, e.g.:
work_dir: ${hydra:runtime.cwd}
Further assume the following experiment.yaml
structure, where we would like to load settings defined in other YAML files and not in the inference_root.yaml
file:
experiment_specific_settings:
option_1: ${work_dir}/setting_v1.yaml
option_2: ${work_dir}/setting_v2.yaml
This will fail to parse the referenced YAML
files. However, adding the following:
def include_constructor(loader, node):
# Get the included file path from the YAML node
filename = loader.construct_scalar(node)
# Load the included file
included_yaml = yaml.load(open(filename), Loader=yaml.SafeLoader)
return included_yaml
# Add the constructor to the SafeConstructor class
yaml.constructor.SafeConstructor.add_constructor('!include', include_constructor)
Combined with placing !include
in front of option_1
and option_2
will.
Unfortunately, the yaml
evaluation happens before the relative paths are evaluated by hydra
, e.g., instead of yaml
evaluating settings_folder/setting_v1.yaml
, it will see ${work_dir}/setting_v1.yaml`, which it won't find.
How do I either:
- read in nested
yaml
file references directly usinghydra
, or; - ensure the relative run-time paths are evaluated before the yaml is parsed?
EDIT per commenter request:
Assume the following directory structure:
configs/
├── inference_root.yaml
└── experiments/
├── my_experiment.yaml
└── ...
└── tires/
├── ....yaml
└── ....yaml
└── engines/
├── ....yaml
└── ....yaml
Experiments let two cars race against each other, car_1
and car_2
. Each car consists of several parts, e.g., tires, engines, doors, each with their own specifications, e.g., tires_1.yaml
, tires_2.yaml
, ... etc.
To ensure repeatable experiments, ideally we would like to structure experimental setup as follows:
inference_root.yaml
defaults:
experiments: my_experiment
my_experiment.yaml
car_1:
tires: tires_1
engine: engine_3
doors: door_81
car_2:
tires: ...
engine: ...
doors: ...
Problems:
- Currently, I am not able to retrieve a
cfg
in myapp.py
, that loads all the nested.yaml
files referenced in themy_experiment.yaml
. - Assume that the nested
YAML
files are not in theconfigs
, but in a different directory who's path is determined at run-time using the following lines ininference_root.yaml
:
# path to work directory
work_dir: ${hydra:runtime.cwd}
# path to data directory
data_dir: ${work_dir}/data
Is there still a way to auto-load these YAML files?
Any guidance would be appreciated!