无法在 Hydra (fb-hydra) 中自定义日志记录行为

Cannot customize logging behaviour in Hydra (fb-hydra)

我是 Hydra 的新手,这是我在 SO 中的第一个问题。

我的目的是根据这个example.

自定义日志记录

我有以下配置文件夹结构:

configs/
├── dataset_structurizers
│   ├── config.yaml
│   └── datasets
│       └── brats_2021.yaml
└── hydra
    └── job_logging
        └── custom.yaml

configs/datasets_structurizers/config.yaml 具有结构:

defaults:
  - override hydra/job_logging: custom
  - datasets:
    - brats_2021

hydra/job_logging/custom.yaml

version: 1
formatters:
  simple:
    format: '[%(levelname)s] : [%(filename)s] : [%(funcName)s] : %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    formatter: simple
    level: DEBUG
    stream: ext://sys.stdout
loggers:
  dataset_structurizers:
    level: DEBUG
    handlers: [console]
    propagate: no
root:
  level: DEBUG
  handlers: [console]
  propagate: no

如果我评论 - override hydra/job_logging: custom 代码工作正常。如果我将 configs/dataset_structurizers/ 中的所有内容移动到 configs,它也会起作用。但是,使用我当前的文件夹结构,如果我 运行 代码,我会收到以下错误:

Traceback (most recent call last):
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/utils.py", line 211, in run_and_report
    return func()
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/utils.py", line 378, in <lambda>
    lambda: hydra.run(
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/hydra.py", line 88, in run
    cfg = self.compose_config(
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/hydra.py", line 559, in compose_config
    cfg = self.config_loader.load_configuration(
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/config_loader_impl.py", line 141, in load_configuration
    return self._load_configuration_impl(
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/config_loader_impl.py", line 239, in _load_configuration_impl
    defaults_list = create_defaults_list(
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/defaults_list.py", line 748, in create_defaults_list
    defaults, tree = _create_defaults_list(
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/defaults_list.py", line 718, in _create_defaults_list
    defaults_tree = _create_defaults_tree(
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/defaults_list.py", line 356, in _create_defaults_tree
    ret = _create_defaults_tree_impl(
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/defaults_list.py", line 455, in _create_defaults_tree_impl
    return _expand_virtual_root(repo, root, overrides, skip_missing)
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/defaults_list.py", line 280, in _expand_virtual_root
    subtree = _create_defaults_tree_impl(
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/defaults_list.py", line 512, in _create_defaults_tree_impl
    _update_overrides(defaults_list, overrides, parent, interpolated_subtree)
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/_internal/defaults_list.py", line 396, in _update_overrides
    In {pcp}: Override '{okey} : {oval}' is defined before '{d.get_override_key()}: {d.get_name()}'.
  File "/home/yere/anaconda3/envs/brain_tumor/lib/python3.8/site-packages/hydra/core/default_element.py", line 495, in get_name
    assert self.value is None or isinstance(self.value, str)
AssertionError

configs/datasets_structurizers/config.yaml 访问 hydra/job_logging/custom.yaml 的正确方法是什么?

您需要将 override 默认值放在默认值列表的末尾:

# config.yaml
defaults:
  - datasets:
    - brats_2021
  - override hydra/job_logging: custom

问题与 override 在默认值中的位置无关。附加到 hydra: \nsearchpath 必要的路径即可解决。