Skip to content

Instantly share code, notes, and snippets.

@Joelfranklin96
Last active December 10, 2019 20:18
Show Gist options
  • Save Joelfranklin96/3d9a534066b4161729eb94526b362ff2 to your computer and use it in GitHub Desktop.
Save Joelfranklin96/3d9a534066b4161729eb94526b362ff2 to your computer and use it in GitHub Desktop.
GridSearchCV
from keras.layers import Dropout
# Defining the model
def create_model(learning_rate,dropout_rate):
model = Sequential()
model.add(Dense(8,input_dim = 8,kernel_initializer = 'normal',activation = 'relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(4,input_dim = 8,kernel_initializer = 'normal',activation = 'relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(1,activation = 'sigmoid'))
adam = Adam(lr = learning_rate)
model.compile(loss = 'binary_crossentropy',optimizer = adam,metrics = ['accuracy'])
return model
# Create the model
model = KerasClassifier(build_fn = create_model,verbose = 0,batch_size = 40,epochs = 10)
# Define the grid search parameters
learning_rate = [0.001,0.01,0.1]
dropout_rate = [0.0,0.1,0.2]
# Make a dictionary of the grid search parameters
param_grids = dict(learning_rate = learning_rate,dropout_rate = dropout_rate)
# Build and fit the GridSearchCV
grid = GridSearchCV(estimator = model,param_grid = param_grids,cv = KFold(),verbose = 10)
grid_result = grid.fit(X_standardized,y)
# Summarize the results
print('Best : {}, using {}'.format(grid_result.best_score_,grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print('{},{} with: {}'.format(mean, stdev, param))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment