YUFENG GUO: I know that you use debuggers the right way
every time, and would never use print statements
to debug your code, right?
Because if you did, you might find that TensorFlow's print
statement doesn't quite work the same way as typical print
statements.
Today I'll show you how TensorFlow's print statements
work and how to make the most of them,
hopefully saving you some time along the way.
There are a couple ways to get things to print out
while writing TensorFlow code.
Of course, there's the classic built-in Python function,
print.
Then there is TensorFlow's print function,
tf.Print with a capital P. When working with TensorFlow,
it's important to remember that everything is ultimately
a graph computation.
This means that if you just use Python to print a TensorFlow
operation, it will simply show a description of what
that operation is since no values have
been passed through it yet.
It will also often show the dimensions
that are expected to be in that node if they're known.
If you want to print the values that
are flowing through a particular part of the graph
as it's being executed, then we'll
need to turn to using tf.Print.
Often we think of print as something
that we add on to the side of something, just let it run,
and then come back to our normal flow of operations.
In TensorFlow, only the nodes of the graph
that need to be executed will be executed.
So if you have a dangling print node in your graph,
it won't be executed.
So what are we to do?
We need to splice the print call into the graph like so.
The way this manifests in code is
to have the print call take as its first parameter
the node that is its input, and then
assign the output to a variable that
will serve as an input in a later node in the graph,
thus linking the print statement serially
into the flow of operations.
While the print statement does no computation
and just passes the value onward,
it will print the desired node as a side effect.
Another behavior that's a bit unlike what
we're used to seeing in a print statement
is that the print node that we introduce
is merely locking in when the print statement will happen,
namely when the node is reached in the computational graph.
However, there aren't really many restrictions
on what you print.
That is, you can print just about any node in the graph
that you can access.
This is the second argument to the tf.Print call--
an array of nodes to print.
Often we'll just use the same node
as the first argument, which was that input,
but we could include a lot more nodes in the print statement
if we'd like.
There is also a third argument--
a message.
This will allow you to prepend some string before printing out
those nodes so you can easily find a given print
statement in your logs.
So there you have it--
tf.Print in a nutshell.
But where might you utilize tf.Print in your code?
I often find myself using tf.Print in my input functions
to debug exactly what's being passed into my training.
Merely using the Python print is not quite enough here
since it will only print once--
when the input function graph is constructed.
So instead, we'll introduce the tf.Print call
into the input function's data pipeline.
Notice that we are just printing the same value
that we passed in as the input, but you could certainly
print other nodes in this array as well.
A word of caution here--
if you use tf.Print in your input function,
be sure to set some kind of limits
around the amount of data that you're passing in.
Otherwise, you may end up with a really long log file.
There are many ways in which TensorFlow's print statement is
not your typical print statement,
but it can be used to great effect
when you need to see the tensors flowing in your graph.
Thanks for watching this episode of Cloud AI Adventures.
Be sure to subscribe to the channel
to catch future episodes right when they come out.
And for now, go see how TensorFlow's print statements
can help you get more clarity into how your machine
learning is running.