Is there a possibility of saving the results of keras-tuner as Dataframe? All I can find are printing functions like result_summary(), but I cannot access the printed content. The example below both prints print None, while the result_summary() still prints the best results. It looks like I will have to access the trial results by traversing through the saved files. I hoped to get a prepared table as printed by result_summary().
optimizer = keras_tuner.BayesianOptimization(
hypermodel=build_model,
objective='val_loss',
max_trials=5,
# num_initial_points=2,
# alpha=0.0001,
# beta=2.6,
seed=1,
hyperparameters=None,
tune_new_entries=True,
allow_new_entries=True
#,min_trials=3
)
search = optimizer.search(x=train_X, y=train_Y,
epochs=epochs, validation_data=(test_X, test_Y))
results = optimizer.results_summary(num_trials=10)
print(results)
print(search)
If you don't need to store "score" with the hyperparameters, this should do what you want.
You need to get the hyperparameters (HPs). The HPs are stored in hp.get_config() under ["values"] key. You collect a list of dicts with the HPs and convert them into DataFrame and to csv file.
best_hps = optimizer.get_best_hyperparameters(num_trials=max_trials)
HP_list = []
for hp in best_hps:
HP_list.append(hp.get_config()["values"])
HP_df = pd.DataFrame(HP_list)
HP_df.to_csv("name.csv", index=False, na_rep='NaN')
If you wish to save the scores too, you need to go through trials and concatenate the hyperparameter dict with its score as this
trials = optimizer.oracle.get_best_trials(num_trials=max_trials)
HP_list = []
for trial in trials:
HP_list.append(trial.hyperparameters.get_config()["values"] | {"Score": trial.score})
HP_df = pd.DataFrame(HP_list)
HP_df.to_csv("name.csv", index=False, na_rep='NaN')