All right.
So in this lesson we will load our pre trained model.
This is gonna be the same model that we've saved in the previous lesson and we're now loaded into a
fresh notebook and use that model to make predictions on the amnesty test dataset.
If you head over to the lesson resources you should see a downloadable zip file and if you extract the
contents of this zip file you should see this Jupiter notebook right here which is going to be a stub
project and you should see to see as V files with some test data.
So after you've downloaded and extracted these files take these test files to see as V files and drag
them over into your amnesty folder just Adam right here.
Now this amnesty folder is a folder that you would have created for the previous module but if you don't
have it then just create a new folder and call it amnesty in all caps.
Once you've done that drag the stop project over into your projects folder.
Now you can go back into Jupiter notebook and you should see the stop project that I've given you right
here.
So just click on it and I'll open in a new window and what you should see here are a couple of markdown
cells that delineate the different sections of the stop project and I find a little bit of code here
already to help us load the test data.
We've used num pi extensively already to load the text files so I've just saved you the work and added
this code here.
The important thing to note about the stop project is that it expects to find these load underscore
x test see as V and load underscore why test dot CSP files in the end this sub folder and it has to
be spelled exactly like this.
And the file names also have to match this exactly.
Now that we're all set up how shall we get started.
Well to do anything useful in tensor flow we need two things.
First we need a graph.
And second we need a session.
The graph is something like this.
It's what holds onto all our tensor is in all the operations and represents how the data flows from
the inputs to the outputs so let's create a blank graph in our Jupiter notebook.
So right here we're just going to create a variable called Graph.
Although our case and we're going to set it equal to T F dot graph with some parentheses at the end.
And what this will do is it will create a placeholder for us a graph object placeholder.
And this at the moment is empty but soon it's going to hold onto the graph that we will load from our
model.
The next thing that we need is a session object and this session if you recall is what actually runs
our operations and tensor flow.
We can only really do calculations intensive flow when we run a session.
So let's create a session.
I'm going to call it says an all lowercase.
And once again it's TAF dot session with parentheses at the end.
Now this will create a session object but we actually should link this session to our graph.
How do we do that.
Well let's look at the documentation.
If you search for tensor flow session then you should be able to go right here to this link and of course
I want to put all of this into the lesson resources when you scroll down you'll see this section labeled
in it.
This is the code that will allow us to actually create the session object and then we see as the second
argument we can supply a graph by default.
This graph has the value none so it is indeed an optional parameter.
But here is how we can link the graph object that we've created to the session object so let's modify
this line of code right here so that it will read graph is equal to graph between the parentheses.
So this is the name of the parameter.
And here is the actual value.
This object right here that we're passing in.
Brilliant.
So now we have a session and a graph and the two are linked.
So the next step is actually loading our saved model.
So how do we load a model well let's pull up the documentation again for tensor flow.
If you google something like load model tensor flow then you should be taking to this page right here.
If we look at this section right here.
Loading a saved model in Python.
Then we see a little excerpt with this bit of code.
There we see that there's something called a load function and it seems to require three arguments a
session some sort of tag and an export directory.
Let's click on this link right here and pull up a more detailed explanation.
So when I click on it and navigate to this load function right here and there what I see is this full
name of the load function.
But just like in the last lesson with simple Save there were some aliases that make it compatible with
future versions of tensor flow.
So in order to get our code to work with the upcoming tensor flow version 2.0 we're going to use this
full name right here.
TMF dot com Pat dot v one dot saved underscore model dot load so let's import this load function in
our Jupiter notebook.
I want to head back in here and at the very very top I'm going to import the load function and that
means importing it from tensor flow dot compact dot V1 dot saved underscore model and from there we
can import load.
Now what I can do is maybe run all the cells in my notebook after you've run all the cells.
You can call your load function.
So I've made a little sexual note here.
Load the model and I've pasted in a little bit of the documentation as we can see in this example write
him we need three things.
The first is that session object.
So let's add that right here.
We'll say load open parentheses and then for the session we'll supply our session object.
The session object that we've created right here in this cell.
The second thing that we need to provide is some sort of tag.
In the example in the documentation they've provided this tag underscore constants dot training.
Now we know that we're not gonna be looking to train our model.
We're looking to serve our model instead.
So what's the tag for serving.
Well actually the really neat thing about having used these simple save function in the last lesson
is that it automatically adds a tag to the model and we can see here that it automatically uses the
serving tag which is exactly what we wanted the easiest way to get hold of this serving tag in our notebook
here is to import the tag constants so much scroll back up here where we're importing load.
And I'm going to tack on tag underscore constants to the import limit shift enter on the cell and then
I can come down here where I'm calling my load function and for the tags I'm going to simply set them
equal to tag underscore constants dot serving.
So we're going exactly with the same format as with the documentation right here.
The last thing we need to provide is this export directory.
If we go to the detailed documentation for this load function we can see that in the description the
export directory is simply the directory in which the saved model protocol buffer and variables to be
loaded are located.
All that means is that the load function needs to know where to find this PDB file and these variable
files.
So we created a sub director here called saved model where all these files were saved and that means
for the third argument in the load function we're simply going to say export underscored Diaw is equal
to single quotes saved model and all we have to do is spell that folder name exactly the same way as
we've gone in Windows Explorer or in Mac finder.
Now let's that shift enter on the cell and see if it works.
Brilliant.
So now that we've successfully loaded our model we can use it to make predictions.
But how do we do that.
How do we make a prediction using this model that we've just loaded for that we need to remember.
What are the inputs that we actually need to make a prediction.
And what are the outputs of a prediction.
We've talked a little bit about this in the previous lesson.
The inputs where our exits.
So I've got the graph for a model here and we can see here that the features namely the pixel values
of the image that we're going to predict are gonna be stored in this tensor right here.
We call this place holder capital X. So let's retrieve this tensor from the graph and we can do that
by saying capital X is equal to some creating a new Python variable here and then I'm going to store
a tensor that I'm going to grab from the graph.
So when I take our graph object and then use the function get tensor by name and within the parentheses
mean to supply single quotes and then I'm going to have to supply the name of the tensor.
And in this case it's capital X because that's what we've got write him but then I have to add a colon
and a zero.
Now that's the full name of the tensor that I might get from the graph.
Now this point you might ask why is there this colon and a zero at the end.
Why is this tacked on.
Because we don't see it intensively right.
Well the answer is that this has something to do with how tensor flow works.
If we look at 10 support what we actually see are a whole bunch of operations so labels is an operation
and if I expand my first layer then clicking on any of these things we see that these are all operations
and all of these operations are linked with arrows and the arrows are the actual tensor is the thing
to understand about tensor flow is that all of these operations have inputs and outputs.
Take this ad operation here in our first layer here we're simply just adding the biases to the weights
and what we get from this addition is a single output the sum.
Now say I want to access the sum of the weights and the biases in the first layer.
How would I do that.
What I would do is I would take the name of the operation.
So this is layer underscore one slash add and then if I wanted to get a hold of the sum in this line
of code here I would simply have graph don't get tensor by name and then in the single quotes I would
have layer underscore one slash add there's only one little twist again I'm going I have to add a colon
and add zero.
This will give me access to the actual sum of the addition.
So this will give me access to the values that are the result of adding two things together.
If I leave out this colon on zero then I get access to the operation itself to the ADD operation on
the other hand if I have the suffix colon zero.
Then I get access to the sum.
So this is a little bit of a quirk of tensor flow.
By adding this colon zero to an operation we get access to an end point of an operation.
So in this case this add operation and the reason we have colon zero is because there might be more
than one output in the case of addition of course.
If you add two things you only have one output but if you had some other operation that has more than
one output then these things are indexed.
The first one is at index 0.
The second one is in index 1 The third one is set in next to and so on.
So when it changes back to X colon 0.
So this is how we're going to get hold of our inputs to make our predictions.
So the next question is how do we actually access the predictions from our model and actually want to
put this over to you as a challenge.
Can you get hold of the tensor that will hold the models predictions from the graph store these predictions
in a variable called Y underscore pred.
I'll give you a few seconds to pause the video before I show you the solution.
Ready.
Here's the solution.
Now if you recall the output of our model was this operation right here.
It had the name accuracy underscore calc slash prediction.
So what I want to do is I'm going to copy this name right here and I'm going to create this variable
by underscore pred and I want to use the same code as above graph.
Don't get tensor by name and then as an argument I'm going to provide in single quotes the name of the
operation and then a colon and then a zero.
This line of code will store the tensor that holds the actual predictions in this variable right here.
So now we have everything we need the inputs and the outputs to make predictions using our pre trained
model so the next step is making predictions on our testing dataset.
How do we do that.
Well any time we actually want to use tensor float to make a calculation or give us an output of some
sort.
Then we have to run a session and we can run our session by writing says Dot run.
This takes our session object and we'll call the run functional.
And it will give us some sort of output usually right.
So I'm going to store this output in a python variable called prediction and now I have to provide some
arguments to this run function.
Let's take a look at the documentation again for this session.
Here we've got the run function and it's got a number of arguments that it can take.
Now I'm not interested in the options and the metadata I'm only interested in these two the fetches
and the feed dictionary.
So what are the fetches in the feed dictionary.
Well the fetches are the values that we want to fetch from the graph.
Here we can specify the kind of output that we want from the session.
In our case we want the predictions.
Right.
Now what about the feed dictionary.
Well the feed dictionary is the data that the prediction is based on.
So once again I'd like to pose this over to you as a challenge.
Can you figure out how to call this wrong function and supply the values to the fetches and the feed
dictionary that will give us the predictions for our model.
Remember the goal is to predict the handwritten digits that are stored inside X on the score test.
I'll give you a few seconds to pause the video before I show you the solution
ready.
Here's the solution.
So for the finches I want to set those equal to while the output that we're after we're after Y underscore
pret the output right here and for the feed dictionary we're going to have to provide X on the score
test.
So this has to be provided in the form of a python dictionary with a name and a value.
The name is gonna be capital X..
And the value is gonna be X on a score test and that's it.
So let's execute these cells and check if it worked first then when execute this cell right here.
And second I want to execute this cell right here where I'm running my session.
Let's see what the actual values are for the first couple of labels in the y underscored test data set.
So why underscore test and then it's square brackets.
Colon five so that we see that the first digit is seven.
The second is a two.
The third one is a one.
And so on now let's take a look at the predictions.
The predictions that are the result of running our session we can pull up like so just like the cell
above and we can see that the output seems to match.
And that's really encouraging right.
This would suggest that we've successfully loaded our model and we've successfully made some predictions
on X underscore test that match the correct values.
But just because the first five results match doesn't mean that they all match.
Right.
So as a challenge can you calculate what percent of the test data set we predicted correctly in this
notebook.
There's a number of ways you can do this but I'll give you a few seconds to pause the video before I
show you a sample solution.
All right.
So here's how I would do it.
I would store the number of correct predictions in a variable something like an hour on a score.
Correct.
And I'm going to set this equal to while I want a check for quality using num PIs equal function.
And here I can check whether two things are equal namely the prediction and why on a score test.
So let's have a look at what this looks like here.
I get an array of billions so each entry that's equal to true has a value equal to one and each entry.
That's equal to False has a value equal to zero.
So what I can do then it's actually just some them all up.
Right.
This will give us the number of correct predictions.
And in order to calculate the percentage what I need to do is divide the total number of correct predictions
by the number of entries.
So in our case we've got 10000 entries in this dataset so I could either divide it by ten thousand or
I could just divided by the length of this array which is alien parentheses prediction and what I get
in this case is ninety seven point eight six percent.
And that's it there's just one last thing to do and that's a little bit of cleanup because at the moment
our tensor flow session is actually still running.
It's still open.
So we can take our session object and close it and we can take our graph and just reset the default
graph and that's it all done at this point.
We've successfully saved and loaded a saved model using tensor flow and we've also verified that our
saved model still works and can make accurate predictions.
The next step is converting our tensor flow model into something that is able to run in the browser
and for that we need to convert our tensor flow model into tensor flow.
Yes.
And that's exactly what we're going to do in the next lesson.
I'll see you there.
Take care.