I know that hydra is only limited when it comes to using it in notebooks but the reality is that many projects have at least some notebooks that might need the same set of configurations.
I would like to know how to load variables in a notebook that use config variables like:
conf.yaml
paths:
root: ${hydra:runtime.cwd}/data/MNIST
without getting an error:
In notebook
# Initialize Hydra and compose the configuration
with initialize(version_base=None, config_path="conf"):
cfg = compose(config_name="config.yaml")
cfg.paths.root
InterpolationResolutionError: ValueError raised while resolving interpolation: HydraConfig was not set
full_key: paths.root
object_type=dict
2
Answers
In a Jupyter notebook, this context is not automatically set up.
To use Hydra’s interpolation feature in a Jupyter notebook, you have to manually set up the Hydra context by using the
hydra.experimental.initialize
andhydra.experimental.compose
functions.This will set up the Hydra context and allow you to use Hydra’s interpolation feature in Jupyter notebook.
There is a tiny but large difference between
${hydra:runtime}
and${hydra.runtime}
, the first is a resolver, i.e. a function, the second a interpolation."." The later
.
will look for thehydra
key in config, which has to be present.":"
${hydra:runtime}
is a OmegaConf resolver underneath accessing theHydraConfig
object that needs Hydra to be initialized throughmain
. Currently does the compose API not offer this. See also this answer from the maintainer: https://stackoverflow.com/a/78168972/12439683.A solution to your problem is to include the hydra dict in your
cfg
variable: