How to save Scikit-Learn-Keras Model into a Persistence File (pickle/hd5/json/yaml)

后端 未结 5 844
小鲜肉
小鲜肉 2020-12-13 19:23

I have the following code, using Keras Scikit-Learn Wrapper:

from keras.models import Sequential
from sklearn import datasets
from keras.layers import Dense
         


        
5条回答
  •  庸人自扰
    2020-12-13 19:51

    In case your keras wrapper model is in a scikit pipeline, you save steps in the pipeline separately.

    import joblib
    from sklearn.pipeline import Pipeline
    from tensorflow import keras
    
    # pass the create_cnn_model function into wrapper
    cnn_model = keras.wrappers.scikit_learn.KerasClassifier(build_fn=create_cnn_model)
    
    # create pipeline
    cnn_model_pipeline_estimator = Pipeline([
        ('preprocessing_pipeline', pipeline_estimator),
        ('clf', cnn_model)
    ])
    
    # train model
    final_model = cnn_model_pipeline_estimator.fit(
    X, y, clf__batch_size=32, clf__epochs=15)
    
    # collect the preprocessing pipeline & model seperately
    pipeline_estimator = final_model.named_steps['preprocessing_pipeline']
    clf = final_model.named_steps['clf']
    
    # store pipeline and model seperately
    joblib.dump(pipeline_estimator, open('path/to/pipeline.pkl', 'wb'))
    clf.model.save('path/to/model.h5')
    
    # load pipeline and model
    pipeline_estimator = joblib.load('path/to/pipeline.pxl')
    model = keras.models.load_model('path/to/model.h5')
    
    new_example = [[...]]
    
    # transform new data with pipeline & use model for prediction
    transformed_data = pipeline_estimator.transform(new_example)
    prediction = model.predict(transformed_data)
    

提交回复
热议问题