Hyperopt

在训练完我们的第一个模型并使用它以合理的准确率预测新数据后,如何使模型更好?

通过简单地将 hyperopt 添加到 Ludwig 配置中,Ludwig 可以执行超参数优化。

rotten_tomatoes.yaml
input_features:
    - name: genres
      type: set
    - name: content_rating
      type: category
    - name: top_critic
      type: binary
    - name: runtime
      type: number
    - name: review_content
      type: text
      encoder: 
        type: embed
output_features:
    - name: recommended
      type: binary
hyperopt:
  goal: maximize
  output_feature: recommended
  metric: accuracy
  split: validation
  parameters:
    training.learning_rate:
      space: loguniform
      lower: 0.0001
      upper: 0.1
    training.optimizer.type:
      space: choice
      categories: [sgd, adam, adagrad]
    review_content.embedding_size:
      space: choice
      categories: [128, 256]
  search_alg:
    type: variant_generator
  executor:
    num_samples: 10

在此示例中,我们指定了一个基本的 hyperopt 配置,具有以下设置:

  • 我们将 goal 设置为最大化验证集上的准确率指标
  • 我们要优化的参数包括学习率优化器类型以及用于文本表示的embedding_size
  • 在优化学习率时,我们随机选择对数尺度上介于 0.0001 和 0.1 之间的值。
  • 在优化优化器类型时,我们从 sgdadamadagrad 优化器中随机选择一个优化器。
  • 在优化文本表示的embedding_size时,我们在 128 或 256 中随机选择。
  • 我们将 hyperopt 的 executor 设置为使用 Ray Tune 的 variant_generator 搜索算法,并从我们定义的搜索空间中生成 10 种随机超参数组合。执行将在本地并行运行试验。
  • Ludwig 支持贝叶斯优化和遗传算法等高级超参数采样算法。详细信息请参阅本指南

超参数优化策略使用 ludwig hyperopt 命令运行

ludwig hyperopt --config rotten_tomatoes.yaml --dataset rotten_tomatoes.csv
from ludwig.hyperopt.run import hyperopt
import pandas

df = pandas.read_csv('rotten_tomatoes.csv')
results = hyperopt(config='rotten_tomatoes.yaml', dataset=df)
docker run -t -i --mount type=bind,source={absolute/path/to/rotten_tomatoes_data},target=/rotten_tomatoes_data ludwigai/ludwig hyperopt --config /rotten_tomatoes_data/rotten_tomatoes.yaml --dataset /rotten_tomatoes_data/rotten_tomatoes.csv

配置中的每个参数都可以使用 hyperopt 进行调整。请参阅完整的hyperopt 指南以了解更多信息。