简体   繁体   中英

Machine learning models persistence options

Any suggestions/best practices for persisting and re-using trained machine learning models ? I'm developing models in Python or R. Then these models must be used in production workflow for scoring (where R is not available). For example there could be a logistic regression model trained in R. Now new observations need to be scored against this model. The scoring engine must be fast and scalable. I've thought of following

  1. PMML ( http://en.wikipedia.org/wiki/Predictive_Model_Markup_Language ). It is easy to convert most of the models developed in R to pmml. However, I couldn't find a useful scoring engine for PMML models. For example, there is augustus ( https://code.google.com/p/augustus/ ) but it implements only 3-4 models yet.

  2. Serialize the models using pickle in Python and write the consumer in Python.

Any thoughts/suggestions on the right approach ?

Scikit-learn , a mature library in this field, uses pickle for its persistence of models . I gather you are writing your own functions to train the model, but looking at the established libraries can tell you about best practices.

On the other hand, JSON can be read from many languages. That is its main advantage. If your plan to serve model results from another language, and your models are fairly simple Python objects, then serializing them to JSON should be pretty easy.

You can save and load the model using pickle in python as follows,

import pickle
s = pickle.dumps(clf)
clf2 = pickle.loads(s)

Other way is to use joblib which is more efficient on objects that carry large numpy arrays internally as is often the case for fitted scikit-learn estimators.

from sklearn.externals import joblib
joblib.dump(clf, 'filename.pkl')
clf = joblib.load('filename.pkl') 

This model can then be deployed in production as RESTful APIs.

You may use messagepack. It uses JSON like format to store the model. It's fast and takes less memory. https://github.com/muayyad-alsadi/sklearn_msgpack

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