hydra
install するのは hydra-core
ハマる
dataclass にメソッド生やせない
えー 設定値から Path 返したりしたいでしょ
dataclass で str | list[str] にできない
まあ気持ちはわかるけど
基本
code:tree
.
├── config.yaml
├── exp
│ └── 001.yaml
└── train.py
code:config.yaml
defaults:
- _self_
- exp: default
# config の値のデフォルト
key: value
# exp の値のデフォルト
exp:
...
hydra:
output_subdir: null
job:
chdir: false
run:
dir: .
code:exp/001.yaml
defaults:
- default@_here_
# exp の上書きする定義いろいろ
code:train.py
from dataclasses import dataclass
import hydra
from hydra.core.config_store import ConfigStore
@dataclass
class ExpConfig: ...
@dataclass
class config:
exp: ExpConfig
# 他の group があれば任意に書く
cs = ConfigStore.instance()
cs.store(name="default", group="exp", node=ExpConfig)
@hydra.main(version_base=None, config_path=".", config_name="config")
def main(cfg: Config) -> None:
...
$ python train.py exp=001
$ python train.py exp=001 exp.flod=0 jupyter 等から呼ぶ
code:from_jupyter.py
from hydra import compose, initialize
from hydra.core.config_store import ConfigStore
from dataclasses import field
@dataclass
class TrainConfig: ...
@datacalss
class ExpConfig: ...
@dataclass
class Config:
exp: ExpConfig
env: EnvConfig
cs = ConfigStore.instance()
cs.store(name="default", group="env", node=EnvConfig)
cs.store(name="default", group="exp", node=ExpConfig)
# notebook 自身のパス取得するのは hacky なので素直に書く必要ある
with initialize(version_bias=None, config_path="/path/to/dir/from/jupyter/cwd"):
cfg = compose(
config_name="config.yaml", # .yaml もつける
overrides=["exp.name=foo", "exp.fold=0"], )