In order to test my code, I would like to launch multiple hyperparameter searches in succession. For each search, I create a new tuner (HyperBand) that I compile and retrieve its best hyperparameters found.
class HyperparametersSearch:
def __init__(self, activation_functions, ...):
self.choices_activation_functions = activation_functions
...
def search():
tuner = kt.Hyperband(
build_model,
objective="val_loss",
max_epochs=10,
hyperband_iterations=5,
)
tuner.search(
x=self.X_train,
y=self.y_train,
validation_data=(self.X_val, self.y_val),
callbacks=[EarlyStopping(patience=5)],
)
return tuner.get_best_hyperparameters(1)[0]
def build_model(self, hp) -> Model:
input_layer = Input(
shape=(
self.steps_in,
self.nb_features,
)
)
current_layer = input_layer
activation_functions = hp.Choice("activation_function", self.choices_activation_functions)
current_layer = self.__generate_block_structure(
hp, current_layer, activation_functions, None,
)
flat_layer = Flatten()(current_layer)
output_layer: Dense = Dense(units=self.nb_steps_out)(
flat_layer
)
model = Model(input_layer, output_layer)
optimizer = Adam(
learning_rate=hp.Float("learning_rate", 1e-5, 3e-5, sampling="log")
)
model.compile(
optimizer=optimizer,
loss="mae",
metrics=None,
)
return model
search_1 = HyperparametersSearch(activation_functions=["relu", "selu"])
hp_search_1 = search_1.search()
keras.backend.clear_session()
search_2 = HyperparametersSearch(activation_functions=["logcosh", "selu"]) # not performed
hp_search_2 = search_2.search() # return same as hp_search_1, with relu activation_function
keras.backend.clear_session()
I realise that between each search, the process is not reset:
- During the first search, I find some of the best hyperparameters.
- I then launch my second search, the search process doesn't even start and returns the same best hyperparameters found for the first one.
I used keras.backend.clear_session()
between search processes in order to reset keras-tuner, but it does not work..
Would anyone have an idea to allow several hyperparameter searches in a succession?
Thanks
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…