使用 Keras Tuner 进行调谐器搜索

sil*_*ter 0 python keras keras-tuner

我正在使用 Keras Tuner 包。我尝试使用此处解释的示例进行超参数调整https://www.tensorflow.org/tutorials/keras/keras_tuner。代码运行得很好,但是当我开始编写代码时,但是当我尝试第二次和第三次开始时,我遇到了问题。

tuner.search(X_train, Y_train, epochs=50, validation_split=0.2, callbacks=[stop_early])

# Get the optimal hyperparameters
best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]

print(f"""
The hyperparameter search is complete. The optimal number of units in the first densely-connected
layer is {best_hps.get('units')} and the optimal learning rate for the optimizer
is {best_hps.get('learning_rate')}.
""")
Run Code Online (Sandbox Code Playgroud)

第二次执行后,代码不会启动并显示上一次的结果。

INFO:tensorflow:Oracle triggered exit

The hyperparameter search is complete. The optimal number of units in the first densely-connected
layer is 128 and the optimal learning rate for the optimizer
is 0.001.
Run Code Online (Sandbox Code Playgroud)

那么知道如何解决这个问题吗?

小智 5

Keras Tuner 正在将检查点保存在 gcs 或本地目录的目录中。如果您想稍后恢复搜索,则可以使用此选项。由于您的搜索之前已经完成,因此再次运行搜索不会执行任何操作。您必须先删除该目录才能重新开始搜索。

在您的示例中,在调谐器搜索之前,您将拥有以下内容:

tuner = kt.Hyperband(model_builder,
                     objective='val_accuracy',
                     max_epochs=10,
                     factor=3,
                     directory='my_dir',
                     project_name='intro_to_kt')
Run Code Online (Sandbox Code Playgroud)

那就是要删除的目录。

下次,要在开始之前自动删除,您可以将该代码更改为:

tuner = kt.Hyperband(model_builder,
                     objective='val_accuracy',
                     max_epochs=10,
                     factor=3,
                     directory='my_dir',
                     project_name='intro_to_kt',
                     # if True, overwrite above directory if search is run again - i.e. don't resume
                     overwrite = True)
Run Code Online (Sandbox Code Playgroud)