From hydra-experiment
Hydra による設定管理パターンをガイドする。 Use when user mentions "hydra", "hydra.main", "ConfigStore", "OmegaConf", "config.yaml with defaults", "exp/*.yaml", "@_here_", or "--cfg job".
How this skill is triggered — by the user, by Claude, or both
Slash command
/hydra-experiment:hydra-experiments-setupThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Hydra によるパラメータ管理と実験実行のパターン。
Hydra によるパラメータ管理と実験実行のパターン。 実験パラメータと環境設定を分離し、yaml は差分だけで管理する。
experiments/ 配下に実験ごとのディレクトリ NNN-name/ を作成する。
アプローチが異なる実験は別ディレクトリ、同じアプローチのパラメータ違いは exp/ 内の yaml で管理する。
experiments/NNN-name/
config.yaml # エントリポイント (defaults 指定 + hydra 設定)
train.py # dataclass 定義 + 学習ロジック
exp/
001.yaml # パラメータ違い (差分のみ)
002.yaml
env/ # 環境を分ける場合のみ作成
runpod.yaml
local.yaml
from dataclasses import dataclass, field
from hydra.core.config_store import ConfigStore
@dataclass
class ExpConfig:
name: str = "default"
seed: int = 42
learning_rate: float = 2e-5
batch_size: int = 16
# ハイパーパラメータはフラットに配置 (ネストしない)
@dataclass
class EnvConfig:
base_model: str = "/workspace/models/Model"
output_dir: str = "output/NNN-name"
model_output_dir: str = "/workspace/output/NNN-name"
@dataclass
class Config:
exp: ExpConfig = field(default_factory=ExpConfig)
env: EnvConfig = field(default_factory=EnvConfig)
cs = ConfigStore.instance()
cs.store(name="default", group="exp", node=ExpConfig)
cs.store(name="default", group="env", node=EnvConfig)
exp/default.yaml や env/default.yaml は作らない (ConfigStore の name="default" と衝突するため)。
# 設定確認: python train.py --cfg job
# 実験指定: python train.py exp=001
defaults:
- _self_
- exp: default # ConfigStore のデフォルト
- env: default # ConfigStore のデフォルト (yaml なし)
# - env: runpod # yaml がある場合はこちら
- override hydra/job_logging: none # hydra ログファイル生成を無効化
hydra:
output_subdir: null # .hydra/ ディレクトリを作らない
job:
chdir: false # 作業ディレクトリを変更しない
run:
dir: . # 出力ディレクトリをカレントに
config.yaml には exp: や env: ブロックで値を直接書かない。値は dataclass のデフォルトで管理し、変更は exp/*.yaml で行う。
# exp/001.yaml
defaults:
- default@_here_ # ConfigStore の "default" スキーマを継承
name: "001-lr-sweep"
learning_rate: 5.0e-5
defaults: [default@_here_] で dataclass のデフォルト値を継承@_here_ は「このファイル自身の位置に展開する」という意味EnvConfig のデフォルト値がメインの実行環境に合っていれば yaml は不要。
別環境で動かす必要が出たら env/ に yaml を追加する:
# env/local.yaml
defaults:
- default@_here_
base_model: "Qwen/Qwen3-4B-Instruct-2507" # HuggingFace から取得
model_output_dir: "output/NNN-name" # ローカルパス
python train.py exp=001 env=local
| 置き場 | 内容 | 判断基準 |
|---|---|---|
exp | 学習パラメータ、データセット、seed | 実験ごとに変える値 |
env | モデルパス、出力パス | 同じ実験を別マシンで動かすときに変える値 |
@hydra.main(version_base=None, config_path=".", config_name="config")
def main(cfg: Config) -> None:
# 実験実行...
if __name__ == "__main__":
main()
Hydra 組み込みの --cfg オプションを使う。show_config フラグは不要。
python train.py --cfg job # 自分の設定だけ表示
python train.py --cfg job exp=001 # 実験指定して確認
python train.py --cfg hydra # Hydra 自体の設定
python train.py --cfg all # 全部
uv run python train.py # デフォルト設定で実行
uv run python train.py exp=001 # exp/001.yaml を使用
uv run python train.py exp=001 env=local # exp/001.yaml, env/local.yaml を使用
uv run python train.py exp=001 exp.debug=true # オーバーライド
uv run python train.py exp=001 exp.batch_size=8 # 値を直接変更
# fold: [0, 1, 2, 3, 4]
for fold in cfg.exp.fold:
with wandb.init(
project=os.environ.get("WANDB_PROJECT", "my-project"),
group=cfg.exp.name,
name=f"{cfg.exp.name}/fold{fold}",
config=OmegaConf.to_container(cfg.exp, resolve=True), # type: ignore
mode="disabled" if cfg.exp.debug else "online",
):
train_fold(cfg, fold)
...
from hydra import compose, initialize
with initialize(version_base=None, config_path="./experiments/001-baseline"):
cfg = compose(
config_name="config.yaml", # .yaml をつける
overrides=["exp.name=foo"],
)
User: 「新しい実験 002-larger-model を始めたい」
mkdir -p experiments/002-larger-model/expconfig.yaml と train.py をコピーexp/001.yaml を作成してパラメータ設定uv run python train.py --cfg job exp=001 で設定確認uv run python train.py exp=001 で実行User: 「learning rate を変えて試したい」
exp/002.yaml を作成:
defaults:
- default@_here_
name: "002-smaller-lr"
learning_rate: 1e-5
uv run python train.py exp=002 で実行User: 「ローカルで同じ実験を動かしたい」
env/local.yaml を作成:
defaults:
- default@_here_
base_model: "Qwen/Qwen3-4B-Instruct-2507"
model_output_dir: "output/002-larger-model"
env: local に変更、または:
uv run python train.py exp=001 env=local
config_path 確認defaults の _self_ の位置を確認"exp.fold=[0,1,2]" と引用符で囲むGlobalHydra.instance().clear() してから initializedefaults:
- default@_here_ # default の内容をこの階層にマージ
str | list[str]) は OmegaConf がサポートしないnpx claudepluginhub pokutuna/claude-plugins --plugin hydra-experimentManages ML experiment lifecycle via structured YAML registry. Registers experiments, records results, compares runs, tracks status. Activates on experiment-related queries.
Designs ML experiments: ablation studies, baseline comparisons, experiment matrices; estimates GPU/API costs; generates config stubs, execution scripts, and analysis plans.
Maintains persistent ML experiment journals in Markdown files, logging hypotheses, changes, results, metrics, and learnings across sessions.