如何覆盖实例化配置组参数以实现learning_rate超参搜索?
问题描述
我的配置文件中有controller字段,通过CLI选项+controller=mpc手动设置该字段对应的配置。这个controller定义在controller配置组中,对应文件mpc.yaml的内容如下:
_target_: ... _partial_: True communicator: _target_: ... # learning_rate: !!float 0.5
该controller包含可选字段learning_rate,默认值为None,我想要对这个参数进行超参搜索。尝试用命令python main.py +controller=mpc +controller/learning_rate=0.5设置时,参数并未生效,反而新增了名为controller/learning_rate的独立键值对。请问如何对该参数(例如取值0.5、0.7)进行超参搜索?
解决方法
单个参数的正确设置方式:要给已加载的
controller子配置设置嵌套字段,需要用点号.替代斜杠/,正确命令如下:python main.py +controller=mpc controller.learning_rate=0.5斜杠
/在Hydra这类配置框架中多用于添加新的配置组或独立键值对,而点号.才是访问已加载配置嵌套字段的正确语法。超参搜索的实现方式:如果使用Hydra的超参搜索功能,可通过以下两种方式实现:
- 直接在CLI指定搜索范围:
加上python main.py +controller=mpc controller.learning_rate=choices(0.5,0.7) --multirun--multirun参数会触发多轮运行,分别使用指定的每一个参数值。 - 在配置文件中定义搜索空间:
先修改mpc.yaml,添加learning_rate的搜索配置:
然后运行多轮搜索命令:_target_: ... _partial_: True communicator: _target_: ... learning_rate: _target_: hydra.conf.values.Choice choices: [0.5, 0.7]python main.py +controller=mpc --multirun
- 直接在CLI指定搜索范围:
内容的提问来源于stack exchange,提问作者mainrs
相关产品推荐
相关产品推荐

