2

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)

Enes
  • 33
  • 1
  • 4
  • `results = tuner.results_summary()` and `pd.DataFrame(results)` doesn't work? – Vitali Avagyan Dec 04 '22 at 21:13
  • @ReinholdN, I am not the one asking the question! – Vitali Avagyan Dec 04 '22 at 22:57
  • 1
    ops sorry buddy @VitaliAvagyan – ReinholdN Dec 04 '22 at 22:58
  • @Enes You will need to make a minimal sample in order for someone to help you out here, pandas dataframes use dictionary format to be created, in that case your keras-tuner will need to be converted to dict first before you convert it to pandas df – ReinholdN Dec 04 '22 at 22:59
  • @VitaliAvagyan, this doesn't work, unfortunately. – Enes Dec 05 '22 at 08:37
  • @ReinholdN, I added a minimal example and hope this explains my problem better. If not, please comment again, and I will try to explain it in more detail. – Enes Dec 05 '22 at 08:47
  • @Enes I can not reproduce your code without the dataset, just give a print of the result and I will be able the solve it for you. – ReinholdN Dec 07 '22 at 00:37

1 Answers1

3

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')
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Sirrah
  • 46
  • 3