All right.
So in this lesson we will save and export our machine learning model.
Why is this important.
Why bother saving a model.
Well first off this can massively cut down on trading times if your model takes 20 minutes to train.
Then saving the model at the end of training and loading it next time will save you 20 minutes.
But if your model takes 10000 gp hours to train then saving is even more important because it allows
you to pick up where you left off.
But even we had this problem in our Jupiter notebooks every time we closed our computer or restarted.
Then we had to rerun all the cells in our notebook and retrain our models from scratch.
So saving models to save on training time is really really helpful.
But in our case we want to take it one step further still.
We want to be able to save our models that we can actually run it on the web.
Right.
We want our model to be able to protect live data from our Web site.
And that means we need a way of being able to export the entire model.
We got to have a train model running on this Web site so that we can make these predictions and intensive
flow parlance this is called serving the model.
So let's dive right into Jupiter and take the first step namely saving our model to fire up Jupiter.
If you're on Windows simply open the anaconda prompt and then type Jupiter notebook and hit enter if
you're on Mac.
You can go here and bring up terminal and type the very same command Jupiter notebook.
And what this should do it should fire up Jupiter and drop you off where you can see your folders.
Now I'm going to go to documents and then e-mail projects and here I've got the notebook that I've worked
on in the previous module.
Number eleven neural networks hyphen TAF handwriting recognition.
This is the notebook that we created to predict the handwritten digits for the endless dataset.
If you've got this notebook right here simply go to duplicate right here click duplicate and then what
you can do is simply select the copy and click rename and what we're gonna do is gonna give this notebook
and you name.
We're gonna call it 12th T F saved model.
Export now if you don't have the code for the previous module you can also download this notebook from
the lesson resources and simply upload it to Jupiter right here.
Then simply open the notebook and then go to sell and click Run all.
And while Jupiter's doing its thing let's have a look on google and see how we can save our tensor flow
models.
Let's pull up the documentation from tensor flow.
The first link should take you to a page like this.
This is a really long and detailed article and it's actually not all that clear.
What we need to do to make sense of this document we need to understand a little bit more about what
our options are.
So let me show you a bit of a high level overview.
There are actually two different types of saving intensive flow.
The first has to do with checkpoints and the second has to do with something called a saved model.
But what's the difference.
Well these two types of saving are actually used for very different purposes.
So the checkpoint is used to save variables what variables.
Well things like connection weights that you've estimated in your neural network.
The saved model on the other hand is also used to save variables but it also saves the entire graph
and the metadata about the graph.
This means Yes it saves the connection weights but it also saves the structure for all the calculations
that take place and even the names of these calculations.
What are the checkpoints useful for.
Well the checkpoints are primarily used during training say while you're running your loop and iterating
over those epochs.
Perhaps you want to save your progress every 10000 steps or so.
This allows you to resume training from a checkpoint.
If things go wrong a saved model on the other hand is primarily used after training has been completed
and since her goal is to create our website our math garden and have our model running there we're gonna
go down the saved model route because the saved model is suitable for serving the saved model.
Not only gives us the variables but it also gives us the graph and the metadata.
So now that we have a little bit of an overview over the two types of saving let's go back to the documentation
and have a look what it says there.
If I look here on the right hand side I see that there is actually a section on how to build and load
a saved model.
And if I go there then in the first section I can see that the easiest way to create a saved model is
to use a function called simple Save this function even has the word simple in it because it just needs
for things to accomplish this goal.
And those four things are a session an export directory some inputs and some outputs.
So let's talk about these in turn.
What's the session.
Well the session that we have to provide is just a session object that we're using to train our model.
And you can see the session object right here in our Jupiter notebook so what about the second thing
here.
The export directory.
Well let's pull up some more details about this function by clicking here.
You can also find all of these links in the lesson resources scrolling down we can see that the second
argument the export directory is simply the path to which the saved model is stored.
Saving a model will actually create a number of files.
So what we're gonna do is we're just going to provide a folder name here.
Now what about the inputs and the outputs to understand what these are.
Let's first pull up 10 support somewhere or create a new window in my terminal and there and when a
fire up 10 support by typing tensor board dash dash log here and point tensor board to where my log
files are being saved the simplest way to get this project path is simply to open up the email project
folder in Windows Explorer or on Mac finder and just drag this into your window.
Now hit enter and then copy this address here.
And navigate to it in your browser.
Now click on graphs and make sure you've got your most recent run selected.
If you take a look at this graph can you spot the inputs.
Or can you spot what the inputs should be to our simple save function.
The input in fact is this little X at the bottom.
This X here represents our features.
This tensor is going to hold all the pixel values for the hand drawn digits it's these inputs that feed
into our old neural network.
Starting with the input layer going through the dropout layer then our second hidden layer and then
finally our output layer.
So where are these inputs in our Jupiter notebook.
Well they're right at the top where we created our tensor flow placeholder.
It's this tensor that holds onto the input data for our neural network but what about the output of
our neural network.
Remember the output of the neural network that we're after.
It's gonna be our prediction.
Let's take another look at transport.
Where would we find the output well the first place I would look is in our third hidden layer.
We've even named this layer out.
So is this layer the output that we should save.
Well close but not quite.
This is the last layer of the neural network which is true but what we're actually after is the prediction
of our model.
If we take a look back into our Jupiter notebook we can see that our last layer our output layer is
defined here.
We're calling this function setup layer and what we're doing there is we're multiplying our weights
and we're adding our biases.
And then for the activation function we're specifying these soft Max activation function.
The soft Max activation function if you remember will give us a probability for each of the different
handwritten digits it might say something like this a 5 percent chance that this digit is a one or a
20 percent chance that it is a two and a 30 percent chance that it's a 3.
And so on but even this is actually not the calculation that will give us the final prediction because
the final prediction will be the largest of these probabilities from the soft max function.
So where do we get that.
Where did we get the largest probability from the output.
Well we're actually got the largest probability from our output right here where we're calculating the
accuracy of our model and this line of code we're checking if the true value the actual value of the
label is equal to the predicted value.
This here is our prediction and we're checking for equality between our prediction and the actual label.
What we can actually do is modify this code a little bit to make this even more explicit.
So I'm actually going to create a little variable here called Model underscore prediction and I want
to use it to split out that line of code below.
I'm going to set this equal to TAF dot org Max and that's actually just going to be equal to this quantity
right here.
Right now what I can do is I can replace this with model underscore prediction.
I think that makes it a little bit more explicit as to what calculation in our tensor flow graph actually
corresponds to the final prediction of our model.
And just to make it show up on our graph very very explicitly which calculation is actually the prediction
I'm going to give this particular calculation a name and when I do that by providing this extra argument
here name is equal to single quotes prediction all lowercase.
Now what I can do is come back up here where I'm setting up my tensor flow graph and go to sell run
all below.
And what this will do is it'll refresh the graph in 10 support allowing me to press refresh here and
in selecting my latest run this one right here if I select my accuracy calculation here and just double
click on it to expand it then what I see is my prediction right here.
The prediction of my model with the name prediction all lowercase this year is the output that we're
gonna provide to these simple save function.
Now the really nice thing about tensor board is that it makes the full name of this calculation very
very clear.
We can see that the full name is accuracy underscore calc slash prediction.
We're going to need this later on.
So what I want to do is I want to click on it and I want to copy it and I encourage you to do the same.
Copy this name verbatim so that you can use it later on.
Now if you look back at the documentation for the simple save function we can make more sense of the
description for the inputs and the outputs both the inputs and the outputs need to be a dictionary mapping
the string input names to tenses the string input name is gonna be the name of our calculation and the
tensor is going to be a model underscore prediction.
So now let's write the code to save our model insert a cell after you finish training.
Make this a markdown cell and just write saving the model.
And here what we're gonna do is we're in a create a variable called outputs.
We're gonna set that equal to a dictionary.
Right.
So curly braces and it's going to be a dictionary mapping the String output names.
So this named right here to a tensor so in our case the name is the name of the calculation and the
tensor it's gonna be model underscore prediction creating the dictionary for the inputs.
Luckily it's much much easier there.
The name is just capital X..
And our tensor is also just capital X..
So I'll create a variable here called inputs.
And this is gonna be a dictionary mapping the string name capital X to our tensor.
Also capital X. Now the spelling here is very very important.
These names they have to match the names that you see intensive.
Exactly.
So now let's call the simple save function.
How would we do that.
Well simple save is the short name but what's the full name the long name.
Well in this case it be TAF dot saved on a scroll model.
Dot simple safe right.
And normally I would use this long name but if I scroll down a little bit then I get this little box
here.
This little red box warning me that there's some changes coming up in tensor flow tensor flow like every
other bit of software is constantly being updated and new versions are always coming out to make sure
our code doesn't break in the future.
What we have to do is we have to access the simple save function this way.
We have to use this long name for it.
TMF dot com Pat dot V1 dot saved underscore a model that's simple safe.
So what I'm gonna do is I'm going to copy the long name right here and then in Jupiter I want a pasted
in and at the end I'm going to provide those four inputs and provide our session.
I want to provide an export directory.
Someone is gonna call this saved model in single quotes and then I'm going to provide the inputs and
outputs the two dictionaries that we've created on the two lines above.
And that's it.
Now it's time to run our code.
But before we do let's bring up our windows explorer or our Mac finder here side by side with our Jupiter
notebook.
That way we can see exactly how this folder is going to be created and what files are going to be saved
for a model.
So when I just go here to Colonel restart and run all.
And then what I'm gonna do is going to keep an eye on this directory here.
The reason that I'm actually running all my cells again is because in order to save our model we actually
have to have a working running session.
My previous session was actually closed.
So that's why I reran the cells in my notebook.
And when all goes well you should see your saved model directory appear here and inside you have a PDB
file and you have some more files for your variables so congratulations you've not successfully saved
your train model including all of its variables.
And the graph itself in the next lesson I want to show you how to load this model in a fresh notebook
and be able to make predictions using this model right away.
For all of that and more I'll see you in the next lesson.
Take care.