Tuning parameters in keras

keras Import 
from keras.datasets Import MNIST
from keras.models Import Sequential
from keras.layers Import Dense, Dropout, Activation, Flatten
from keras.layers Import Conv2D, MaxPooling2D
from keras.wrappers.scikit_learn KerasClassifier # Import
from keras Import backend AS K
from grid search sklearn.model_selection GridSearchCV # Import
# Comment out this section if a non-windows
Import win_unicode_console
win_unicode_console.enable ()
# Comment out this section if a non-windows

num_classes = 10

# INPUT Image Dimensions
img_rows, img_cols = 28, 28

# Training Data Load do Basic Data Normalization and
# (x_train, y_train), (x_test, android.permission.FACTOR.) = mnist.load_data ()
(x_train, y_train), (x_test, y_test) = mnist.load_data(path='data/mnist.npz')

#判断图片维度的格式
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

# convert class vectors to binary class matrices
= keras.utils.to_categorical y_train (y_train, num_classes)
android.permission.FACTOR. keras.utils.to_categorical = (android.permission.FACTOR., num_classes)

# configured neural network: parameters (fully connected layers, output dimensions, core, pooled)
DEF make_model (dense_layer_sizes , filters, kernel_size, pool_size): # make_model parameters is to tune the parameters of the model, we have to build a dictionary, key values of these parameters to keep correspondence in the dictionary;
'' 'Creates Comprised of model 2 Convolutional Layers the Followed by Dense Layers

dense_layer_sizes: List of Layer sizes.
This List has One Number for each Layer
Filters: Number The of Convolutional Filters in each Convolutional Layer
kernel_size: Convolutional Kernel size
pool_size: size of Pooling Area for max Pooling
'' '

Model = Sequential ()
model.add (Conv2D (Filters, kernel_size,
= padding 'Valid',
input_shape = input_shape))
model.add (Activation ( 'RELU'))
model.add (Conv2D (Filters, kernel_size))
model.add (Activation ( 'RELU'))
model.add (MaxPooling2D ( = pool_size pool_size))
model.add (Dropout (0.25))

model.add (The Flatten ())
for layer_size in dense_layer_sizes:
model.add (the Dense (layer_size))
model.add (Activation ( 'RELU'))
model.add (Dropout (0.5))
model.add (the Dense (num_classes))
model.add (activation ( 'SoftMax'))
# 2 convolutional layer, the first active layer 'relu' Finally 'softmax', pooled layer, was discarded ,
# flattening layer, dense_layer_sizes layers fully connected layers, the final layer is fully connected to the output of num_classes features,
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])

return model

dense_size_candidates = [[32], [64], [32, 32], [64, 64]]
my_classifier = KerasClassifier(make_model, batch_size=32)
validator = GridSearchCV(my_classifier,
param_grid={'dense_layer_sizes': dense_size_candidates,
# epochs is avail for tuning even when not
# an argument to model building function
'epochs': [3, 6],
'filters': [8],
'kernel_size': [3],
'pool_size': [2]},
scoring='accuracy',
n_jobs=1)
# validator.fit(x_train, y_train)
validator.fit(x_train,y_train)
print('The parameters of the best model are: ')
print(validator.best_params_)

# validator.best_estimator_ returns sklearn-wrapped version of best model.
# validator.best_estimator_.model returns the (unwrapped) keras model
best_model = validator.best_estimator_.model
metric_names = best_model.metrics_names
metric_values = best_model.evaluate(x_test, y_test)
for metric, value in zip(metric_names, metric_values):
print(metric, ': ', value)

Guess you like

Origin www.cnblogs.com/1112-msf/p/12543520.html