In this lesson we're going to set up tents aboard in our Jupiter notebook.
And during this process we're going to get started working with summaries on tensor board and the file
writer.
The first thing that we're gonna do is under the constants let's set up a logging path.
So call this one logging underscore path and we'll set that equal to a string called tensor board underscore
and missed on a score digit underscore logs and we'll have a forward slash at the end.
Hit shift enter on that cell and then scroll down to the section where we've had loss optimization and
metrics.
Let's add another markdown cell here just above and we'll call this one tensor board setup.
And here what we'll do is we'll create the folders for tensor board so I'll add a common to folder for
tensor board and we'll do him as we'll pick a folder name so the folder on the screen name is gonna
be equal to maybe an F string and that string will read model 1 and then we'll add maybe the time right
the current time that we have just like we did in the last module.
If we scroll back up to our input statements we're going to add a single line here.
That's gonna read from time import as TR f time hit shift enter on that cell scroll back down to where
we were adding our folder name and let's call that function here.
S t r f time open parentheses.
Double quotes.
Percent H colon percent M double quotes what we'll do then is we'll join the root logging path with
the folder name so we'll call this directory is equal to who stop path dot join parentheses logging
underscore the path comma folder name and kind that we've got our directory that we want to create.
We'll try to create the folder with O S dot make dears parentheses directory.
This is a review from last module and we'll catch an exception in case we have an OS error that is except
OS error as exception.
Colon next line we'll just print it out print out the exception exception don't SDR error so we'll get
the error as a string.
Suppose of some sort of code or no.
Finally will I had an LS block where we just say print successfully created directory use exclamation
mark add some attitude to our print statements.
Fantastic.
Let's hit shift enter on the cell and at that point it should have credit to folders for you.
This root folder write him and this sub folder right here for this model.
Now we've talked a lot about how when tensor flow runs at session it can do a lot of calculations the
way that 10 support gets hold of these calculations is through something called a summary the summaries
are essentially what allows tensor board to generate these pretty charts and he's pretty graphs that
we saw in the previous module.
The thing that you'll also remember is that tensor board was looking towards these files to read this
information and create these charts in the first place.
Writing these files requires a component called a file writer.
So the file writer will take the summaries and write them to these files.
So let's create a first summary and that summary is gonna be our accuracy metric so that a line of code
here that's going to read T F dot summary dot scalar because accuracy is just a number and in the parentheses
I'm going to say what the name is of the summary.
So I'm going to call this accuracy and then what the value is.
And this will be this accuracy metric here.
So now that I've created my summary I can hit shift enter on the cell and I can come down here.
We've created my session and here I'm going to add a markdown cell and that markdown cell is going to
read set up file write up and merge summaries.
I'm going to add a cell here and in that cell I'm going to do a couple of things I'm going to merge
all my summaries so I might have more than one laying around.
I've just got one at the moment but we're gonna add a few more in a second so I'm going to see merged.
Summary it's gonna hold on to TAF dot summary dot merge all so all my summaries all the things that
I want to read all the calculations that I want to keep hold of.
We're going to combine them going to merge them.
I want to store them in this thing called merged summary.
The next thing that I'll do is I'll create my file writer so my file to which I'll call train on a school
writer.
It's gonna be equal to TMF dot summary dot file writer and this file writer will need a directory to
work in.
It's gonna need to know where to save these files.
So I want these files to go in this directory right here that we're creating and I wanted to create
a sub directory here called train.
So I'll say directory plus single quotes forward slash train this directory is the same one that we've
used here.
Now that I've got my file writer I need to tell my file right home where to look for the calculations
to write down and I can do that with file write a dot add on a school graph open parentheses session
dot graph so what have I done here all of tensor flows calculations are in this thing called the graph
and the calculations are evaluated during a session for the file right to know which calculations to
save and write down to my disk I have to tell it which graph to use and I'm telling it here to use my
sessions graph.
So let me hit shift enter on the cell and I'm going to move this comment here maybe to a markdown cell.
So now I can come down to my training loop and make use of my file writer because my file writer will
write my summaries during the training.
What I'd like you to do is that every epoch.
I'd like it to create a summary for me and I'm going to do that by moving this line out of this loop
here and to my fetches my fetches remember when we're asking our session to return some calculations
for us I'm going to add my merged underscore summary so all the summaries that are part of this notebook
are gonna be in our merged summary at the moment it includes my accuracy calculation so we're kind of
doubling up him.
But this one we're using here.
The other one we're actually going to use intensive boat.
So for the sake of argument Brooke good to have two things that our session will return to us.
They're both the same thing.
So one of them is the merch summary at the moment which will get a lot more involved.
And the other one is the accuracy.
The point is you can provide a list of things that you want the session to return to you.
And on the other side we can store those things that we're returning in two separate variables called
the first one s for summary.
The second one for batch accuracy which will be this one right here and then our file writer will spring
into action.
So train underscore right to don't add under school summary.
We'll take our summary s which we're retrieving from the session and then write it to our desk.
And what I'd like you to do is also track the epoch in which it generated the summary.
So every epoch every time we pass the entire data set through the neural network we're gonna ask our
session to calculate all our summaries for us which at the moment only include our accuracy.
And we're then going to write this to the disk.
So now that we've added this code let's rerun our entire notebook.
But in order to do that and not confuse tensor flow we're going to have to add a little bit of code
at the end which will reset for the next run.
And what I'd like to do here is I'm going to close everything right.
I might take my file writer.
I'm going to close it on I take my session and I'm going to close that.
And then for good measure I'm going to reset the graph so TAF dot reset default graph parentheses will
reset all the calculations.
So when execute the cell FNM I come back up to where I've got set up tensor flow graph and was a run
all below.
So I'll take a little bit of time we're rerunning the training session and now we can fire up tensor
board.
So go to your command prompt or your terminal and open up a new window and their type tensor board space
hyphen hyphen log directory log dear is equal to and then paste in the path of where you're storing
all your logs.
Now we've got a couple of sub directories the path that I'd actually like you to paste into the shell
is this one right here.
That way you're getting all the logs for all the models that you're going to run.
If you hit enter then tentacle it will take a minute or so but after it's done I'll give you this year
earlier which you can take and then paste into your browser when that's all done you should see your
accuracy summary come up under the scalar section.
Brilliant.
So we've got that working.
Let's add a chart for our loss for good measure.
So back in Jupiter we can come back to where we've added our summary for accuracy scalar and we can
add another summary for the loss and we can add another summary for the loss.
And we do this in a very similar way.
TMF summary scalar because our loss is also just a number and open parentheses call it loss comma and
then loss loss is gonna be the name for this summary.
Maybe we should just call it cost to differentiate it a little bit and the value that I'd actually like
to plant is the result of this calculation right here.
So if I come back up here click on the cell here and say run all below our training well recommends
from zero.
And if we refresh tensor a board then we should see that a new sub folder has been created here.
Model one at whatever time you're running it slash train that's the sub folder right here.
And and here we have another file.
But in contrast to the first one this one has two summaries it has both the accuracy and it has the
cost.
So the name that shows up here is the name that we've given the summary and the values that show up
here are the values that come out of the session.
The reason that we get both the cost and the accuracy is because we're fetching the merged summary from
our session.
The merged summary if you remember includes all the summaries in the notebook.
Thanks to this merge all function here.
The next thing we're gonna do is we're going to add the validation loss and the validation accuracy
and we're gonna plot these on the same chart.
So side by side to do that create another file write to him.
I'll call this one validation underscore right.
And this will be equal to TMF dot summary dot file right.
And it's going to create its own sub directory so three plus single quotes forward slash validation.
Now in theory I should add the graph to our validation writer as well.
But the training writer the validation writer actually using the same graph so I can leave this line
of code out.
I don't actually need it so I'll just add shift enter on the cell and then it might come down to where
I'm doing my training.
This bit him is actually going to pertain to the training data set right for our validation data said
I'm going to take a little bit of a shortcut.
We've got ten thousand examples and the validation data set and I'm actually not going to bother patching
them.
So for brevity I'll add the validation bit right here in our loop.
It's still gonna be evaluated every epoch but it's gonna be outside of this loop.
What we're going to do here is we're gonna request a summary let's call it summary.
I'm going to request that summary from our session again with session says dog run and then we'll need
those fetches right fetches.
It's gonna be equal to the merged summary and then we have to supply the data that it should be calculated
over and that's going to be once again our feed dictionary the feed dictionary this time around it's
going to be made up of the validation data set though so capital X is gonna correspond to X on a square
vowel and Capital Y is going to correspond to y underscore Val.
This is how we're gonna calculate the cost and the accuracy from the validation features and the validation
labels.
Once we've got our summary our validation rider our file writer will spring into action with validation
on a score writer don't add on the score summary and here we're gonna supply the summary and the epoch
that the summary was generated under.
So let's try this out.
I think going to scroll back up him.
I've got set up tensor flow graph.
I'm actually going to delete these two sub folders and then I'm going to run all below we.
We start training once again five epochs and then when we refresh tensor board or alternatively wait
I think it does refresh by itself.
Just very very impatient we can see that there are now two new items here.
We've got this red thing and this blue one here.
Now if you're intensive board and you're seeing what I'm seeing right here namely a single dot for our
validation data set then simply refresh the page in your browser and tens a board will refresh and it
should then pull this in correctly.
I have definitely seen tense it would get confused sometimes.
But a quick refresh will usually solve the problem for you.
What you should see then is you should be able to track the accuracy of the training data and the validation
data on the same chart.
Typically you'll see the accuracy on the validation data set lower than the training data set.
And that's perfectly normal.
If you scroll down you'll also see the loss on the validation data set and the training data set on
the same chart.
Now back in Jupiter at the very bottom we've closed out our file writer and we've done this for our
train underscore right.
So let's not forget to do that for our validation a school right.
We're going to close this one as well that we will release all the resources for the next time we run
our notebooks.
So I know there's a lot going on in this lesson.
So what are the key learning points.
What are the key takeaways from our discussions so far.
Well any value that you want to visualize intensive board is tracked by this thing called a summary.
We've created two different types of summaries in this lesson.
The first one was for the accuracy and the second one was for the loss when we used our file writer
to add these summaries.
Then we wrote the information from the summaries toward disk.
That's how we ended up with these files in our logging directory.
The component that does the actual writing to the disk is something called a file writer.
We've created two of these are trained writer and our validation writer the main thing that we needed
to tell a file writer when we create one is where to dump the files.
In this case it was our logging directory for our model.
So setting up the file writer just requires a path to the location of where to dump the data.
The only other thing that we had to do to set up our file writer was to point it to our graph and we
did all of that using the add graph method and the graph that we used was our sessions graph.
So far in the kind of summaries that our file writer was writing to our disk was a scalar type of summary
the scalar summaries are the ones that generate this two dimensional chart since we had more than one
summary that we did we use this merge all function that way when the file writer was writing our summaries
to our disk.
It could write all the summaries at the same time.
In other words the file writer only had to write the merged summary in the coming lessons.
We're going to unlock a whole lot more power from tense board and we're going to do that by using some
of the other summaries that we have available.
I'm already looking forward to it.
See in the next lesson.