您如何在 Keras Tuner 运行期间保存进度?

问题描述

我目前正在免费的 Google Colab 实例上使用 Keras Tuner 在更大的搜索空间中进行转移。由于使用限制,我的搜索运行将在完成之前中断。我想定期保存我的搜索进度以应对这些中断,并在 Colab 资源再次可供我使用时从上一个检查点恢复。 我找到了有关如何从运行中保存特定模型的文档,但我想保存搜索的整个状态,包括已经尝试过的内容和这些实验的结果。

我可以直接调用 Tuner.get_state(),保存结果,然后从 Tuner.set_state() 中断的地方继续吗?或者有其他方法吗?

解决方法

您不需要调用 tuner.get_state()tuner.set_state()。在实例化 Tuner 时,请说 RandomSearch,如 example 中所述,

# While creating the tuner for the first time
tuner = RandomSearch(
    build_model,objective="val_accuracy",max_trials=3,executions_per_trial=2,directory="my_dir",project_name="helloworld",)

您需要设置参数 directoryproject_name。检查点保存在此目录中。您可以将此目录保存为 ZIP 文件并使用 files.download() 下载。

当您获得新的 Colab 实例时,解压缩该存档并恢复 my_dir 目录。再次使用,

实例化 Tuner
# While loading the Tuner
tuner = RandomSearch(
    build_model,# <----- Use the directory as you did earlier
    overwrite=False,# <------- 
    project_name="helloworld",)

现在开始搜索,您会注意到 best params so far 没有改变。此外,tuner.results_summary() 返回所有搜索结果。

请参阅 Tunerhere 的文档。