1
$\begingroup$

If you already been following some of the best practices in Deep Learning, Facebook's Hydra might be a familiar name to you. Going through the Hydra Documentation, I came across two important operators + and ++. The documents mentions the use of these operators as follows:

Appending a config value : +foo.bar=value
Appending or overriding a config value : ++foo.bar=value

I need some help understanding these operators, as using them in the code technically makes no difference in the overall output.

$\endgroup$

1 Answer 1

1
$\begingroup$

Suppose we have the following simple hydra app:

├── config.yaml
└── my_app.py
# my_app.py
import hydra
from omegaconf import OmegaConf, DictConfig

@hydra.main(".", "config")
def app(cfg: DictConfig):
    print(OmegaConf.to_yaml(cfg))

if __name__ == "__main__":
    app()
# config.yaml
a:
  b: c
$ python my_app.py
a:
  b: c

Let's take a look at the four methods of "Modifying the Config Object" from the docs page you linked:

  • Overriding a config value: foo.bar=value
  • Appending a config value: +foo.bar=value
  • Appending or overriding a config value: ++foo.bar=value
  • Removing a config value: ~foo.bar, ~foo.bar=value

Overriding a config value:

$ python my_app.py a.b=y
a:
  b: y
$ python my_app.py a.x=y
Could not override 'a.x'.
To append to your config use +a.x=y
Key 'x' is not in struct
    full_key: a.x
    object_type=dict

Appending a config value:

$ python my_app.py +a.b=y
Could not append to config. An item is already at 'a.b'.
Either remove + prefix: 'a.b=y'
Or add a second + to add or override 'a.b': '++a.b=y'
...
$ python my_app.py +a.x=y
a:
  b: c
  x: y

Appending or overriding a config value:

$ python my_app.py ++a.b=y
a:
  b: y
$ python my_app.py ++a.x=y
a:
  b: c
  x: y

Removing a config value:

$ python my_app.py ~a.b
a: {}
$ python my_app.py ~a.b=c
a: {}
$ python my_app.py ~a.b=y
Could not delete from config. The value of 'a.b' is c and not y.
...
$ python my_app.py ~a.x
Could not delete from config. 'a.x' does not exist.
...
$ python my_app.py ~a.x=y
Could not delete from config. 'a.x' does not exist.

To summarize:

  • Overriding a config value (foo.bar=value) works only if the given key is already present in the config
  • Appending a config value (+foo.bar=value) works only if the given key not yet present in the config
  • Appending or overriding a config value (++foo.bar=value) works whether or not the key is present in the config
  • Removing a config value (~foo.bar, ~foo.bar=value) works only if the given key or key-value pair is in the config.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.