Specify Hydra multirun sweeps in a config file

Viewed 829

I would like to run a Hydra multirun, but specify the sweeps in a config file. I would like to know if there is a way to do this before asking for a feature request.

So far what I have tried is the following:

Tree structure:

.
├── conf
│   ├── compile
│   │   ├── base.yaml
│   │   └── grid_search.yaml
│   └── config.yaml
└── my_app.py

Content of my_appy.py:

import hydra
from omegaconf import DictConfig, OmegaConf

@hydra.main(config_path="conf", config_name="config")
def my_app(cfg : DictConfig) -> None:
    print(OmegaConf.to_yaml(cfg, resolve=True))

if __name__ == "__main__":
    my_app()

Content of conf/config.yaml:

defaults:
  - compile: base

Content of conf/compile/base.yaml:

loss: mse
optimizer: adam

Content of conf/compile/grid_search.yaml:

defaults:
  - base

lr: 1e-2,1e-3,1e-4

When I run python my_app.py -m compile=grid_search, I get the following output:

[2022-01-07 10:08:05,414][HYDRA] Launching 1 jobs locally
[2022-01-07 10:08:05,414][HYDRA]        #0 : compile=grid_search
compile:
  loss: mse
  optimizer: adam
  lr: 1e-2,1e-3,1e-4

This is an output I understand, because in this example there is no way to tell the difference between a config variable holding a list, and a config variable over which you want to sweep. Is there to indicate such a thing in a config file?

Basically I would like to be able to specify my grid searches in config files rather than in the command line or in shell scripts.

Bonus question: how would this be done for a sweep specified by a dictionary override, as in this issue?

1 Answers

I had also asked this question on GitHub, and got an answer. In conf/experiment/grid_search.yaml, you can have:

# @package _global_
hydra:
  sweeper:
    params:
      +compile.lr: 1e-2,1e-3,1e-4

Then you can run:

python my_app.py -m +experiment=grid_search

In order to have a sweep defined over a dictionary (the bonus part of my question), you can replace the final configuration line by:

+compile: "{lr:1e-2,wd:1e-4},{lr:1e-3,wd:1e-5}"

One big caveat: this will only be available in Hydra version 1.2! (pip install --upgrade hydra-core==1.2.0.dev2)

Related