1

I am adjusting hyperparameters in Keras model using GridSearchCV from sklearn as in this tutorial

model = KerasClassifier(build_fn=create_model, verbose=0)
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)

And they I call fit method to find the best hyperparameters

grid_result = grid.fit(X, Y)

However, let's say that I want to change batch_sizes and call fit again (without restarting the kernel in Jupyter).

batch_size = [15, 20, 25, 30, 35, 40]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)

And when I call fit

grid_result = grid.fit(X, Y)

it works endlessly and won't terminate. In order to do the fit on this changed parameters I have to restart the kernel and then reload the data, modules, etc.

Question. How can I call fit second time on GridSearchCV without restarting the kernel?


Details. I use this data. Detailed snippet:

import numpy as np
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier

def create_model():
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

dataset = np.loadtxt("data/pima-indians-diabetes.data.csv", delimiter=",")
X = dataset[:,0:8]
y = dataset[:,8]

model = KerasClassifier(build_fn=create_model, verbose=0)

batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50]
param_grid = dict(batch_size = batch_size, epochs = epochs)

grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)

Then I call the fit and it works fine

grid_result = grid.fit(X, y)
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))

Out: Best: 0.690104 using {'batch_size': 10, 'epochs': 50}

Then I run the following to make some changes:

batch_size = [5, 10, 15, 20]
param_grid = dict(batch_size = batch_size, epochs = epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)

And finally I call the fit for the second time which never stops

grid_result = grid.fit(X, y)
Fallen Apart
  • 723
  • 8
  • 20

1 Answers1

2

I can reproduce the error on my MacBook Pro. I used pima-indians-diabetes.data.csv dataset as well.

The issue here is with the tensorflow session. If a session is created in the parent process before GridSearchCV.fit(), it will hang for sure.

One possible solution would be to keep all session creation code restricted to the KerasClassifer class and the model creation function.

Additionally, you may want to restrict the memory usage of TF in either the model creation function or a subclass of KerasClassifier.


Fast solution:

n_jobs = 1

but it will take a long time to finish.


References:

Session hang issue with python multiprocessing

Keras + Tensorflow and Multiprocessing in Python

Limit the resource usage for tensorflow backend

GridSearchCV Hangs On Second Run

scikit-lean GridSearchCV n_jobs != 1 freezing

seralouk
  • 30,938
  • 9
  • 118
  • 133
  • Fast solution worked, but I will yet try to load `keras` modules inside function `create_model` – Fallen Apart May 29 '18 at 12:33
  • Yes, another thing to try is to use `if __name__ == '__main__'` if you use Winsdows. let me know and please consider accepting my answer. – seralouk May 29 '18 at 12:35
  • I am ussing Google Compute VM with Debian. I will browse through links you have provided and try to find the best solution for me. `n_jobs = 1` is a poor solution because my VM provides multiple CPUs – Fallen Apart May 29 '18 at 12:37
  • I tried to wrap the code inside the function (and functions) and import `keras` module inside, but I see that I do it mindlessly and nothing works. I guess from your note *One possible solution would be to keep all session creation code restricted to the KerasClassifer class and the model creation function.* and from the post from the first link of yours, that it is possible somehow. Can you give me some hints how to do it? – Fallen Apart May 29 '18 at 13:07