[MUSIC PLAYING]
YUFENG GUO: Did you know that you can convert a Keras model
to a TensorFlow estimator?
It'll give you a whole host of options around distributed
training and scaling.
Stay tuned to find out how.
Welcome to "Cloud AI Adventures,"
where we explore the art, science,
and tools of machine learning.
My name is Yufeng Guo.
And on this episode, we are going
to prepare a Keras model for running at scale by converting
it to a TensorFlow estimator.
So we have a Keras model--
easy to define, clear to read, and friendly to maintain.
But it doesn't do so good with scaling up to larger datasets
or running across many machines.
Luckily, Keras and TensorFlow have
some fantastic interoperability features.
What we want to do is convert our Keras model
to a TensorFlow estimator, which comes with the distributed
training built in.
That's our ticket to solving our scaling challenges.
Plus it makes it easy to do model serving
once our training is complete.
The function that we are interested in
is called model_to_estimator.
And I've taken the notebook we started
with in the previous episode and updated it with new code
to convert our Keras model to a TensorFlow estimator.
We'll also take a look at how to export your Keras
model as is once training is complete, along
with the TensorFlow model.
Let's switch over now to the Kaggle notebook and dive
into the code.
So here we are back in our Kaggle kernel
with our Fashion MNIST dataset.
This code should look eminently familiar.
We're going to load up NumPy, pandas, TensorFlow,
and that plotlib.
And we're going to pull in our dataset
with pandas, reading those CSV files,
and take a peek at the first couple of rows.
We can see that the first column is our label column
and all our pixels are all the rest
of the columns to the right.
I've got this handy preprocessing function,
which takes our data frame and pulls out
the values for the features and the labels.
And then we can save them directly
into the variables, like train features, train labels,
test features, and test labels.
Then we'll go ahead and do our one-hot encoding.
This is exactly the same as we saw in our previous episode.
We're going to convert those labels, which were numbers,
to labels that are arrays of length 10 for our 10
different possible labels.
Then we'll run our train.
We've got our training parameters
and we'll create our Keras model.
This is exactly the same code that we saw to create our Keras
model before.
And once we have that created, we can run model.fit--
identical to what we saw in the previous episode.
Once we have our model fit, we can go ahead and test it.
And now, we get to the interesting stuff--
our conversion.
How do we convert our Keras model to TensorFlow estimator?
We'll use model_to_estimator.
This function takes a couple of different arguments.
But in our case, we're just going
to pass it the one pointer to our model--
to the argument Keras model.
And that will convert our Keras model
into a TensorFlow estimator.
Once we have our TensorFlow estimator in hand,
we can do everything we would normally
do with a TensorFlow estimator.
In this case, I've chosen to go ahead and train it.
We should train our estimator and see
what we can achieve in terms of accuracy and performance.
So if we want to do that, first we
need to think about how do we architect our input function.
And the big question there is for our features,
we need a label for those features.
What do we call it?
When we're coming from Keras, we don't really get to name it
ourselves.
We have to run model.input_names.
And this is the input names of the Keras model, not
the TensorFlow model.
Those names are copied over.
And so we can look at our Keras model
and see that, in this case, it's called dense_3_input.
And when we pull it out, we'll use that variable input
name, which points to dense_3_input,
as our key for our features.
And so once we have that set up, we
can do pretty much the same as what
we're used to seeing with TensorFlow estimator training.
We'll have our feature columns.
In this case, I'm going to rename our tf classifier
as just classifier.
And now, we can train.
And since we used the correct input names,
we can do this without any errors.
If you don't use the right input names,
you will get errors, so be sure to get that input name out.
Now, we can run evaluate and set up
that evaluate_input function.
You'll notice that we'll need to use that input name once
again here, so a really good idea to save that off
to a variable.
Another thing to notice here is that we used the one-hot labels
in the TensorFlow training and evaluation,
just like we did in Keras.
Now when we ran this model in pure TensorFlow
in the past with Fashion MNIST, we
were able to use the not-one-hot encoded labels.
We could just use the labels that were 5, and 7, and 9.
In this case, because we took the model directly from Keras
and moved it to TensorFlow, that conversion
also resulted in the model expecting
one-hot encoded labels.
And so you'll need to make sure you use the same labels
as you did in Keras.
In practice, this ends up being really convenient because you
can just use the same variables in the same arrays
that you used before.
Now, we can see that the evaluation is completed.
In this case, we got to 86%.
And in Keras, I think we got something similar.
We can see here-- yeah, 85%.
The accuracies will vary depending
on a number of different factors ranging
from the random initialization state, how
my computer is feeling on a given day, things like that.
Now that we have our models trained in both Keras
and TensorFlow, let's take a brief look
at how we would export these models out
into the file system.
Keras models are very straightforward to export.
You just call model.save and give it a name,
and it will export it in the HDF5 format.
On the TensorFlow side, exporting our model
requires the use of the export_savedmodel function.
This function takes two arguments.
One is a path to export it to.
And secondly, a serving input receiver function,
which for short is often just called a serving function.
TensorFlow has a utility to help you
build that serving function.
And that takes, as an argument, this mapping
of the input shape and type.
This helps the model know what kind of inputs to expect.
So once we run our export_savedmodel
on our TensorFlow model, we can see where it was exported to.
And we can see that TensorFlow exports models
as a file and a folder called variables.
So for convenience, I also like to zip it up or tar it
up so that it's easy to download.
And then on the other side, once you download it,
you can unzip it with tar as well.
So that's kind of what's involved
with taking a Keras model.
We've taken that, converted it to a TensorFlow estimator,
and then exported that TensorFlow estimator.
We've managed to do all of this without actually
creating a TensorFlow estimator from scratch.
We've gone all the way from taking a Keras model,
training it, converting it to a TensorFlow estimator,
training that, and then exporting the two of them.
And we'll be able to use those exported files
in future episodes to do more interesting things.
Getting distributed training using TensorFlow estimators
is an easy one-line adjustment to your Keras model.
So now, you have the best of both worlds--
easy to read Keras model creation syntax along
with distributed training via TensorFlow estimators.
Thanks for watching this episode of "Cloud AI Adventures."
And if you enjoyed it, please like it
and subscribe to get all the latest episodes right
when they come out.
For now, remember that all you need
to get distributed training working with your Keras model
is to use model_to_estimator.