In the last 20 chapters around 200 recipies, we have convered how to take raw data nad usem achine learning to create well-performing predictive models. However, for all our work to be worthwhile we eventually need to do something with our model, such as integrating it with an existing software application. To accomplish this goal, we need to be able to bot hsave our models after training and load them when they are needed by an application. This is the focus of the final chapter
You have trained a scikit-learn model and want to save it and load it elsewhere.
Save the model as a pickle file:
# load libraries
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
from sklearn.externals import joblib
# load data
iris = datasets.load_iris()
features = iris.data
target = iris.target
# create decision tree classifier object
classifier = RandomForestClassifier()
# train model
model = classifier.fit(features, target)
# save model as pickle file
joblib.dump(model, "model.pkl")
/Users/f00/anaconda/envs/machine_learning_cookbook/lib/python3.6/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release. from numpy.core.umath_tests import inner1d
['model.pkl']
Once the model is saved we can use scikit-learn in our destination application (e.g., web application) to load the model:
# load model from file
classifier = joblib.load("model.pkl")
And use it to make predictions
# create new observation
new_observation = [[ 5.2, 3.2, 1.1, 0.1]]
# predict obserrvation's class
classifier.predict(new_observation)
array([0])
The first step in using a model in production is to save that model as a file that can be loaded by another application or workflow. We can accomplish this by saving the model as a pickle file, a Python-specific data format. Specifically, to save the model we use joblib
, which is a library extending pickle for cases when we have large NumPy arrays--a common occurance for trained models in scikit-learn.
When saving scikit-learn models, be aware that saved models might not be compatible between versions of scikit-learn; therefore, it can be helpful to include the version of scikit-learn used in the model in the filename:
# import library
import sklearn
# get scikit-learn version
scikit_version = joblib.__version__
# save model as pickle file
joblib.dump(model, "model_(version).pkl".format(version=scikit_version))
['model_(version).pkl']
# load libraries
import numpy as np
from keras.datasets import imdb
from keras.preprocessing.text import Tokenizer
from keras import models
from keras import layers
from keras.models import load_model
# set random seed
np.random.seed(0)
# set the number of features we want
number_of_features = 1000
# load data and target vector from movie review data
(train_Data, train_target), (test_data, test_target) = imdb.load_data(num_words=number_of_features)
# convert movie review data to a one-hot encoded feature matrix
tokenizer = Tokenizer(num_words=number_of_features)
train_features = tokenizer.sequences_to_matrix(train_data, mode="binary")
test_features = tokenizer.sequences_to_matrix(test_data, mode="binary")
# start neural network
network = models.Sequential()
# add fully connected layer with ReLU activation function
network.add(layers.Dense(units=16, activation="relu", input_shape=(number_of_features,)))
# add fully connected layer with a sigmoid activation function
network.add(layers.Dense(units=1, activation="sigmoid"))
# compile neural network
network.compile(loss="binary_crossentropy", optimizer="rmsprop", metrics=["accuracy"])
# train neural network
history = network.fit(train_features, train_target, epochs=3, verbose=0, batch_size=100, validation_data=(test_features, test_target))
# save neural network
network.save("model.h5")
Using Theano backend.
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-1-ab685cfa2361> in <module>() 1 # load libraries 2 import numpy as np ----> 3 from keras.datasets import imdb 4 from keras.preprocessing.text import Tokenizer 5 from keras import models ~/anaconda/envs/machine_learning_cookbook/lib/python3.6/site-packages/keras/__init__.py in <module>() 1 from __future__ import absolute_import 2 ----> 3 from . import utils 4 from . import activations 5 from . import applications ~/anaconda/envs/machine_learning_cookbook/lib/python3.6/site-packages/keras/utils/__init__.py in <module>() 4 from . import data_utils 5 from . import io_utils ----> 6 from . import conv_utils 7 8 # Globally-importable utils. ~/anaconda/envs/machine_learning_cookbook/lib/python3.6/site-packages/keras/utils/conv_utils.py in <module>() 7 from six.moves import range 8 import numpy as np ----> 9 from .. import backend as K 10 11 ~/anaconda/envs/machine_learning_cookbook/lib/python3.6/site-packages/keras/backend/__init__.py in <module>() 84 elif _BACKEND == 'theano': 85 sys.stderr.write('Using Theano backend.\n') ---> 86 from .theano_backend import * 87 elif _BACKEND == 'tensorflow': 88 sys.stderr.write('Using TensorFlow backend.\n') ~/anaconda/envs/machine_learning_cookbook/lib/python3.6/site-packages/keras/backend/theano_backend.py in <module>() 5 from collections import defaultdict 6 from contextlib import contextmanager ----> 7 import theano 8 from theano import tensor as T 9 from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams ModuleNotFoundError: No module named 'theano'
We can then load the model either in another application or for additional training
# load neural network
network = load_model("model.h5")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-7-114bd8857354> in <module>() 1 # load neural network ----> 2 network = load_model("model.h5") NameError: name 'load_model' is not defined
Unlike scikit-learn, Keras does not recommend you save models using pickle. Instead, models are saved as an HDF5 file. The HDF5 file contains everything you need to not only load the model to make predicitons (i.e., achitecture and trained parameters), but also to restart training (i.e. loss and optimizer settings and the current state)