KerasTuner

KerasTuner

星标

KerasTuner 是一个易于使用、可扩展的超参数优化框架,它解决了超参数搜索的痛点。通过“定义即运行”的语法轻松配置搜索空间,然后利用其中一种可用的搜索算法为模型找到最佳超参数值。KerasTuner 内置了贝叶斯优化、Hyperband 和随机搜索算法,并且设计使其易于研究人员扩展,以便试验新的搜索算法。



安装

安装最新版本

pip install keras-tuner --upgrade

您也可以在我们的 GitHub 仓库 中查看其他版本。


快速简介

导入 KerasTuner 和 TensorFlow

import keras_tuner
import keras

编写一个创建并返回 Keras 模型的函数。在模型创建过程中,使用 hp 参数来定义超参数。

def build_model(hp):
  model = keras.Sequential()
  model.add(keras.layers.Dense(
      hp.Choice('units', [8, 16, 32]),
      activation='relu'))
  model.add(keras.layers.Dense(1, activation='relu'))
  model.compile(loss='mse')
  return model

初始化一个调优器(这里是 RandomSearch)。我们使用 objective 来指定选择最佳模型的指标,并使用 max_trials 来指定要尝试的不同模型的数量。

tuner = keras_tuner.RandomSearch(
    build_model,
    objective='val_loss',
    max_trials=5)

开始搜索并获取最佳模型

tuner.search(x_train, y_train, epochs=5, validation_data=(x_val, y_val))
best_model = tuner.get_best_models()[0]

要了解更多关于 KerasTuner 的信息,请查看 入门指南


引用 KerasTuner

如果 KerasTuner 对您的研究有所帮助,我们非常感谢您的引用。以下是 BibTeX 条目

@misc{omalley2019kerastuner,
    title        = {KerasTuner},
    author       = {O'Malley, Tom and Bursztein, Elie and Long, James and Chollet, Fran\c{c}ois and Jin, Haifeng and Invernizzi, Luca and others},
    year         = 2019,
    howpublished = {\url{https://github.com/keras-team/keras-tuner}}
}