List All Tensor Names In A TensorFlow Graph

Use the TensorFlow Get Operations Operation to list all Tensor names in a TensorFlow graph

Use the TensorFlow Get Operations Operation to list all Tensor names in a TensorFlow graph

Video Transcript


This video will show you how to use the TensorFlow get_operations operation to list all tensor names in a TensorFlow graph.


First, we import TensorFlow as tf.

import tensorflow as tf


Next, we print out the version of TensorFlow we are using.

print(tf.__version__)

We are using TensorFlow 1.10.0.


We start out by defining a simple TensorFlow constant tensor whose value is going to be the integer 10.

tf_constant_one    = tf.constant(10, name="tensor_constant_ten")

So we use tf.constant, we give it the value of integer 10, and we give it the name of tensor_constant_ten, we assign it to the Python variable tf_constant_one.


Let’s print out the tf_constant_one Python variable to see what we have.

print(tf_constant_one)

We see that it’s a TensorFlow tensor, we see our name, tensor_constant_ten, we see that the shape is left blank because it’s just an integer, and we see that the data type is int32 which is inferred by our number we initially passed.


Let’s also define a slightly more complicated tensor full of zeros.

tf_constant_zeros  = tf.zeros(shape=[2,3,4], dtype="float32", name="tensor_constant_zeros")

So we use tf.zeros, the shape we’re going to give it is 2x3x4, the data type is float32, and the name we’re going to give it is tensor_constant_zeros, and then we assign it to the Python variable tf_constant_zeros.


Let’s print the tf_constant_zeros variables to see what we have.

print(tf_constant_zeros)

We see that it’s a TensorFlow tensor, we see that we have our name that we assigned it, tensor_constant_zeros, the shape is 2x3x4, and the data type is float32.


Now that we have created our TensorFlow tensors, let’s create the computational graph.


So we launch the graph in a session.

sess = tf.Session()


Then we initialize all the global variables in the graph.

sess.run(tf.global_variables_initializer())


Before we get to figuring out the names of the tensors in our graph, let’s print the values of the tensors to make sure they did what we thought they should be doing.


First, we print the tf_constant_one variable in a TensorFlow session.

print(sess.run(tf_constant_one))

So print (sess.run(tf_constant_one)).

We see the number 10 which is an integer.


Then we print the tf_constant_zeros variable in the TensorFlow session.

print(sess.run(tf_constant_zeros))

This time, we get a 2x3x4 tensor who’s full of zeros.

Also note that each zero has a decimal point which is used to represent that it is a floating point number.


Now that we’ve done this, let’s get into the meat of how to list all of the tensor names in a TensorFlow graph.


The first thing we do is get the graph we are constructing from our current TensorFlow session.

tf_sess_graph = sess.graph

So we use the graph operation on our session and we’re going to assign it to the Python variable tf_sess_graph.


We can print the tf_sess_graph Python variable to see what we have.

print(tf_sess_graph)

We see that it is TensorFlow, and eventually we see that it is actually a Graph object.


Next, let’s get all of the operations from this graph.

tf_sess_ops = tf_sess_graph.get_operations()


So we can see here we have tf_sess_graph, and from that graph we’re going to use the get_operations operation.

And we’re going to assign this result to the Python variable tf_sess_ops.


Let’s print this tf_sess_ops variable to see what’s in it.

print(tf_sess_ops)

We see three operations listed in a Python list.

Here is the first one, here is the second one, and here is the third one.

Note that this NoOp is a placeholder we can ignore as it doesn’t do anything.


The great thing about the graph operations being in a Python list is that we can iterate through the list to do something with each item.


So what we’ll do to get the list of all of the tensor names in our TensorFlow graph is to look at all of the operations to get the name of the operation.

for tf_op in tf_sess_ops:

So we’re going to use a For loop.

So For, we’re going to loop through this Python list, and we’re going to define a temporary variable called tf_op.


So for each of these operations in this list which we’re going to name tf_op, we want to print the name.

for tf_op in tf_sess_ops:
    print(tf_op.name)

So here again the print, and then tf_op which is our temporary variable representing each of the items in the list.

We’re going to use the name operation to get the name.


So one thing to remember is that operations in TensorFlow represent actual graph nodes in the graph.

The other thing is that tensors are represented as graph nodes as well.

So what we’re doing here is we’re saying the tensors are nodes and the nodes are in the graph.


So we’re going to loop through all of the operations, that is all of the graph nodes, that is all of the tensors to get the tensor names.

for tf_op in tf_sess_ops:
    print(tf_op.name)

And when we do that, we see that we have our tensor_constant_ten tensor name, we see that we have tensor_constant_zeros tensor name, and we have the init operation which was the NoOp operation.


Perfect - We were able to use the TensorFlow get_operations operation to list all of the tensor names in our TensorFlow graph.

Receive the Data Science Weekly Newsletter every Thursday

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Easy to unsubscribe at any time. Your e-mail address is safe.