Print TensorFlow Tensor Shape

Use the TensorFlow get_shape operation to print the static shape of a TensorFlow tensor as a list

Use the TensorFlow get_shape operation to print the static shape of a TensorFlow tensor as a list

Video Transcript


This video will show you how to use the TensorFlow get_shape operation to print the static shape of a TensorFlow tensor as a list.


First, we import TensorFlow as tf.

import tensorflow as tf


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

print(tf.__version__)

We are using TensorFlow 1.10.0.


In this example, we’ll create a tensor and then get the shape of it that is inferred from the operation that was used to create the tensor.


First, we create a tensor full of float32 numbers pulled from a random uniform probability distribution where the minimum value is 0 and the max value is 1.

tf_constant_ru_float = tf.random_uniform([2, 4, 6], minval=0, maxval=1, dtype=tf.float32)

So we use tf.random_uniform, the shape is going to be 2x4x6, min value 0, max value is 1, and the dtype is float32.

We assign all of this to the Python variable tf_constant_ru_float.


Let’s print the tf_constant_ru_float variable to see what we have.

print(tf_constant_ru_float)

We see that it’s a tensor, we see that TensorFlow gave it a name, we see that the shape is 2x4x6, and the data type is float32 which is exactly how we just defined it.


Now that we have our tensor, let’s use TensorFlow’s get shape operation to see what shape the tensor has.

tf_constant_ru_float.get_shape()

We see that a TensorShape object is returned with a list that has a dimension of 2, dimension of 4, and dimension of 6.


We can check the type of what object is being returned if we pass that result into the Python type operation.

type(tf_constant_ru_float.get_shape())

When we do that, we see that the class is tensorflow.python.framework.tensor_shape.TensorShape.

Which is great but what we want is we want the TensorShape as a Python list.


So to get the TensorShape as a Python list, we are going to take our Python variable which contains our tensor.

We’re going to then do the .get_shape operation and then we’re going to do the .as_list operation.

tf_constant_ru_float.get_shape().as_list()

So our tensor, we’re going to get the shape which returns a TensorShape object, and then that TensorShape object allows us to use the .as_list operation.

When we do that, we get back a list that is 2x4x6, which is exactly how we originally defined it.


Lastly, to make sure it’s a list, we’re going to pass in the whole thing, so our tensor .get_shape .as_list to the Python type operation.

type(tf_constant_ru_float.get_shape().as_list())

This will tell us whether or not it actually is a list.

When we evaluate this, we see that it is a class of list.


Pefect - We were able to use the TensorFlow get_shape operation to print the static shape of a TensorFlow tensor as a list.

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.