Reputation: 153
I have a config tree such as:
config.yaml
model/
model_a.yaml
model_b.yaml
model_c.yaml
Where config.yaml
contains:
# @package _global_
defaults:
- _self_
- model: model_a.yaml
some_var: 42
I would like to access the name of the model config file used (either the default or overridden) from my python code or from the file itself. Something like:
@hydra.main(...)
def main(config):
model_name = config.model.__filename__
or (from e.g. model_a.yaml
)
dropout: true
dense_layers: 128
model_name: ${__filename__}
Thanks in advance!
Upvotes: 6
Views: 4501
Reputation: 7639
The a look at the hydra.runtime.choices variable mentioned in the Configuring Hydra - Introduction page of the Hydra docs. This variable stores a mapping that describes each of the choices that Hydra has made in composing the output config.
Using your example from above with model: model_a.yaml
in the defaults list:
# my_app.py
import hydra
from pprint import pprint
from hydra.core.hydra_config import HydraConfig
from omegaconf import OmegaConf
@hydra.main(config_path=".", config_name="config")
def main(config):
hydra_cfg = HydraConfig.get()
print("choice of model:")
pprint(OmegaConf.to_container(hydra_cfg.runtime.choices))
main()
At the command line:
$ python3 app.py
choices used:
{'hydra/callbacks': None,
'hydra/env': 'default',
'hydra/help': 'default',
'hydra/hydra_help': 'default',
'hydra/hydra_logging': 'default',
'hydra/job_logging': 'default',
'hydra/launcher': 'basic',
'hydra/output': 'default',
'hydra/sweeper': 'basic',
'model': 'model_a.yaml'}
As you can see, in this example the config option model_a.yaml
is stored in the Hydra config at hydra_cfg.runtime.choices.model
.
Upvotes: 9