In the last 20 chapters and around 200 recipes, we have covered how to take raw data and use machine 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 both save our models after training and load them when they are needed by an application. That is the focus of our final chapter together.
Save the model as a pickle file:
# Load librariesfromsklearn.ensembleimportRandomForestClassifierfromsklearnimportdatasetsfromsklearn.externalsimportjoblib# Load datairis=datasets.load_iris()features=iris.datatarget=iris.target# Create decision tree classifer objectclassifer=RandomForestClassifier()# Train modelmodel=classifer.fit(features,target)# Save model as pickle filejoblib.dump(model,"model.pkl")
['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 fileclassifer=joblib.load("model.pkl")
And use it make predictions:
# Create new observationnew_observation=[[5.2,3.2,1.1,0.1]]# Predict observation's classclassifer.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 occurrence 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 libraryimportsklearn# Get scikit-learn versionscikit_version=joblib.__version__# Save model as pickle filejoblib.dump(model,"model_{version}.pkl".format(version=scikit_version))
['model_0.11.pkl']
Save the model as HDF5:
# Load librariesimportnumpyasnpfromkeras.datasetsimportimdbfromkeras.preprocessing.textimportTokenizerfromkerasimportmodelsfromkerasimportlayersfromkeras.modelsimportload_model# Set random seednp.random.seed(0)# Set the number of features we wantnumber_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 matrixtokenizer=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 networknetwork=models.Sequential()# Add fully connected layer with a ReLU activation functionnetwork.add(layers.Dense(units=16,activation="relu",input_shape=(number_of_features,)))# Add fully connected layer with a sigmoid activation functionnetwork.add(layers.Dense(units=1,activation="sigmoid"))# Compile neural networknetwork.compile(loss="binary_crossentropy",# Cross-entropyoptimizer="rmsprop",# Root Mean Square Propagationmetrics=["accuracy"])# Accuracy performance metric# Train neural networkhistory=network.fit(train_features,# Featurestrain_target,# Target vectorepochs=3,# Number of epochsverbose=0,# No outputbatch_size=100,# Number of observations per batchvalidation_data=(test_features,test_target))# Test data# Save neural networknetwork.save("model.h5")
Using TensorFlow backend.
We can then load the model either in another application or for additional training:
# Load neural networknetwork=load_model("model.h5")
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 predictions (i.e., architecture and trained parameters), but also to restart training (i.e., loss and optimizer settings and the current state).