skip to Main Content

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


  1. 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 and hydra.experimental.compose functions.

    from hydra.experimental import compose, initialize
    
    initialize(config_path="conf")
    cfg = compose(config_name="config.yaml")
    print(cfg.paths.root)
    

    This will set up the Hydra context and allow you to use Hydra’s interpolation feature in Jupyter notebook.

    Login or Signup to reply.
  2. 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 the hydra key in config, which has to be present.

    ":" ${hydra:runtime} is a OmegaConf resolver underneath accessing the HydraConfig object that needs Hydra to be initialized through main. 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:

    # Initialize Hydra and compose the configuration
    with initialize(version_base=None, config_path="conf"):
        cfg = compose(config_name="config.yaml", return_hydra_config=True)
        # NOTE: . not :
        cfg.paths.root = "${hydra.runtime.cwd}/data/MNIST" # or change in your, file 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search