Friday, May 19, 2017

Keras: Hyperparameter optimization example 1

import numpy
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.cross_validation import train_test_split

from keras.utils import np_utils
def one_hot_encode_object_array(arr):
    '''One hot encode a numpy array of objects (e.g. strings)'''
    uniques, ids = np.unique(arr, return_inverse=True)
    return np_utils.to_categorical(ids, len(uniques))
def create_model():
    model = Sequential()
    model.add(Dense(12, input_dim=4, activation='relu'))
    model.add(Dense(3, activation='sigmoid'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

seed = 7
numpy.random.seed(seed)
dataframe = pd.read_csv("iris.csv",header=None)
dataset = dataframe.values

X = dataset[:,0:4].astype(float)
Y = dataset[:,4]

train_X, test_X, train_y, test_y = train_test_split(X, Y, train_size=0.5, random_state=1)
#print X
Y_train = one_hot_encode_object_array(train_y)
Y_test = one_hot_encode_object_array(test_y)
#Y = one_hot_encode_object_array(Y1)
#print Y
model = KerasClassifier(build_fn=create_model, verbose=0)
batch_size = [10,20,40]
epochs =[10,50,100]
param_grid= dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid_result=grid.fit(train_X,Y_train)

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))


-----------------------------------------------------------------------------
Results
Best:0.826667 using {'epochs': 100, 'batch_size': 10}
0.266667 (0.018856) with: {'epochs': 10, 'batch_size': 10}
0.640000 (0.198662) with: {'epochs': 50, 'batch_size': 10}
0.826667 (0.099778) with: {'epochs': 100, 'batch_size': 10}
0.413333 (0.049889) with: {'epochs': 10, 'batch_size': 20}
0.506667 (0.082192) with: {'epochs': 50, 'batch_size': 20}
0.773333 (0.147271) with: {'epochs': 100, 'batch_size': 20}
0.266667 (0.018856) with: {'epochs': 10, 'batch_size': 40}
0.320000 (0.299333) with: {'epochs': 50, 'batch_size': 40}
0.746667 (0.131993) with: {'epochs': 100, 'batch_size': 40}
 

No comments:

Post a Comment