blinkjisoo
blinkjisoo

Reputation: 41

Keras Tuner get_best_hyperparameters()

Is there any way to have the best hyperparameters get returned as a list that I can access in other parts of my code? I don't want the entire model, I just want to be able to extract the values of the optimal hyperparameters it finds and use it in a different python file.

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)

For example, I would like to retrieve the list of best_hp, or the summary of the best hyperparameters that results_summary returns to my terminal.

Upvotes: 4

Views: 6687

Answers (1)

geeeb
geeeb

Reputation: 81

I think I found a way to do it. Turns out there is a dictionary that stores the best hyperparameters values and names, to acces it you have to type the following (try it in the console first):

best_hp.values

This is of course, assuming that you have already done the tuning and hyperparameter search. It's odd that I couldn't find this anywhere in the documentation. I ended up finding after diving into the 'best_hps' in Spyder's variable explorer.

P.S.: bear with me, this is my first StackOverflow answer

Upvotes: 8

Related Questions