<jupyter_start><jupyter_text>9.3 Grid Search Deep Learning Model Parameters<jupyter_code># Setup 
from keras.models import Sequential
from keras.layers import Dense

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score

import numpy as np
import tensorflow as tf
print(tf.__version__)
seed = 7
np.random.seed(seed)
# Load dataset
dataset = np.loadtxt("pima-indians-diabetes.csv", delimiter=",")
X = dataset[:,0:8]
Y = dataset[:,8]
# Function to create model, required for KerasClassifier
def create_model(optimizer='rmsprop', init='glorot_uniform'):
    # Create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, kernel_initializer=init, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model
# Create model (create model signature: create_model(optimizer='rmsprop', init='glorot_uniform'))
model = KerasClassifier(build_fn=create_model, verbose=0)
# Grid search optimizers, initializers, number of epochs, batch size
from sklearn.model_selection import GridSearchCV

optimizers = ['rmsprop', 'adam']
inits = ['glorot_uniform', 'normal', 'uniform']
epochs = [50, 100, 150]
batches = [5, 10, 20]
param_grid = dict(optimizer=optimizers, epochs=epochs, batch_size=batches, init=inits)
grid = GridSearchCV(estimator=model, param_grid=param_grid, verbose=2)
grid_result = grid.fit(X, Y)  
# Results
print("Best: %.2f%% using %s" % (grid_result.best_score_*100, 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("%.2f%% +/- %.2f%% with: %r" % (mean*100, stdev*100, param))<jupyter_output>Best: 74.87% using {'batch_size': 5, 'epochs': 150, 'init': 'normal', 'optimizer': 'rmsprop'}
69.01% +/- 1.76% with: {'batch_size': 5, 'epochs': 50, 'init': 'glorot_uniform', 'optimizer': 'rmsprop'}
66.93% +/- 4.31% with: {'batch_size': 5, 'epochs': 50, 'init': 'glorot_uniform', 'optimizer': 'adam'}
67.32% +/- 3.41% with: {'batch_size': 5, 'epochs': 50, 'init': 'normal', 'optimizer': 'rmsprop'}
72.53% +/- 4.54% with: {'batch_size': 5, 'epochs': 50, 'init': 'normal', 'optimizer': 'adam'}
70.83% +/- 2.44% with: {'batch_size': 5, 'epochs': 50, 'init': 'uniform', 'optimizer': 'rmsprop'}
70.96% +/- 0.80% with: {'batch_size': 5, 'epochs': 50, 'init': 'uniform', 'optimizer': 'adam'}
69.79% +/- 1.76% with: {'batch_size': 5, 'epochs': 100, 'init': 'glorot_uniform', 'optimizer': 'rmsprop'}
67.19% +/- 4.22% with: {'batch_size': 5, 'epochs': 100, 'init': 'glorot_uniform', 'optimizer': 'adam'}
70.70% +/- 1.28% with: {'batch_size': 5, 'epochs': 100, 'init': 'normal', 'optimizer': 'rmsprop'}
72.66% +[...]
{'fraction_non_alphanumeric': 0.18426361802286484, 'fraction_numerical': 0.04808338937457969, 'mean_word_length': 6.168674698795181, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '142796', 'license': 'Apache-2.0', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1088, 'n_words': 266, 'path': 'keras-presentation/diabetes-example-04.ipynb', 'repo_name': 'mgfeller/tensorflow', 'size': '2974'}