GridSearchCV 返回错误“参数无效”

问题描述 投票:0回答:1

我正在尝试使用 GridSearchCV 调整超参数,但是代码返回参数无效的错误,即使它们应该完美工作。 我的tensorflow和keras版本是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)

这是我的错误消息

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

我该如何解决这个问题?

python keras scikit-learn deep-learning gridsearchcv
1个回答
0
投票

这可以通过在参数神经元和优化器之前添加

model__
来解决,如下所示:

# define the parameter range
param_grid = {'model__neurons': [2, 8, 16],
              'batch_size': [4, 16],
              'model__optimizer': ['SGD', 'RMSprop', 'Adam']} 
© www.soinside.com 2019 - 2024. All rights reserved.