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)