Keras Tuner get_best_hyperparameters()

bli*_*soo 3 python-3.x tensorflow keras-tuner

有什么方法可以将最佳超参数作为列表返回,以便我可以在代码的其他部分访问?我不需要整个模型,我只是希望能够提取它找到的最佳超参数的值并在不同的 python 文件中使用它。

tuner = keras_tuner.RandomSearch(
    hypermodel=build_model,
    objective="val_loss",
    max_trials=2,
    executions_per_trial=2,
    overwrite=True,
    directory="my_dir",
    project_name="helloworld",
)
tuner.search_space_summary()
tuner.search(x_train, y_train, epochs=20, validation_data=(x_val, y_val))

best_hp = tuner.get_best_hyperparameters()[0]

model = tuner.hypermodel.build(best_hp)

summary = tuner.results_summary(num_trials=10)
Run Code Online (Sandbox Code Playgroud)

例如,我想检索 best_hp 列表,或 results_summary 返回到我的终端的最佳超参数的摘要。

小智 5

我想我找到了办法。事实证明,有一个字典存储了最佳的超参数值和名称,要访问它,您必须输入以下内容(首先在控制台中尝试):

best_hp.values
Run Code Online (Sandbox Code Playgroud)

当然,这是假设您已经完成了调整和超参数搜索。奇怪的是我在文档中找不到这个。我在深入研究 Spyder 变量资源管理器中的“best_hps”后最终发现了这一点。

PS:请耐心等待,这是我的第一个 StackOverflow 答案