使用GridSearchCV调整Keras超参数-无法在Dense层中调整'kernel_initializer'

时间:2018-08-11 10:23:57

标签: python tensorflow scikit-learn keras grid-search

我是keras和神经网络的新手。我正在尝试使用scikit-learn中的GridSearchCV和python中的keras来调整简单神经网络的超参数。下面是示例代码供参考。

def base_model(input_layer_nodes = 150, optimizer = 'adam', kernel_initializer = 'normal', dropout_rate = 0.2):

    model = Sequential()

    model.add(Dense(units = input_layer_nodes, input_dim = 107, kernel_initializer = kernel_initializer, activation='relu'))
    Dropout(dropout_rate)

    model.add(Dense(units = 1, kernel_initializer = kernel_initializer, activation='sigmoid'))

    # Compile model
    model.compile(loss = 'binary_crossentropy', optimizer = optimizer, metrics = ['accuracy'])

    return model

# Defining parameters for performing GridSearch
# optimizer = ['sgd', 'rmsprop', 'adam']
# dropout_rate = [0.1, 0.2, 0.3, 0.4, 0.5]
# input_layer_nodes = [50, 107, 150, 200]
kernel_initializer = ['uniform', 'normal']

param_grid = dict(kernel_initializer = kernel_initializer)

model = KerasClassifier(build_fn = base_model, epochs = 10, batch_size = 128, verbose = 2)

grid = GridSearchCV(estimator = model, param_grid=param_grid, n_jobs = 1, cv = 5)
grid.fit(X_train, y_train)

# View hyperparameters of best neural network
print("\nBest Training Parameters: ", grid.best_params_)
print("Best Training Accuracy: ", grid.best_score_)

执行上面的代码时,出现以下错误。

ValueError: ('Some keys in session_kwargs are not supported at this time: %s', dict_keys(['kernel_initializer']))

我能够调整网络的其他一些参数,例如dropout_rate,优化器,时期。如果相同的代码可用于其他参数,为什么kernel_initializer部分不起作用?我正在使用keras 2.2.2,tensorflow 1.9.0-gpu和python 3.6.6。我的OS Windows 10 x64。任何帮助,将不胜感激。

0 个答案:

没有答案