当使用 Hydra 的 optuna 插件时,我可以从另一个配置文件导入搜索空间吗?

发布于 2025-01-12 07:30:44 字数 1780 浏览 5 评论 0原文

我想对同一数据进行超参数优化多个时间序列预测模型。我正在使用 Hydra 的 Optuna Sweeper 插件。不同的模型具有不同的超参数,因此具有不同的搜索空间。目前我的配置文件如下所示:

defaults:
  - datasets: data
  - models: Ets
  - override hydra/sweeper: optuna
  - override hydra/sweeper/sampler: tpe

hydra:
#  launcher:
#    n_jobs: 10
 run:
  dir: data/outputs/${now:%Y-%m-%d}/${user.user}/${now:%H-%M-%S}
 sweeper:
   sampler:
     seed: 123
   direction: minimize
   study_name: main_val
   storage: null
   n_trials: 2
   n_jobs: 4

   search_space: 

# Ets
    models.damped_trend:
      type: categorical
      choices:
      - 'True'
      - 'False'

  # Theta
    # models.method:
    #   type: categorical
    #   choices:
    #   - 'additive'
    #   - 'multiplicative' 

现在,当我使用 --multirun 运行 main_val.py 文件时,我获得了 Ets 的最佳超参数。伟大的。但是,当我想要对另一个模型(在此示例中为 Theta)运行优化时,我必须手动注释掉 Ets 的搜索空间并取消注释 Theta 的搜索空间。事实上,每个模型都有更多的参数需要优化,我正在使用 10 个不同的模型。这使得我的配置文件变得相当长且令人困惑,而且这种注释/取消注释的内容既烦人又容易出错。

我想从另一个 yaml 文件导入每个模型的搜索空间。这可能吗?

我尝试了以下操作:

defaults:
  - datasets: data
  - models: Ets
  - search_spaces: Ets
  - override hydra/sweeper: optuna
  - override hydra/sweeper/sampler: tpe

hydra:
#  launcher:
#    n_jobs: 10
 run:
  dir: data/outputs/${now:%Y-%m-%d}/${user.user}/${now:%H-%M-%S}
 sweeper:
   sampler:
     seed: 123
   direction: minimize
   study_name: main_val
   storage: null
   n_trials: 2
   n_jobs: 4

   search_space: search_spaces

文件 search_spaces/Ets.yaml 如下所示:

models.damped_trend:
  type: categorical
  choices:
  - 'True'
  - 'False'

但我收到错误:

Validation error while composing config:
    Cannot assign str to Dict[str, Any]
        full_key: hydra.sweeper.search_space
        object_type=OptunaSweeperConf

I want to hyper-parameter optimize multiple time series forecasting models on the same data. I'm using the Optuna Sweeper plugin for Hydra. The different models have different hyper-parameters and therefore different search spaces. At the moment my config file looks like this:

defaults:
  - datasets: data
  - models: Ets
  - override hydra/sweeper: optuna
  - override hydra/sweeper/sampler: tpe

hydra:
#  launcher:
#    n_jobs: 10
 run:
  dir: data/outputs/${now:%Y-%m-%d}/${user.user}/${now:%H-%M-%S}
 sweeper:
   sampler:
     seed: 123
   direction: minimize
   study_name: main_val
   storage: null
   n_trials: 2
   n_jobs: 4

   search_space: 

# Ets
    models.damped_trend:
      type: categorical
      choices:
      - 'True'
      - 'False'

  # Theta
    # models.method:
    #   type: categorical
    #   choices:
    #   - 'additive'
    #   - 'multiplicative' 

Now, when I run the main_val.py file with --multirun, I get the optimal hyper-parameters for Ets. Great. But when I want to run the optimization for another model, in this example Theta, I have to manually comment out the search space for Ets and uncomment the search space for Theta. In reality, each model has much more parameters to optimize and I'm working with 10 different models. This makes my config file quite long and confusing and this commenting/uncommenting stuff is both annoying and error-prone.

I would like to import the search space for each model from another yaml file. Is that possible?

I tried the following:

defaults:
  - datasets: data
  - models: Ets
  - search_spaces: Ets
  - override hydra/sweeper: optuna
  - override hydra/sweeper/sampler: tpe

hydra:
#  launcher:
#    n_jobs: 10
 run:
  dir: data/outputs/${now:%Y-%m-%d}/${user.user}/${now:%H-%M-%S}
 sweeper:
   sampler:
     seed: 123
   direction: minimize
   study_name: main_val
   storage: null
   n_trials: 2
   n_jobs: 4

   search_space: search_spaces

with the file search_spaces/Ets.yaml looking like this:

models.damped_trend:
  type: categorical
  choices:
  - 'True'
  - 'False'

But I got the error:

Validation error while composing config:
    Cannot assign str to Dict[str, Any]
        full_key: hydra.sweeper.search_space
        object_type=OptunaSweeperConf

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

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

发布评论

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

评论(1

手长情犹 2025-01-19 07:30:44

这里有两个选项:

  1. 使用 @package 指令
  2. 使用 变量插值

详细信息:

使用 @package 指令

@package 指令可用于放置 Ets.yaml在 Hydra.sweeper.search_spaces 包中:

defaults:
  - datasets: data
  - models: Ets
  - [email protected]_space: Ets
  - override hydra/sweeper: optuna
  - override hydra/sweeper/sampler: tpe

hydra:
 run:
  dir: data/outputs/${now:%Y-%m-%d}/${user.user}/${now:%H-%M-%S}
 sweeper:
   sampler:
     seed: 123
   direction: minimize
   study_name: main_val
   storage: null
   n_trials: 2
   n_jobs: 4

使用变量插值:

字符串插值可用于创建从 Hydra.sweeper.search_spaces 到顶层的引用search_spaces 配置。

defaults:
  - datasets: data
  - models: Ets
  - search_spaces: Ets
  - override hydra/sweeper: optuna
  - override hydra/sweeper/sampler: tpe

hydra:
 run:
  dir: data/outputs/${now:%Y-%m-%d}/${user.user}/${now:%H-%M-%S}
 sweeper:
   sampler:
     seed: 123
   direction: minimize
   study_name: main_val
   storage: null
   n_trials: 2
   n_jobs: 4

   search_space: ${search_spaces}

Here are two options:

  1. Use a @package directive
  2. Use a variable interpolation

In detail:

Using an @package directive

An @package directive can be used to place Ets.yaml in the hydra.sweeper.search_space package:

defaults:
  - datasets: data
  - models: Ets
  - [email protected]_space: Ets
  - override hydra/sweeper: optuna
  - override hydra/sweeper/sampler: tpe

hydra:
 run:
  dir: data/outputs/${now:%Y-%m-%d}/${user.user}/${now:%H-%M-%S}
 sweeper:
   sampler:
     seed: 123
   direction: minimize
   study_name: main_val
   storage: null
   n_trials: 2
   n_jobs: 4

Using a variable interpolation:

A string interpolation can be used to create a reference from hydra.sweeper.search_spaces to the top-level search_spaces config.

defaults:
  - datasets: data
  - models: Ets
  - search_spaces: Ets
  - override hydra/sweeper: optuna
  - override hydra/sweeper/sampler: tpe

hydra:
 run:
  dir: data/outputs/${now:%Y-%m-%d}/${user.user}/${now:%H-%M-%S}
 sweeper:
   sampler:
     seed: 123
   direction: minimize
   study_name: main_val
   storage: null
   n_trials: 2
   n_jobs: 4

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