【问题标题】:Value interpolation with hydra composition使用 hydra 组合进行值插值
【发布时间】:2021-01-06 13:31:42
【问题描述】:

我正在使用具有以下结构的 hydra 组合:

├── configs
    │   ├── config.yaml
    │   ├── data
    │   │   ├── dataset_01.yaml
    │   │   └── dataset_02.yaml
    │   └── model
    │       ├── bert.yaml
    │       └── gpt.yaml
  • config.yaml
defaults:
  - model: bert
  - data: dataset_01

...

  • data/dataset_01.yaml
# @package _group_

name: "dataset_01"

train:
  path: "../resources/datasets/dataset_01/train.jsonl"
  num_samples: 1257391

test:
  path: "../resources/datasets/dataset_01/test.jsonl"
  num_samples: 71892

val:
  path: "../resources/datasets/dataset_01/val.jsonl"
  num_samples: 73805

  • 模型/bert.yaml
# @package _group_

name: "bert"

encoder: "source.encoder.BertEncoder.BertEncoder"

encoder_hparams:
  architecture: "bert-base-uncased"

lr: 1e-7

tokenizer:
  architecture: "bert-base-uncased"

predictions:
  path: "../resources/predictions/bert_predictions.pt"
  • 入口点
@hydra.main(config_path="configs/", config_name="config.yaml")
def perform_tasks(hparams):

    model = MyModel(hparams.model)

if __name__ == '__main__':
    perform_tasks()

hparams.model 的上下文中,OmegaConf 无法插入密钥data.name,因为它不在范围内。 因此,如果有一种方法可以在应用程序开始时进行插值,那就太好了。

【问题讨论】:

    标签: fb-hydra hydra-python omegaconf


    【解决方案1】:

    OmegaConf 插值是绝对的,并且在最终配置上运行。

    试试这个:

    九头蛇 1.0(稳定版)

    predictions:
      path: "../resources/predictions/bert_${data.name}_predictions.pt"
    

    九头蛇 1.1(开发)

    Hydra 1.1 将消除在配置中指定名称的需要。 您将能够在不使用 hydra:choices.GROUP_NAME 添加名称字段的情况下进行插值:

    predictions:
      path: "../resources/predictions/bert_${hydra:choices.data}_predictions.pt"
    

    这已记录在 here。 请注意,这仅在尚未正式发布的 Hydra 1.1 中可用(您可以通过安装 dev 版本来试用)。

    【讨论】:

    • 您好,实际上我不想插入模型的名称(在同一个文件model/bert.yaml 中)。相反,我想插入外部文件中的数据名称 (data/dataset_01.yaml)
    • 这个问题确实令人困惑。我更新了我的答案。
    • 感谢您的回答。我已经尝试过这种方法,但没有成功。打印结果配置 (print(OmegaConf.to_yaml(config))) 时,结果为:path: ../resources/predictions/bert_${data.name}_predictions.pt
    • OmegaConf.to_yaml() 默认情况下不解析插值。你可以通过 resolve=True 来做到这一点。请注意,当您访问字段时,会解析插值。
    • 我强烈建议您阅读 OmegaConf 文档或幻灯片(链接自项目 README)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 2015-10-08
    • 2012-08-18
    相关资源
    最近更新 更多