V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
maple1
V2EX  ›  机器学习

在 tf2.0 中为什么使用 RandomizedSearchCV 报错?

  •  1
     
  •   maple1 · 2020-03-06 19:37:01 +08:00 · 1156 次点击
    这是一个创建于 1723 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的代码如下:

    def build_model(hidden_layers = 1,
                    layer_size = 30,
                    learning_rate = 3e-3):
        model = keras.models.Sequential()
        model.add(keras.layers.Dense(layer_size, activation='relu',
                                     input_shape=x_train.shape[1:]))
        for _ in range(hidden_layers - 1):
            model.add(keras.layers.Dense(layer_size,
                                         activation = 'relu'))
        model.add(keras.layers.Dense(1))
        optimizer = keras.optimizers.SGD(learning_rate)
        model.compile(loss = 'mse', optimizer = optimizer)
        return model
    
    sklearn_model = tf.keras.wrappers.scikit_learn.KerasRegressor(
        build_fn = build_model)
    callbacks = [keras.callbacks.EarlyStopping(patience=5, min_delta=1e-2)]
    history = sklearn_model.fit(x_train_scaled, y_train,
                                epochs = 10,
                                validation_data = (x_valid_scaled, y_valid),
                                callbacks = callbacks)
    
    from scipy.stats import reciprocal
    param_distribution = {
        "hidden_layers":[1, 2, 3, 4],
        "layer_size": np.arange(1, 100),
        "learning_rate": reciprocal(1e-4, 1e-2),
    }
    
    from sklearn.model_selection import RandomizedSearchCV
    
    random_search_cv = RandomizedSearchCV(sklearn_model,
                                          param_distribution,
                                          n_iter = 10,
                                          cv = 3,
                                          n_jobs = 1)
    random_search_cv.fit(x_train_scaled, y_train, epochs = 100,
                         validation_data = (x_valid_scaled, y_valid),
                         callbacks = callbacks)
    

    运行后,报错如下:“Cannot clone object , as the constructor either does not set or modifies parameter layer_size” 这是什么原因呢,还有设置 n_jobs > 1 时也会报错

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2790 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 07:15 · PVG 15:15 · LAX 23:15 · JFK 02:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.