learn管线保存到磁盘上

learn管线保存到磁盘上

本文介绍了如何将带有keras回归器的scikit-learn管线保存到磁盘上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有kerasRegressor的scikit学习管道:

I have a scikit-learn pipline with kerasRegressor in it:

estimators = [
    ('standardize', StandardScaler()),
    ('mlp', KerasRegressor(build_fn=baseline_model, nb_epoch=5, batch_size=1000, verbose=1))
    ]
pipeline = Pipeline(estimators)

在训练了基础知识之后,我正尝试使用joblib将其保存到磁盘...

After, training the pipline, I am trying to save to disk using joblib...

joblib.dump(pipeline, filename , compress=9)

但是我遇到一个错误:

您如何将管道保存到磁盘?

How would you save the pipeline to disk?

推荐答案

我遇到了同样的问题,因为没有直接的方法可以做到这一点.这是一个对我有用的技巧.我将管道保存到两个文件中.第一个文件存储了sklearn管道的腌制对象,第二个文件用于存储Keras模型:

I struggled with the same problem as there are no direct ways to do this. Here is a hack which worked for me. I saved my pipeline into two files. The first file stored a pickled object of the sklearn pipeline and the second one was used to store the Keras model:

...
from keras.models import load_model
from sklearn.externals import joblib

...

pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('estimator', KerasRegressor(build_model))
])

pipeline.fit(X_train, y_train)

# Save the Keras model first:
pipeline.named_steps['estimator'].model.save('keras_model.h5')

# This hack allows us to save the sklearn pipeline:
pipeline.named_steps['estimator'].model = None

# Finally, save the pipeline:
joblib.dump(pipeline, 'sklearn_pipeline.pkl')

del pipeline

这是如何加载模型的方法:

And here is how the model could be loaded back:

# Load the pipeline first:
pipeline = joblib.load('sklearn_pipeline.pkl')

# Then, load the Keras model:
pipeline.named_steps['estimator'].model = load_model('keras_model.h5')

y_pred = pipeline.predict(X_test)

这篇关于如何将带有keras回归器的scikit-learn管线保存到磁盘上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:40