Search code examples
python-3.xfb-hydra

Call hydra.utils.instantiate inside configuration


The following works:

trainer = pl.Trainer(early_stop_callback=hydra.utils.instantiate(cfg.callbacks.early_stopping),
                     **cfg.trainer)

But it makes early_stopping a required parameter. Is there a better way or a way to specify all this in the configuration with something similar to the following?

$ cat trainer.yaml
trainer:
  cls: pytorch_lightning.Trainer
  params:
    early_stop_callback: instantiate(cfg.callbacks.early_stopping)

So that I can do:

trainer = hydra.utils.instantiate(cfg.trainer)

Solution

  • Hydra 1.1.0 (2021-06-09) now supports recursive instantiation, see the CHANGELOG, and in particular this issue.