Last active
December 10, 2019 20:19
-
-
Save Joelfranklin96/2adb0472b1aba6f83f26493b47e0b18a to your computer and use it in GitHub Desktop.
GridSearchCv - Activation function and kernel initializer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Defining the model | |
def create_model(activation_function,init): | |
model = Sequential() | |
model.add(Dense(8,input_dim = 8,kernel_initializer = init,activation = activation_function)) | |
model.add(Dropout(0.1)) | |
model.add(Dense(4,input_dim = 8,kernel_initializer = init,activation = activation_function)) | |
model.add(Dropout(0.1)) | |
model.add(Dense(1,activation = 'sigmoid')) | |
adam = Adam(lr = 0.001) | |
model.compile(loss = 'binary_crossentropy',optimizer = adam,metrics = ['accuracy']) | |
return model | |
# Create the model | |
model = KerasClassifier(build_fn = create_model,verbose = 0,batch_size = 40,epochs = 10) | |
# Define the grid search parameters | |
activation_function = ['softmax','relu','tanh','linear'] | |
init = ['uniform','normal','zero'] | |
# Make a dictionary of the grid search parameters | |
param_grids = dict(activation_function = activation_function,init = init) | |
# Build and fit the GridSearchCV | |
grid = GridSearchCV(estimator = model,param_grid = param_grids,cv = KFold(),verbose = 10) | |
grid_result = grid.fit(X_standardized,y) | |
# Summarize the results | |
print('Best : {}, using {}'.format(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('{},{} with: {}'.format(mean, stdev, param)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment