You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hydra.utils.instantiate converts to OmegaConf named arguments, making it impossible to use the named arguments syntax for arguments which cannot be represented in OmegaConf.
ValueError: key params: generator is not a primitive type
As a side note, the fact that hydra.utils.instantiate takes as input an argument named config makes it impossible to instantiate an object which expects the same argument using named arguments, which seems an unnecessary restriction.
The following solves both issues:
definstantiate_f(config):
assertconfigisnotNone, "Input config is None"class_name=config["class"]
clazz=get_class(class_name)
params=config.paramsif"params"inconfigelseOmegaConf.create()
assertisinstance(
params, DictConfig
), "Input config params are expected to be a mapping, found {}".format(
type(config.params)
)
params=OmegaConf.to_container(params, resolve=True)
deff(*args, **kwargs):
try:
kwargs= {**params, **kwargs}
returnclazz(*args, **kwargs)
exceptExceptionase:
log.error("Error instantiating {} : {}".format(class_name, e))
raiseereturnf@hydra.main(config_path="conf.yaml")defmy_app(cfg):
print(cfg.pretty())
model=torchvision.models.alexnet()
instantiate_f(cfg)(params=model.parameters()) # OKinstantiate_f(cfg)(model.parameters()) # OKif__name__=="__main__":
my_app()
A note in the documentation should be added explaining the behavior wrt config, as it gets resolved immediately.
The text was updated successfully, but these errors were encountered:
Thanks for the issue report. will look into addressing it in a bit cleaner when when I have a chance.
support for passthrough was first added exactly for this use case (optimizer taking parameters()). named parameter support was added later which explains why this issue went unnoticed by me.
hydra.utils.instantiate
converts to OmegaConf named arguments, making it impossible to use the named arguments syntax for arguments which cannot be represented in OmegaConf.To reproduce:
conf.yaml
:test.py
:** Stack trace/error message **
As a side note, the fact that
hydra.utils.instantiate
takes as input an argument namedconfig
makes it impossible to instantiate an object which expects the same argument using named arguments, which seems an unnecessary restriction.The following solves both issues:
A note in the documentation should be added explaining the behavior wrt
config
, as it gets resolved immediately.The text was updated successfully, but these errors were encountered: