激活参数在 GridSearch 中不起作用
Posted
技术标签:
【中文标题】激活参数在 GridSearch 中不起作用【英文标题】:Activation parameter not working in GridSearch 【发布时间】:2022-01-11 23:35:41 【问题描述】:我正在尝试为最佳参数创建一个 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()`
【问题讨论】:
【参考方案1】:我认为他们改变了一些东西,因为我只能将activation=relu
参数传递给KerasClassifier
。
那里不需要其他参数。
【讨论】:
【参考方案2】:我遇到了同样的问题。以下代码在使用 keras.wrappers 时运行完美
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)
【讨论】:
以上是关于激活参数在 GridSearch 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
@font-face 在 Firefox 中不起作用 [重复]