如何正确生成多层次构图?

发布于 2025-01-12 07:14:16 字数 2543 浏览 3 评论 0原文

目前,我的 Hydra 配置组织如下:

configs/
├── config.yaml
├── data
│   ├── IMDB.yaml
│   └── REUT.yaml
└── model
    ├── BERT.yaml
    ├── GPT.yaml
    └── loss
        ├── CrossEntropyLoss.yaml
        └── TripletMarginLoss.yaml

config.yaml:

defaults:
  - model: BERT
  - data: IMDB

tasks: [ "fit", "eval" ]

数据集(IMDB.yamlREUT.yaml)设置的格式为:

name: IMDB
dir: resource/dataset/imdb_reviews/
folds: [0,1,2,3,4]
max_length: 256
num_classes: 10

模型(>BERT.yamlGPT.yaml)设置的格式为:

defaults:
  - loss: TripletMarginLoss

name: BERT
architecture: bert-base-uncased
lr: 5e-5
tokenizer:
  architecture: ${model.architecture}

最后,损失函数设置(CrossEntropyLoss.yamlTripletMarginLoss.yam) 采用以下结构:

_target_: source.loss.TripletMarginLoss.TripletMarginLoss
params:
  name: TripletMarginLoss
  margin: 1.0
  eps: 1e-6
  reduction: mean

运行以下入口点:

@hydra.main(config_path="configs/", config_name="config.yaml")
def my_app(params):
    OmegaConf.resolve(params)
    print(
        f"Params:\n"
        f"{OmegaConf.to_yaml(params)}\n")

if __name__ == '__main__':
    my_app()

# python main.py

生成正确的配置组合:

tasks:
- fit
- eval
model:
  loss:
    _target_: source.loss.TripletMarginLoss.TripletMarginLoss
    params:
      name: TripletMarginLoss
      margin: 1.0
      eps: 1.0e-06
      reduction: mean
  name: BERT
  architecture: bert-base-uncased
  lr: 5.0e-05
  tokenizer:
    architecture: bert-base-uncased
data:
  name: IMDB
  dir: resource/dataset/imdb_reviews/
  folds:
  - 0
  - 1
  - 2
  - 3
  - 4
  max_length: 256
  num_classes: 10

但是,覆盖损失函数会生成错误的配置:

python main.py model.loss=CrossEntropyLoss

tasks:
- fit
- eval
model:
  loss: CrossEntropyLoss
  name: BERT
  architecture: bert-base-uncased
  lr: 5.0e-05
  tokenizer:
    architecture: bert-base-uncased
data:
  name: IMDB
  dir: resource/dataset/imdb_reviews/
  folds:
  - 0
  - 1
  - 2
  - 3
  - 4
  max_length: 256
  num_classes: 10

因此,如何正确生成多级组合?

Currently, my hydra config is organized as follows:

configs/
├── config.yaml
├── data
│   ├── IMDB.yaml
│   └── REUT.yaml
└── model
    ├── BERT.yaml
    ├── GPT.yaml
    └── loss
        ├── CrossEntropyLoss.yaml
        └── TripletMarginLoss.yaml

config.yaml:

defaults:
  - model: BERT
  - data: IMDB

tasks: [ "fit", "eval" ]

The dataset (IMDB.yaml and REUT.yaml) settings are in the format:

name: IMDB
dir: resource/dataset/imdb_reviews/
folds: [0,1,2,3,4]
max_length: 256
num_classes: 10

The model (BERT.yaml and GPT.yaml) settings are in the format:

defaults:
  - loss: TripletMarginLoss

name: BERT
architecture: bert-base-uncased
lr: 5e-5
tokenizer:
  architecture: ${model.architecture}

And finally, the loss function settings (CrossEntropyLoss.yaml and TripletMarginLoss.yam) adopt the following structure:

_target_: source.loss.TripletMarginLoss.TripletMarginLoss
params:
  name: TripletMarginLoss
  margin: 1.0
  eps: 1e-6
  reduction: mean

Running the following entry point:

@hydra.main(config_path="configs/", config_name="config.yaml")
def my_app(params):
    OmegaConf.resolve(params)
    print(
        f"Params:\n"
        f"{OmegaConf.to_yaml(params)}\n")

if __name__ == '__main__':
    my_app()

# python main.py

generates the correct config composition:

tasks:
- fit
- eval
model:
  loss:
    _target_: source.loss.TripletMarginLoss.TripletMarginLoss
    params:
      name: TripletMarginLoss
      margin: 1.0
      eps: 1.0e-06
      reduction: mean
  name: BERT
  architecture: bert-base-uncased
  lr: 5.0e-05
  tokenizer:
    architecture: bert-base-uncased
data:
  name: IMDB
  dir: resource/dataset/imdb_reviews/
  folds:
  - 0
  - 1
  - 2
  - 3
  - 4
  max_length: 256
  num_classes: 10

However, overriding the loss function generates the wrong config:

python main.py model.loss=CrossEntropyLoss

tasks:
- fit
- eval
model:
  loss: CrossEntropyLoss
  name: BERT
  architecture: bert-base-uncased
  lr: 5.0e-05
  tokenizer:
    architecture: bert-base-uncased
data:
  name: IMDB
  dir: resource/dataset/imdb_reviews/
  folds:
  - 0
  - 1
  - 2
  - 3
  - 4
  max_length: 256
  num_classes: 10

Therefore, how to correctly generate a multi-level composition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界和平 2025-01-19 07:14:16

覆盖嵌套配置组是使用 / 作为分隔符来完成的,如配置组描述此处中所述。

尝试:

$ python main.py model/loss=CrossEntropyLoss

Overriding nested config groups is done with / as separator as documented in the config group description here.

Try:

$ python main.py model/loss=CrossEntropyLoss
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文