简体   繁体   中英

Problem loading ML model saved using joblib/pickle

I saved a jupyter notebook .pynb file to .pickle format using joblib.

My ML model is built using pandas, numpy and the statsmodels python library.

I saved the fitted model to a variable called fitted_model and here is how I used joblib:

from sklearn.externals import joblib
# Save RL_Model to file in the current working directory

joblib_file = "joblib_RL_Model.pkl"  
joblib.dump(fitted_model, joblib_file)

I get this as output:

['joblib_RL_Model.pkl']

But when I try to load from file, in a new jupyter notebook, using:

# Load from file
joblib_file = "joblib_RL_Model.pkl"  
joblib_LR_model = joblib.load(joblib_file)
joblib_LR_model

I only get this back: <statsmodels.tsa.holtwinters.HoltWintersResultsWrapper at 0xa1a8a0ba8>

and no model. I was expecting to see the model load there and see the graph outputs as per original notebook.

Use with open, it is better because, it automatically open and close file. Also with proper mode.

with open('joblib_RL_Model.pkl', 'wb') as f:
    pickle.dump(fitted_model, f)

                                    
with open('joblib_RL_Model.pkl', 'rb') as f:
    joblib_LR_model = pickle.load(f)

And my implementation in Colab is here . Check it.

you can use more quantifiable package which is pickle default package for python to save models

you can use the the following function for the saving of ML Models

import pickle

def save_model(model):
    pickle.dump(model, open("model.pkl", "wb"))

template for function would be

import pickle

def save_model(model):
    pickle.dump(model, open(PATH_AND_FILE_NAME_TO_BE_SAVED, "wb"))

to load the model when saved it from pickle library you can follow the following function

def load_model(path):
    return pickle.load(open(path, 'rb'))
  • Where path is the path and name to file where model is saved to.

Note:

  • This would only work for basic ML Models and PyTorch Models, it would not work for Tensorflow based models where you need to use
model.save(PATH_TO_MODEL_AND_NAME)

where model is of type tensorflow.keras.models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM