Get A TensorFlow Tensor By Name

Get A TensorFlow Tensor By Name by using the TensorFlow get_default_graph operation and then the TensorFlow get_tensor_by_name operation

Type: FREE   By: Sebastian Gutierrez, AIWorkbox.com Instructor Sebastian Gutierrez   Duration: 2:32   Technologies: TensorFlow, Python

Page Sections: Video  |  Code  |  Transcript


< > Code:

You must be a Member to view code

Access all courses and lessons, gain confidence and expertise, and learn how things work and how to use them.

    or   Log In


Transcript:

First, we import TensorFlow as tf.

import tensorflow as tf


Next, let’s print out the TensorFlow version that we are using.

print(tf.__version__)

We are using TensorFlow 1.0.1.


For this video, we’re going to create a TensorFlow variable that will hold random numbers between 0 and 10 that are of the data type 32-bit signed integers.

random_int_var_one_ex = tf.get_variable("random_int_var_one",
                                        initializer=tf.random_uniform([2, 3, 4],
                                        minval=0,
                                        maxval=10,
                                        dtype=tf.int32))

Note that we also give it a specific name: random_int_var_one.

So we’re going to create it using the tf.get_variable operation and assign it to the Python variable random_int_var_one_ex.

So notice that there is a difference between the variable we give it here in the get variable and the Python variable.


Now that we’ve assigned our variable, let’s print to see what we have.

print(random_int_var_one_ex)

We have a TensorFlow tensor whose name is random_int_var_one (which is what we would expect from here) /read:0, shape 2x3x4, data type is int32.


Now that we’ve created our TensorFlow variable, it’s time to run the computational graph.


So we launch the graph in the session:

sess = tf.Session()


And then we initialize the global variable in the graph.

sess.run(tf.global_variables_initializer())


Now that we’ve done this, let’s print our variable again, only this time within a TensorFlow session run.

print(sess.run(random_int_var_one_ex))

And we can see that we have a 2x3x4 tensor whose values are between 0 to 10.


To get the tensor by name in a TensorFlow session, we can use the name operation.

random_int_var_one_ex_tensor_name = random_int_var_one_ex.name

So we pass in our random_int_var_one_ex and we say .name, and then we’re going to assign that to the Python variable random_int_var_one_ex_tensor_name.


Then we can print this Python variable:

print(random_int_var_one_ex_tensor_name)

And we see that the name, like we saw above, is random_int_var_one:0.


To get the tensor by name in a TensorFlow session, we’re going to use the tf.get_default_graph operation and then the TensorFlow get_tensor_by_name operation as follows:

print(sess.run(tf.get_default_graph().get_tensor_by_name(random_int_var_one_ex_tensor_name)))

So tf.get_default_graph().get_tensor_by_name and then we’re going to pass in the Python name we just defined, so random_int_var_one_ex_tensor_name.

When we do that, we see that it’s the exact same values: 6, 8, 2, 9; 6, 8, 2, 9.

So we can see that we were able to get the tensor by its name rather than by the Python variable.


Finally, we close the TensorFlow session to release the TensorFlow resources we used within the session.

sess.close()


That is how you can get a TensorFlow tensor by name using the tf.get_default_graph operation and then the TensorFlow get_tensor_by_name operation.



Back to deep learning tutorial lesson list