pythonaidata-science

Python Hydra

A short introduction to python hydra and why is it useful.
4/29/2025
0 minute

Warning: The post writing is still in progress and thus shouldn't be taken into cinsideration for now.

Hydra is a framework developed by Facebook AI. As you guessed it, it is mainly used for AI.

Its main purpose is:

  • Manage complex configurations in projects.
  • Easily integrates with command line.
  • Create multi-run experiments with different configurations easily.
  • Compose multiple configuration files using a hierrarchical and modular approach.

Hydra uses YAML files for configurations where you can define parameters for your application:

model:
    _target_: src.models.encoder.TextEncoder
    hidden_size: 128
    number_of_layers: 2
training:
    epochs: 16
    lr: 0.001

Note that the _target_ field is a special field that tells Hydra which class or function to instantiate.

from hydra.utils import instantiate

# NOTE: thus target should point to a valid python class / function inside of a module
model = instantiate(configuration.model)

Hydra also supports configurations composition using the defaults keyword:

# config.yaml
defaults:
    - model: lstm
    - train: default

... configurations directory; overriding from the command line; multi runs; structured configs; main function using hydra; etc


Hydra is built on top of OmegaConf.

© Raideno.
Python Hydra