使用 RandomSearchCV 发生运行时错误

RunTime Error occurs using RandomSearchCV

我想用 sklearn 的 RandomSearchCV 实现随机搜索。很遗憾 我总是得到 RunTimeError:

RuntimeError: Cannot clone object <tensorflow.python.keras.wrappers.scikit_learn.KerasRegressor object at 0x13f81f850>, as the constructor either does not set or modifies parameter learning_rate2

这是我的代码:

def create_model2(nn21=64, nn22=32, dropout_rate1=0, learning_rate2=0.01):
    model = Sequential()
    model.add(Dense(nn21, activation='relu'))
    model.add(Dropout(dropout_rate1))
    model.add(Dense(nn22, activation='relu'))
    #model.add(Dropout(dropout_rate2))
    model.add(Dense(1))
    opt = keras.optimizers.Adam(learning_rate=learning_rate2)
    model.compile(optimizer=opt,
                loss='mse',
                metrics=['mean_absolute_percentage_error', 'mae'])
    return model

    # Hyperparameter Tuning
    model3 = KerasRegressor(build_fn=create_model2, verbose=0)
    param_dist = {'learning_rate2': list(np.arange(0.001, 0.05, 0.001)),
                  'nn21': list(range(16, 512, 16)),
                  'nn22': list(range(16, 512, 16)),
                  'dropout_rate1': list(np.arange(0, 1, 0.1))}
    grid = RandomizedSearchCV(model3, param_distributions=param_dist, n_iter=10, n_jobs=-1, cv=5, scoring='neg_mean_absolute_error')
    grid_result = grid.fit(X_train, Y_train)
    print("Best: %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))

我试过这样做:

'learning_rate2': [list(np.arange(0.001, 0.05, 0.001))]

或者这个:

'learning_rate2': (list(np.arange(0.001, 0.05, 0.001)))

但没有任何效果。我是否犯了任何错误,或者是否有其他解决方案而不是降级这里解释的 sklearn 版本 Cannot clone object <tensorflow.python.keras.wrappers.scikit_learn.KerasClassifier object?

试试这个:

  param_dist = {'learning_rate2': np.arange(0.001, 0.05, 0.001).tolist(),
                  'nn21': list(range(16, 512, 16)),
                  'nn22': list(range(16, 512, 16)),
                  'dropout_rate1': list(np.arange(0, 1, 0.1))}