GridSearchCV returns error "invalid parameters"

32 views Asked by At

I'm trying to tune hyperparameters using GridSearchCV however the code returns the error that the parameters are invalid even though they should be working perfectly. my tensorflow and keras versions are 2.15.0

#Define the keras model as a function. 
#Parameters that need to be tuned can be provided as inputs to the function 
#Here, let us tune the number of neurons and the optimizer

# Appropriate architecture for the challenge
def create_model(neurons, optimizer):
    model = Sequential()
    model.add(Dense(neurons, input_dim=30, activation='relu')) 
    model.add(Dropout(0.2))
    model.add(Dense(1)) 
    model.add(Activation('sigmoid'))  
    model.compile(loss='binary_crossentropy',
                  optimizer=optimizer,             
                  metrics=['accuracy'])
    return model
    


# define the parameter range
param_grid = {'neurons': [2, 8, 16],
              'batch_size': [4, 16],
              'optimizer': ['SGD', 'RMSprop', 'Adam']} 

# 3 x 2 x 3 = 18 combinations for parameters

#Define the model using KerasClassifier method.
#This makes our keras model available for GridSearch
model1 = KerasClassifier(build_fn=create_model, epochs=10, verbose=1)

#n_jobs=-1 parallelizes but it may crash your system. 
#Provide the metric for KFold crossvalidation. cv=3 is a good starting point
grid = GridSearchCV(estimator=model1, param_grid=param_grid, n_jobs=1, cv=3)

#Takes a long time based on the number of parameters and cv splits. 
#In our case - 18 * 3 * 2 * num_epochs = 1080 total epochs if epochs=10
grid_result = grid.fit(X, Y)

# summarize results
print("Best accuracy of: %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))

###########################################################

#Let us load the best model and predict on our input data
best_model =grid_result.best_estimator_

# Predicting the Test set results
y_pred = best_model.predict(X)
y_pred = (y_pred > 0.5)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(Y, y_pred)
sns.heatmap(cm, annot=True)

This is my error message

Invalid parameter neurons for estimator KerasClassifier.
This issue can likely be resolved by setting this parameter in the KerasClassifier constructor:
`KerasClassifier(neurons=2)`

how do I solve this problem?

1

There are 1 answers

1
Beginner_coder On

This can be solved by adding model__ before the parameters neuron and optimizer like so:

# define the parameter range
param_grid = {'model__neurons': [2, 8, 16],
              'batch_size': [4, 16],
              'model__optimizer': ['SGD', 'RMSprop', 'Adam']}