keras 激活参数在GridSearch中无效

bf1o4zei  于 2022-11-13  发布在  其他
关注(0)|答案(3)|浏览(249)

我正在尝试使用GridSearch查找最佳参数,如下所示:

def MultiPerceptron(optimizer = 'adam', loss = 'binary_cross_entropy', kernel_initializer = 'random_uniform', activation = 'relu', units = 16):
  model = Sequential()
  model.add(InputLayer(30))
  model.add(Dense(units = units, activation = activation, kernel_initializer = kernel_initializer))
  model.add(Dense(units = units, activation = activation, kernel_initializer = kernel_initializer))
  model.add(Dense(units = 1, activation = 'sigmoid'))
  model.compile(optimizer = optimizer, loss = loss, metrics =['binary_accuracy'])
  return model

classifier = KerasClassifier(build_fn = MultiPerceptron, validation_split = 0.1, validation_batch_size = 50)
param = {'batch_size': [10, 30],
         'epochs': [50, 100],
         'optimizer': ['adam', 'sgd'],
         'loss': ['binary_crossentropy', 'hinge'],
         'kernel_initializer': ['random_uniform', 'normal'],
         'activation': ['relu', 'tanh'],
         'units': [16, 8]}

search = GridSearchCV(estimator = classifier, param_grid = param, scoring = 'accuracy', cv = 5)
search = search.fit(x,y)

我得到以下错误:

ValueError: Invalid parameter activation for estimator KerasClassifier.
This issue can likely be resolved by setting this parameter in the KerasClassifier constructor:
`KerasClassifier(activation=relu)`
Check the list of available parameters with `estimator.get_params().keys()`
q7solyqu

q7solyqu1#

我认为他们改变了一些东西,因为我只能通过将activation=relu参数传递给KerasClassifier来使它工作。
此处不需要其他参数。

bq9c1y66

bq9c1y662#

我也遇到了同样的问题。下面的代码在使用keras时运行得很好。

def build_model(lambda_parameter):
    model = Sequential()
    model.add(Dense(10, input_dim=X.shape[1], activation='relu', 
    kernel_regularizer=l2(lambda_parameter)))
    model.add(Dense(6, activation='relu', 
    kernel_regularizer=l2(lambda_parameter)))
    model.add(Dense(4, activation='relu', 
    kernel_regularizer=l2(lambda_parameter)))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer='sgd', metrics= 
    ['accuracy'])
    return model
seed = 1
np.random.seed(seed)
random.set_seed(seed)
model = KerasClassifier(build_fn=build_model, verbose=0, shuffle=False)
lambda_parameter = [0.01, 0.5, 1]
epochs = [50, 100]
batch_size = [20]
param_grid = dict(lambda_parameter=lambda_parameter, epochs=epochs, 
batch_size=batch_size)
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5)
results_1 = grid_search.fit(X, y)
print(f"Best cross-validation score = {results_1.best_score_}")
print(f"Parameters for best cross-validation score = 
{results_1.best_params_}")
accuracy_means = results_1.cv_results_['mean_test_score']
accuracy_stds = results_1.cv_results_['std_test_score']
parameters = results_1.cv_results_['params']
for p in range(len(parameters)):
    print(f"Accuracy {accuracy_means[p]} for params {accuracy_stds[p]}, 
    {parameters[p]}

但在切换到Scikeras后,我总是得到一个ValueError:

ValueError: Invalid parameter lambda_parameter for estimator 
KerasClassifier.
This issue can likely be resolved by setting this parameter in the 
KerasClassifier constructor: KerasClassifier(lambda_parameter=0.01)`
Check the list of available parameters with 
estimator.get_params().keys()`

我在KerasClassifiet中添加了lambda_parameter=0.01来解决这个问题

model = KerasClassifier(model=build_model, verbose=0, shuffle=False, 
lambda_parameter=0.01)
ilmyapht

ilmyapht3#

KerasClassifier构造函数中使用激活和层

def create_model(layers, activation):
        model= Sequential()
        for i, nodes in enumerate(layers):
            if i==0:
                model.add(Dense(nodes, input_dim=X_train.shape[1]))
                model.add(Activation(activation))
            else:
                model.add(Dense(nodes))
                model.add(Activation(activation))
        model.add(Dense(1))
        
        model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
        return model
    
    model= KerasClassifier(model=create_model, verbose=0, activation='relu', layers=20)

然后

layers=[[20],[40,20], [45, 30, 15]]
activations = ['sigmoid','relu']
param_grid = dict(layers=layers, activation=activations, batch_size=[128, 256], epochs=[30])
grid = GridSearchCV(estimator=model, param_grid=param_grid)

grid_result= grid.fit(X_train, y_train)
[grid_result.best_score_,grid_result.best_params_]

它的工作!最后得到了下面的输出:

[0.8397500000000001,
 {'activation': 'relu',
  'batch_size': 128,
  'epochs': 30,
  'layers': [45, 30, 15]}]

相关问题