Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save a scikit-learn pipline with keras regressor inside to disk?

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)

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

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

But I am getting an error:

RuntimeError: maximum recursion depth exceeded

How would you save the pipeline to disk?

like image 894
Dror Hilman Avatar asked Jun 23 '16 06:06

Dror Hilman


People also ask

Can you save a Sklearn model?

You can save and load the model using the pickle operation to serialize your machine learning algorithms and save the serialized format to a file. Hope it helps!

What is SciKeras?

The goal of scikeras is to make it possible to use Keras/TensorFlow with sklearn. This is achieved by providing a wrapper around Keras that has an Scikit-Learn interface. SciKeras is the successor to tf. keras. wrappers.


2 Answers

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)
like image 137
constt Avatar answered Oct 02 '22 22:10

constt


Keras is not compatible with pickle out of the box. You can fix it if you are willing to monkey patch: https://github.com/tensorflow/tensorflow/pull/39609#issuecomment-683370566.

You can also use the SciKeras library which does this for you and is a drop in replacement for KerasClassifier: https://github.com/adriangb/scikeras

Disclosure: I am the author of SciKeras as well as that PR.

like image 41
LoveToCode Avatar answered Oct 02 '22 21:10

LoveToCode