Keras: Out of memory when doing hyper parameter grid search
Using the tip given by indraforyou, I added the code to clear the TensorFlow session inside the function I pass to GridSearchCV, like this:
def create_model():
# cleanup
K.clear_session()
inputs = Input(shape=(4096,))
x = Dense(2048, activation='relu')(inputs)
p = Dense(2, activation='sigmoid')(x)
model = Model(input=inputs, outputs=p)
model.compile(optimizer='SGD',
loss='mse',
metrics=['accuracy'])
return model
And then I can invoke the grid search:
model = KerasClassifier(build_fn=create_model)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1)
It should work.
Cheers!
As indicated, the backend being used is Tensorflow. With the Tensorflow backend the current model is not destroyed, so you need to clear the session.
After the usage of the model just put:
if K.backend() == 'tensorflow':
K.clear_session()
Include the backend:
from keras import backend as K
Also you can use sklearn wrapper to do grid search. Check this example: here. Also for more advanced hyperparameter search you can use hyperas.