I am trying to use keras_tuner
with cross-validation for hyperparameter optimization. My code looks as follows:
for i in range(5):
train_df = df[df['fold'] != i]
valid_df = df[df['fold'] == i]
.
.
.
tuner = kt.BayesianOptimization(build_model, objective = 'val_loss', max_trials = 2, directory = 'keras_tuner', overwrite = True)
tuner.search_space_summary()
tuner.search(train_dataset, epochs = 2, validation_data= validation_dataset, verbose=2)
tuner.results_summary()
best_model = tuner.get_best_models()[0]
best_model.save(f"models/cv/model_{i}.h5")
I am getting the following error:
Traceback (most recent call last):
File "/home/2222/Desktop/red/cv_grid.py", line 646, in <module>
tuner = kt.BayesianOptimization(build_model, objective = 'val_loss', max_trials = 2, directory = 'keras_tuner', overwrite = True)
File "/home/2222/anaconda3/envs/tf/lib/python3.10/site-packages/keras_tuner/tuners/bayesian.py", line 402, in __init__
super().__init__(oracle=oracle, hypermodel=hypermodel, **kwargs)
File "/home/2222/anaconda3/envs/tf/lib/python3.10/site-packages/keras_tuner/engine/tuner.py", line 113, in __init__
super().__init__(
File "/home/2222/anaconda3/envs/tf/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 122, in __init__
tf.io.gfile.rmtree(self.project_dir)
File "/home/2222/anaconda3/envs/tf/lib/python3.10/site-packages/tensorflow/python/lib/io/file_io.py", line 674, in delete_recursively_v2
_pywrap_file_io.DeleteRecursively(compat.path_to_bytes(path))
tensorflow.python.framework.errors_impl.FailedPreconditionError: keras_tuner/untitled_project/trial_1; Directory not empty
Exception ignored in: <function _CheckpointRestoreCoordinatorDeleter.__del__ at 0x155517d15f30>
Traceback (most recent call last):
File "/home/2222/anaconda3/envs/tf/lib/python3.10/site-packages/tensorflow/python/checkpoint/checkpoint.py", line 193, in __del__
TypeError: 'NoneType' object is not callable
Can someone please tell me what needs to be changed?