hydra-zen: Composable Hydra Configurations

0.16.0 · active · verified Thu Apr 16

hydra-zen is a Python library that simplifies the creation of reproducible, composable, and scalable configurations for machine learning and scientific workflows using Hydra. It provides a more Pythonic API to define Hydra configurations, enabling type-checking, autocompletion, and easier integration with existing Python code. The current version is 0.16.0, with releases occurring periodically, often driven by new features or compatibility updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a configuration for a Python function using `builds` and then instantiate that configuration directly using `instantiate`. It shows how to set default parameters and how to override them programmatically, illustrating `hydra-zen`'s ability to create and manage `_target_` configurations.

from hydra_zen import builds, instantiate
from omegaconf import OmegaConf

def train_model(epochs: int, lr: float, model_name: str = "ResNet"): #_target_
    """Simulates a model training process."""
    return f"Training {model_name} for {epochs} epochs with learning rate {lr}"

# Define a config for our function, setting default values
# Parameters not set here become mandatory CLI arguments or must have defaults in the function.
TrainConfig = builds(train_model, epochs=10, lr=0.01, model_name="AwesomeNet")

# Instantiate the configuration directly to run the target function
# This simulates `hydra.utils.instantiate` using hydra-zen's convenience API
result = instantiate(TrainConfig)
print(f"Direct instantiation: {result}")

# You can also override values programmatically
override_config = TrainConfig(epochs=20, lr=0.005, model_name="SuperNet")
result_override = instantiate(override_config)
print(f"Instantiation with overrides: {result_override}")

# Inspect the raw OmegaConf config object
print("\nGenerated config:\n" + OmegaConf.to_yaml(override_config))

view raw JSON →