-
In Hydra docs, they described a nice pattern for Configuring Experiments, which is especially useful for data science. Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Thanks for asking this @mbignotti , I will try to find time in the next couple of days to answer 😄 |
Beta Was this translation helpful? Give feedback.
-
First, you may have realized I deleted a comment where I accidently replied to @rsokl comment, this replaces that with a fix to a user error I had. Thanks for asking this @mbignotti. This is definitely something we should have in our docs. @rsokl I'll take a cut at this using our latest features from dataclasses import dataclass
from hydra_zen import MISSING, make_config, store, to_yaml, zen
# Here we utilize hydra-zen's `store` to
# add configurations to the Hydra ConfigStore
# First create stores for each group
db_store = store(group="db")
server_store = store(group="server")
experiment_store = store(group="experiment")
# add to named configurations for the `db` group
@db_store(name="mysql")
@dataclass
class MySQL:
name: str = "mysql"
@db_store(name="sqlite")
@dataclass
class SQLITE:
name: str = "sqlite"
# add to named configurations for the `server` group
@server_store(name="apache")
@dataclass
class Apache:
name: str = "apache"
port: int = 80
@server_store(name="nginx")
@dataclass
class NGINX:
name: str = "nginx"
port: int = 80
# utilize `make_config` to easily create configurations with
# hydra defaults
Config = make_config(
hydra_defaults=["_self_", {"db": "sqlite"}, {"server": "apache"}],
db=MISSING,
server=MISSING,
)
# the experiment configs:
# - must inherit from `Config`
# - must set `package="_global_"`
experiment_store(
make_config(
hydra_defaults=["_self_", {"override /db": "sqlite"}],
server=dict(port=8080),
bases=(Config,),
),
name="aplite",
package="_global_",
)
experiment_store(
make_config(
hydra_defaults=[
"_self_",
{"override /db": "sqlite"},
{"override /server": "nginx"},
],
server=dict(port=8080),
bases=(Config,)
),
name="nglite",
package="_global_"
)
# put main configuration in the store
store(
Config,
name="config",
)
# the task function
def task(db, server):
print("db:")
print(to_yaml(db))
print("server:")
print(to_yaml(server))
if __name__ == "__main__":
store.add_to_hydra_store()
zen(task).hydra_main(config_path=None, config_name="config", version_base="1.2") You should be able to run the following experiments: $ python my_app.py
db:
name: sqlite
server:
name: apache
port: 80
$ python my_app.py db=mysql server.port=8080
db:
name: mysql
server:
name: apache
port: 8080
$ python my_app.py +experiment=aplite
db:
name: sqlite
server:
name: apache
port: 8080
$ python my_app.py --multirun +experiment=aplite,nglite
[2023-01-17 10:45:25,609][HYDRA] Launching 2 jobs locally
[2023-01-17 10:45:25,609][HYDRA] #0 : +experiment=aplite
db:
name: sqlite
server:
name: apache
port: 8080
[2023-01-17 10:45:25,713][HYDRA] #1 : +experiment=nglite
db:
name: sqlite
server:
name: nginx
port: 8080 |
Beta Was this translation helpful? Give feedback.
First, you may have realized I deleted a comment where I accidently replied to @rsokl comment, this replaces that with a fix to a user error I had.
Thanks for asking this @mbignotti. This is definitely something we should have in our docs. @rsokl I'll take a cut at this using our latest features
zen
andstore
. Here's the code: