Machine Learning Model Deployment (Part 1)
Machine Learning Model Deployment (Part 1)
Serialize and Deserialize Your model
I am going to discuss the foundations of ML model deployment. Besides scrapping and wrangling data, performing Exploratory Data Analysis (EAD), Sanity checks, and data visualization; understanding algorithms and libraries to create ML models, and performing model optimization, we must consider model deployment.
If you have taken courses in ML, you will learn about algorithms and models, but you may not have been exposed to model deployment techniques because this isn't exactly part of data science or machine learning.
However, once you have developed your ML model, how do you share it with other teams and put it into production? To deploy your model either to other teams or to a production platform, you will need to know a few basics:
Serialization
Containerization
Kubernetes
Machine Learning Model deployment is the process of making your trained model available to other stakeholders (i.e. users, servers, applications, etc) without requiring any re-training of data and maintaining the same original performance.
In this first part of the model deployment, we will explain how to save and load your model for later reuse.
Serialize your model
Model Serialization is a fancy name for saving your model for reuse. It is also called pickling because we can use a Python library called pickle.
If you do not serialize your model, you won't be able to reuse it after you turn off your computer or close your Jupyter session without training it again. Can you imagine having to retrain your model every time you want to use it to make predictions on new data?
To easily serialize your model to a file, we can use pickle or joblib.
Use pickle.dump ("pickling")
Usage: import pickle
pickle.dump(model, open('filename','wb'))
where:
model: your machine learning model
filename: The filename to store the model. You can specify any name and extension, i.e.: "mymodel.ml','aModel.ai', 'example.sav', etc.
wb: write in binary mode
Example: pickle.dump(model, open('mymodel.ml', 'wb'))
Use joblib
Install joblib: pip install joblib
Usage: import joblib
joblib.dump(model, 'filename')
where:
model: your machine learning model
filename: The filename to store the model. You can specify any name and extension, i.e.: "mymodel.mlm','aModel.ai', 'example.sav', etc.
wb: write in binary mode
Example: joblib.dump(model, 'yourmodel.mlm')
Deserialize your model
Model Deserialization is nothing else than Loading the model into memory.
Use pickle ("unpickling")
Usage: pickle.load(open('filename','rb'))
where:
filename: The filename where you stored your model.
rb: read in binary mode
Example: pickle.dump('mymodel.ml', 'rb')
Use joblib
Usage: joblib.load('filename')
where:
filename: The filename where you stored your model.
Example: joblib.load('yourmodel.mlm')
Jupyter Notebook
Summary
Use pickle.dump or joblib.dump to serialize your model or to save any data.
Use pickle.load or joblib.load to deserialize your model or any data.
Now you know how to serialize (save) and deserialize (load) the model.
References