How can I pass the Hyperopt params to KerasClassifier if I set conditional search space

孤街醉人 提交于 2019-12-23 04:38:05

问题


Thanks to the good answer in my last post (How to put KerasClassifier, Hyperopt and Sklearn cross-validation together), it is great help.

I have further questions:

if I set conditional search space like:

second_layer_search_space = \
  hp.choice('second_layer',
    [
      {
        'include': False,
      },
      {
        'include': True,
        'layer_size': hp.choice('layer_size', np.arange(5, 26, 5)),
      }

    ])

space = {
    'second_layer': second_layer_search_space,
    'units1': hp.choice('units1', [12, 64]),
    'dropout': hp.choice('dropout1', [0.25, 0.5]),
    'batch_size': hp.choice('batch_size', [10, 20]),
    'epochs': hp.choice('nb_epochs', [2, 3]),
    'activation': 'relu'
}

How I can make the hyper parameter as the input parameters for create_model function?

I come up with a solution to this, but do not know whether it was the right one

def create_model(units1, activation, dropout, second_layer):
    model = Sequential()
    model.add(
        Dense(units1,
              input_dim=X.shape[1],
              kernel_initializer="glorot_uniform",
              activation=activation))

    if second_layer['include']:
        model.add(Dense(units=second_layer['layer_size'], activation='relu'))

    model.add(Dropout(dropout))
    model.add(Dense(1, activation='sigmoid'))

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

    return model

来源:https://stackoverflow.com/questions/56855499/how-can-i-pass-the-hyperopt-params-to-kerasclassifier-if-i-set-conditional-searc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!