Original Note

QuickStart - Read

QuickStart

正文

主要整理原始教程

Installation

Hydra 1.4 is coming soon. Until the stable release is available, you can try the development release:

pip install --pre --upgrade hydra-core

For the latest stable Hydra release, install:

pip install hydra-core --upgrade

Basic example

Config:

conf/config.yaml

db:  
driver: mysql  
user: omry  
pass: secret

Application:

import hydra
from omegaconf import DictConfig, OmegaConf

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

if __name__ == "__main__":
    my_app()

You can override values in the loaded config from the command line:

$ python my_app.py db.user=root db.pass=1234
db:
  driver: mysql
  user: root
  pass: 1234

同时跑多个配置

Hydra 提供了更方便的写法:

python my_app.py --multirun db=mysql,postgresql

也可以简写成:

python my_app.py -m db=mysql,postgresql

意思是:

用 db=mysql 跑一次,再用 db=postgresql 跑一次。

输出示例

$ python my_app.py --multirun db=mysql,postgresql
[HYDRA] Sweep output dir : multirun/2020-01-09/01-16-29
[HYDRA] Launching 2 jobs locally
[HYDRA]        #0 : db=mysql
db:
  driver: mysql
  pass: secret
  user: omry

[HYDRA]        #1 : db=postgresql
db:
  driver: postgresql
  pass: drowssap
  timeout: 10
  user: postgres_user