Only keep the best trial using keras-tuner (Hyperband)

185 views Asked by At

How can I tell keras_tuner to only save the best model (overwriting each trial) and not create any checkpoints or directories for each trial?

There has been discussion already about how to reduce the amount of disk space that keras_tuner uses by either re-implementing run_trial() or adjusting the _save_trail() in HyperbandOracle (which I'm not using). These processes both attempt to implement what I desire and I tried it below

import keras_tuner as kt

### Make Hyperband Class update
class HyperbandTuner(kt.Hyperband):
    def __init__(self, hypermodel, **kwargs):
        super().__init__(hypermodel, **kwargs)

    def run_trial(self, trial, *args, **kwargs):
        hp = trial.hyperparameters
        model = self.hypermodel.build(hp)
        return self.hypermodel.fit(hp, model, *args, **kwargs)

but trial_XXXX folders were still created. Another option I tried was passing a checkpoint to tuner.search() like

best_model_checkpoint = tf.keras.callbacks.ModelCheckpoint(
                            f'../Results/{directory}/{project_name}/best_model.hdf5',
                            save_best_only=True,
                            monitor='val_loss',
                            mode='min'
                       )

which saves the model after each trial if it's better than the previous model, but the issue with trial_XXXX directories persists.

I can also delete all the trial folders after the models finish running like

import shutil

### Remove all trials ###
trial_path = Path(f"{directory}/{project_name}")
shutil.rmtree(trial_path)

but it would be best to delete them as they're made or not make them at all. How can I do this?

0

There are 0 answers