## How to use cross_val_score for Cross Validation in Keras
def Learn_By_Example_320():
print()
print(format('How to use cross_val_score for Cross Validation in Keras','*^82'))
import warnings
warnings.filterwarnings("ignore")
# load libraries
from keras.wrappers.scikit_learn import KerasClassifier
from keras.initializers import VarianceScaling
from keras.regularizers import l2
from keras.models import Sequential
from keras.layers import Dense
from sklearn import datasets
from sklearn.model_selection import cross_val_score
# simulated data
dataset = datasets.make_classification(n_samples=10000, n_features=20, n_informative=5,
n_redundant=2, n_repeated=0, n_classes=2, n_clusters_per_class=2,
weights=None, flip_y=0.01, class_sep=1.0, hypercube=True, shift=0.0,
scale=1.0, shuffle=True, random_state=None)
X = dataset[0]; y = dataset[1]
print(X.shape); print(y.shape)
# Define a Deep Learning Model
def create_network(optimizer='RMSprop'):
model = Sequential()
model.add(Dense(units=36, input_shape=(X.shape[1],),
kernel_regularizer=l2(0.001), # weight regularizer
kernel_initializer=VarianceScaling(), # initializer
activation='relu'))
model.add(Dense(units=28,
kernel_regularizer=l2(0.01), # weight regularizer
kernel_initializer=VarianceScaling(), # initializer
activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))
# Compile the Model
model.compile(loss='binary_crossentropy', optimizer = optimizer,
metrics=['acc','mae'])
return model
# Wrap Keras model so it can be used by scikit-learn
neural_network = KerasClassifier(build_fn=create_network, epochs=5, batch_size=10,
verbose=0)
# evaluate using 10-fold cross validation
results = cross_val_score(neural_network, X, y, cv=10, scoring='accuracy')
print(); print(results)
print(); print("Accucary: ", results.mean()*100)
print("Standard Deviation: ", results.std())
Learn_By_Example_320()