如何将 Keras 模型插入 scikit-learn 管道? [英] How to insert Keras model into scikit-learn pipeline?

查看:28
本文介绍了如何将 Keras 模型插入 scikit-learn 管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Scikit-Learn 自定义管道 (sklearn.pipeline.Pipeline) 与 RandomizedSearchCV 结合用于超参数优化.这很好用.

I'm using a Scikit-Learn custom pipeline (sklearn.pipeline.Pipeline) in conjunction with RandomizedSearchCV for hyper-parameter optimization. This works great.

现在我想在管道中插入一个 Keras 模型作为第一步.应该优化模型的参数.计算的(拟合的)Keras 模型稍后应该在其他步骤的管道中使用,所以我认为我必须将模型存储为全局变量,以便其他管道步骤可以使用它.对吗?

Now I would like to insert a Keras model as a first step into the pipeline. Parameters of the model should be optimized. The computed (fitted) Keras model should then be used later on in the pipeline by other steps, so I think I have to store the model as a global variable so that the other pipeline steps can use it. Is this right?

我知道 Keras 为 Scikit-Learn API 提供了一些包装器,但问题是这些包装器已经进行了分类/回归,但我只想计算 Keras 模型,而不是其他任何东西.

I know that Keras offers some wrappers for the Scikit-Learn API but the problem is that these wrappers already do classification / regression but I only want to compute the Keras model and nothing else.

如何做到这一点?

例如我有一个返回模型的方法:

For example I have a method which returns the model:

def create_model(file_path, argument2,...):
    ...
    return model

该方法需要一些固定参数,如文件路径等,但不需要 X 和 y(或可以忽略).需要优化模型的参数(层数等).

The method needs some fixed parameters like a file path etc. but X and y is not needed (or can be ignored). The parameters of the model should be optimized (number of layers etc.).

推荐答案

您需要先将 Keras 模型包装为 Scikit 学习模型,然后再照常进行.

You need to wrap your Keras model as a Scikit learn model first, and then just proceed as normal.

这是一个简单的例子(为简洁起见,我省略了导入)

Here's a quick example (I've omitted the imports for brevity)

这是一篇完整的博客文章,包含这个和许多其他示例:Scikit-learn Pipeline例子

Here is a full blog post with this one and many other examples: Scikit-learn Pipeline Examples

# create a function that returns a model, taking as parameters things you
# want to verify using cross-valdiation and model selection
def create_model(optimizer='adagrad',
                 kernel_initializer='glorot_uniform', 
                 dropout=0.2):
    model = Sequential()
    model.add(Dense(64,activation='relu',kernel_initializer=kernel_initializer))
    model.add(Dropout(dropout))
    model.add(Dense(1,activation='sigmoid',kernel_initializer=kernel_initializer))

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

    return model

# wrap the model using the function you created
clf = KerasRegressor(build_fn=create_model,verbose=0)

# just create the pipeline
pipeline = Pipeline([
    ('clf',clf)
])

pipeline.fit(X_train, y_train)

这篇关于如何将 Keras 模型插入 scikit-learn 管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆