<jupyter_start><jupyter_text>cross validation<jupyter_code># Function to create model, required for KerasClassifier
def create_model():
#     with tf.Session(config=tf.ConfigProto(
#                         intra_op_parallelism_threads=7,
#                         inter_op_parallelism_threads=7)) as sess:
#         K.set_session(sess)

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

# fix random seed for reproducibility
seed = 7
np.random.seed(seed)
# load pima indians dataset
dataset = np.loadtxt('./data/raw-pima-indians-diabetes.csv', delimiter=',')
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=create_model, epochs=150, batch_size=10, verbose=0)
# evaluate using 10-fold cross validation
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
%time results = cross_val_score(model, X, Y, cv=kfold)
print(results.mean())
import tensorflow as tf
from keras.backend import tensorflow_backend as K

def create_model():
    # use more cpu
    with tf.Session(config=tf.ConfigProto(
                        intra_op_parallelism_threads=7,
                        inter_op_parallelism_threads=7)) as sess:
        K.set_session(sess)

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

# fix random seed for reproducibility
seed = 7
np.random.seed(seed)
# load pima indians dataset
dataset = np.loadtxt('./data/raw-pima-indians-diabetes.csv', delimiter=',')
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=create_model, epochs=150, batch_size=10, verbose=0)
# evaluate using 10-fold cross validation
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
%time results = cross_val_score(model, X, Y, cv=kfold)
print(results.mean())<jupyter_output>Wall time: 1min 35s
0.686090226709<jupyter_text>grid search CVhttps://machinelearningmastery.com/grid-search-hyperparameters-deep-learning-models-python-keras/<jupyter_code>import tensorflow as tf
from keras.backend import tensorflow_backend as K

# Function to create model, required for KerasClassifier
def create_model():
    # use more cpu
    with tf.Session(config=tf.ConfigProto(
                        intra_op_parallelism_threads=7,
                        inter_op_parallelism_threads=7)) as sess:
        K.set_session(sess)
        
        # create model
        model = Sequential()
        model.add(Dense(12, input_dim=8, activation='relu'))
        model.add(Dense(1, activation='sigmoid'))
        # Compile model
        model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
        return model

# fix random seed for reproducibility
seed = 7
np.random.seed(seed)
# load pima indians dataset
dataset = np.loadtxt('./data/raw-pima-indians-diabetes.csv', delimiter=',')
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]

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

# define the grid search parameters
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)

%time grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=5)
%time grid_result = grid.fit(X, Y)
# summarize results

print("Best: %f using %s" % (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("%f (%f) with: %r" % (mean, stdev, param))<jupyter_output>Wall time: 0 ns<jupyter_text>Minimial example of gridsearchcv<jupyter_code>from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
iris = datasets.load_iris()
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
svc = svm.SVC()
clf = GridSearchCV(svc, parameters, n_jobs=-1, cv=5)
%time clf.fit(iris.data, iris.target)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12559756627553237, 'fraction_numerical': 0.021077792264232943, 'mean_word_length': 4.3585564610011645, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '235206', 'license': 'MIT', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1523, 'n_words': 347, 'path': 'keras/model_selection_in_keras.ipynb', 'repo_name': 'ray-chunkit-chung/modeling', 'size': '4602'}