YUFENG GUO: Welcome to "AI Adventures,"
where we explore the art, science,
and tools of machine learning.
My name is Yufeng Guo, and on this episode,
we'll train a scikit-learn model and deploy it
on Google Cloud ML Engine.
Take this information and use it to set up your own deployment
pipeline and you'll never need to worry
about how to deploy and scale your models again.
Say you have some data, you've trained up
on a scikit-learn model, and now you
want to set up a prediction server.
Let's see how you'd do that based on the code
that we had in a previous episode about zoo animals.
First, we got to export the model,
so we'll use the joblib library from sklearn.externals.
We can use joblib.dump to export the model to a file.
Once we commit and run this, we'll
be able to retrieve the output from our Kaggle kernel.
And here we are ready for download.
With our trained scikit-learn model in hand,
we are ready to head over to Google Cloud Machine Learning
Engine and load up the model to serve predictions.
That's right, you can get all the auto scaling secured
REST API goodness not only for TensorFlow, but also
for scikit-learn.
Cloud ML Engine has traditionally worked only
for TensorFlow, but now it also supports scikit-learn as well
as XGBoost.
This enables you to easily transition back and forth
between scikit-learn and TensorFlow and XGBoost
without having to rework your plumbing or architecture.
The first step to getting our model in the cloud
is to upload the joblib file to Google Cloud Storage.
It is required that the name of the file
be literally model.joblib.
So be sure to stick it inside a folder
with a name you'll remember, otherwise you'll
end up with more models later and they'll all be the name.
So we'll use that folder name to locate the model later.
Now let's create our model and version,
and we got to make sure we specify that we're loading up
a scikit-learn model and select the runtime version of Cloud ML
Engine as well as the version of Python
that we used to export this model.
Since we were running on Kaggle, that's Python 3.
And well, that's really all there is to it.
We now have a scikit-learn model serving in the cloud.
Now, of course, a scalable model is no use to anyone
without being able to call those predictions.
So let's take a simple look at how that is done.
I've gone ahead and pulled one sample row
from our data whose answer should be category 4.
Now, what we can do is present that data
to Cloud ML Engine as just a simple array encoded as a JSON
file.
Here I'm taking the test features data frame,
and then we'll extract row 10 from it,
and then we'll call .values to get the underlying numpy array.
Unfortunately, numpy arrays don't
print with commas between their values.
So we really want the commas.
So we then wrapped it and turned the numpy array into a Python
list to be printed out.
So we have our list, we have it saved to an input file,
and now we can call the prediction REST API
for our scikit-learn model.
G Cloud has a built in utility to do that for us,
and we're expecting a response of 4, and yes, we got it.
You can follow these steps in this video
to deploy your scikit-learn model to production,
or even step it up by turning it into an entire automated
pipeline so that, every time you make a new model,
it can get deployed out so you can test it.
Thanks for watching this episode of "Cloud AI Adventures."
And if you enjoyed it, please hit the Like button
and subscribe to get all the latest episodes right
when they come out.
For now, head on over to Cloud ML Engine
and upload your scikit-learn model
to get auto scaling predictions right at your fingertips.