I'd like to add a key + value after my Hydra Config is loaded. Essentially I want to run my code and check if a gpu is available or not. If it is, log the device as gpu, else keep the cpu.
Essentially saving the output of :
torch.cuda.is_available()
When I try to add a key with "setdefault" as such:
@hydra.main(config_path="conf", config_name="config")
def my_app(cfg: DictConfig) -> None:
cfg.setdefault("new_key", "new_value")
Same error if I do:
cfg.new_key = "new_value"
print(cfg.new_key)
I get the error:
omegaconf.errors.ConfigKeyError: Key 'new_key' is not in struct
full_key: new_key
reference_type=Optional[Dict[Union[str, Enum], Any]]
object_type=dict
My current workaround is to just use OmegaConfig as such:
cfg = OmegaConf.structured(OmegaConf.to_yaml(cfg))
cfg.new_key = "new_value"
print(cfg.new_key)
>>>> new_value
Surely there must be a better way to do this?
from omegaconf.omegaconf import open_dict
– Perseus