0

I have 2 configs like, and I want to make inner config PARTLY inherit outer config. Thank you for your help!

this is the outer config:

# config.yaml
defaults:
  - _self_
  - model: pct

lr: 1e-3 
num_epoch: 200
weight_decay: 5e-4
scheduler_stepsize: 50
batch_size: 16

this is the inner config:

# model/pct.yaml
file_name: pct
lr: 1e-4

And I want to override the outer config PARTLY, such as inherit 'lr':

# my expect output config
lr: 1e-4
model:
  file_name: pct
...

I know there is '@here' keyword to make inner config be global, but I do not know how to make inner config PARTLY be global.

Alobal
  • 1
  • 1

1 Answers1

0

In this situation I'd recommend that you use OmegaConf's variable interpolation capability.

# config.yaml
defaults:
  - _self_
  - model: pct

lr: ${model.lr}
num_epoch: 200
# model/pct.yaml
file_name: pct
lr: 1e-4
# my_app.py
from omegaconf import DictConfig, OmegaConf
import hydra

@hydra.main(config_path=".", config_name="config")
def my_app(cfg: DictConfig) -> None:
    OmegaConf.resolve(cfg)
    print(OmegaConf.to_yaml(cfg))

if __name__ == "__main__":
    my_app()

Running the app:

$ python my_app.py
lr: 0.0001
num_epoch: 200
model:
  file_name: pct
  lr: 0.0001
Jasha
  • 5,507
  • 2
  • 33
  • 44
  • Thank you for your help! I think this solution is just like delete ``lr`` in config.yaml, and directly use model/pct.yaml. However, I want to keep the default ``lr`` in config.yaml. So , when there is no ``lr`` in model/pct.yaml , I can use the default value in config.yaml. – Alobal Apr 11 '22 at 11:03
  • Or may be what I need is ``if condition`` , but I do not find it in hydra by searching "if" – Alobal Apr 11 '22 at 11:12