简体   繁体   English

在 scikit-optimize 中使用 KerasRegressor 的示例

[英]Example of using a KerasRegressor in scikit-optimize

I am using the awesome scikit-optimize toolbox for hyperparameter optimization.我正在使用很棒的 scikit-optimize 工具箱进行超参数优化。 My goal is to compare keras and scikit-learn models.我的目标是比较 keras 和 scikit-learn 模型。

According to the example https://scikit-optimize.github.io/stable/auto_examples/sklearn-gridsearchcv-replacement.html#sphx-glr-auto-examples-sklearn-gridsearchcv-replacement-py only scikit learn models had been used.根据示例https://scikit-optimize.github.io/stable/auto_examples/sklearn-gridsearchcv-replacement.html#sphx-glr-auto-examples-sklearn-gridsearchcv-replacement-py只使用了 learnkit 模型. Trying something like the following code does not allow to integrate the keras mode in the BayesSearchCV.尝试类似以下代码的操作不允许在 BayesSearchCV 中集成 keras 模式。

# Function to create model, required for KerasRegressor
def create_model(optimizer='rmsprop', init='glorot_uniform'):
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, kernel_initializer=init, activation='relu'))
    model.add(Dense(8, kernel_initializer=init, activation='relu'))
    model.add(Dense(1, kernel_initializer=init, activation='linear'))
    # Compile model
    model.compile(loss='mse', optimizer=optimizer, metrics=['r2'])
    return model

model = KerasRegressor(build_fn=create_model, verbose=0)
NN_search = {
    'model': [model()],
    'model__optimizers': optimizers,
    'model__epochs' : epochs, 
    'model__batch_size' : batches, 
    'model__init' : init
}

Does anyone managed to merge a KerasClassifier/Regressor into BayesSearch CV?有没有人设法将 KerasClassifier/Regressor 合并到 BayesSearch CV 中?

This would be incredibly useful!这将非常有用!

Well, I found an option to define a model that is build up based on global parameters.好吧,我找到了一个选项来定义基于全局参数构建的 model。 So inside of the scikit-opt minimize function the objective function is called, here the global parameters are set, and used in the create_model_NN function, which is built on the keras scikit-learn KerasRegressor Wrapper. So inside of the scikit-opt minimize function the objective function is called, here the global parameters are set, and used in the create_model_NN function, which is built on the keras scikit-learn KerasRegressor Wrapper.

def create_model_NN():
    #start the model making process and create our first layer
    model = Sequential()
    model.add(Dense(num_input_nodes, input_shape=(40,), activation=activation
                   ))
    #create a loop making a new dense layer for the amount passed to this model.
    #naming the layers helps avoid tensorflow error deep in the stack trace.
    for i in range(num_dense_layers):
        name = 'layer_dense_{0}'.format(i+1)
        model.add(Dense(num_dense_nodes,
                 activation=activation,
                        name=name
                 ))
    #add our classification layer.
    model.add(Dense(1,activation='linear'))
    
    #setup our optimizer and compile
    adam = Adam(lr=learning_rate)
    model.compile(optimizer=adam, loss='mean_squared_error',
                 metrics=['mse'])
    return model

def objective_NN(**params):
    print(params)

    global learning_rate
    learning_rate=params["learning_rate"]
    global num_dense_layers
    num_dense_layers=params["num_dense_layers"]
    global num_input_nodes
    num_input_nodes=params["num_input_nodes"]
    global num_dense_nodes
    num_dense_nodes=params["num_dense_nodes"]
    global activation
    activation=params["activation"]
    
    model = KerasRegressor(build_fn=create_model, epochs=100, batch_size=1000, verbose=0)
    X_train, X_test, y_train, y_test = train_test_split(X_time, y_time, test_size=0.33, random_state=42)
    
    model.fit(X_train, y_train)
    
    y_pr = model.predict(X_test)
    
    res = metrics.r2_score(y_test, y_pr)
return -res

And to call it:并称之为:

res_gp = gp_minimize(objective_NN, space_NN, n_calls=10, random_state=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 `scikit-optimize` package 中的 TypeError - TypeError inside the `scikit-optimize` package Scikit-优化如何将收敛图保存到文件 - Scikit-Optimize how to save convergence plot to file XGBoost 和 scikit-optimize:BayesSearchCV 和 XGBRegressor 不兼容 - 为什么? - XGBoost and scikit-optimize: BayesSearchCV and XGBRegressor are incompatible - why? scikit-learn 0.24.1 和 scikit-optimize 0.8.1 之间的不兼容问题 - incompatibility issue between scikit-learn 0.24.1 and scikit-optimize 0.8.1 scikit-optimize 中的 cv_results_ 和 best_score_ 的测试分数是如何计算的? - How are the test scores in cv_results_ and best_score_ calculated in scikit-optimize? 使用来自 Scikit Optimize 的 @use_named_args - Using @use_named_args from Scikit Optimize Scikit 优化和离散变量 - Scikit optimize and discrete variables 使用scikit-learn的数据集不平衡且负多数 - Imbalanced data set with a negative example majority using scikit-learn 使用所有序列输出和一系列目标使用 LSTM 训练 KerasRegressor - Training a KerasRegressor with LSTM using all sequence outputs and a whole series of targets 来自 scikit-learn 的 plot_partial_dependence() 错误地为正确拟合的模型(例如 KerasRegressor 或 LGBMClassifier)引发 NotFittedError - plot_partial_dependence() from scikit-learn incorrectly raises NotFittedError for properly fitted models (e.g. KerasRegressor or LGBMClassifier)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM