Created
December 10, 2019 21:23
-
-
Save Joelfranklin96/0e1c62f22fd3013284e655f6f1d475d6 to your computer and use it in GitHub Desktop.
Ultimate hyperparameter optimization
This file contains 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
def create_model(learning_rate,dropout_rate,activation_function,init,neuron1,neuron2): | |
model = Sequential() | |
model.add(Dense(neuron1,input_dim = 8,kernel_initializer = init,activation = activation_function)) | |
model.add(Dropout(dropout_rate)) | |
model.add(Dense(neuron2,input_dim = neuron1,kernel_initializer = init,activation = activation_function)) | |
model.add(Dropout(dropout_rate)) | |
model.add(Dense(1,activation = 'sigmoid')) | |
adam = Adam(lr = learning_rate) | |
model.compile(loss = 'binary_crossentropy',optimizer = adam,metrics = ['accuracy']) | |
return model | |
# Create the model | |
model = KerasClassifier(build_fn = create_model,verbose = 0) | |
# Define the grid search parameters | |
batch_size = [10,20,40] | |
epochs = [10,50,100] | |
learning_rate = [0.001,0.01,0.1] | |
dropout_rate = [0.0,0.1,0.2] | |
activation_function = ['softmax','relu','tanh','linear'] | |
init = ['uniform','normal','zero'] | |
neuron1 = [4,8,16] | |
neuron2 = [2,4,8] | |
# Make a dictionary of the grid search parameters | |
param_grids = dict(batch_size = batch_size,epochs = epochs,learning_rate = learning_rate,dropout_rate = dropout_rate, | |
activation_function = activation_function,init = init,neuron1 = neuron1,neuron2 = neuron2) | |
# 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