Combine StructuredConfigs and YAML files via composition #757
-
On the whole, I strongly agree with Hydra-Zen's philosophy of removing YAML files and building StructuredConfigs directly from code. However, for ML there's a use-case where I think I still want some of the config to live in YAML files. For example, when training a model I'll need an explicit list of feature names (corresponding to columns in the dataset). I don't see a way to do this with StructuredConfigs because
Here's an example script to demonstrate # train.py
from dataclasses import dataclass
from typing import List
import hydra
from hydra_zen import builds, ZenStore
immediate_store = ZenStore(deferred_hydra_store=False)
@immediate_store(name="default", group="model")
@dataclass
class Model:
features: List[str]
def train(self):
print(f"Training on features: {self.features}")
@immediate_store(
name="config",
hydra_defaults=[
"_self_",
{"model": "default"},
],
)
def train(model: Model):
model.train()
@hydra.main(config_path="config", config_name="config", version_base="1.1")
def main(cfg):
hydra.utils.call(cfg) # Invokes `_target_` which is `train()`
if __name__ == "__main__":
main() If
However when I try to give a longer list with a YAML # config/model.yaml
features:
- f1
- f2
- ...
I also tried renaming
which makes it appear Hydra assumes I want the StructuredConfig to validate the schema of my YAML, when in fact I want to compose the StructuredConfig and YAML together. Surely there's got to be a way to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Found the solution for my initial problem: refactor |
Beta Was this translation helpful? Give feedback.
Found the solution for my initial problem: refactor
config/model.yaml
toconfig/model/default.yaml
. Then the{"model": "default"}
entry of Hydra's defaults list will pick this up. The previous structure would correspond to a top-level@hydra.main(config_name="model", ...)
which was not my intent