Last active
July 31, 2022 17:02
-
-
Save Joelfranklin96/346cd124a3735887f9012afdd8d3a499 to your computer and use it in GitHub Desktop.
GridSearchCV - Number of neurons
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(neuron1,neuron2): | |
model = Sequential() | |
model.add(Dense(neuron1,input_dim = 8,kernel_initializer = 'uniform',activation = 'tanh')) | |
model.add(Dropout(0.1)) | |
model.add(Dense(neuron2,input_dim = neuron1,kernel_initializer = 'uniform',activation = 'tanh')) | |
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 | |
neuron1 = [4,8,16] | |
neuron2 = [2,4,8] | |
# Make a dictionary of the grid search parameters | |
param_grids = dict(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