tf.dtype: Print And Check TensorFlow Tensor Type

tf.dtype - Use TensorFlow's dtype operation to print and check a TensorFlow's Tensor data type

tf.dtype - Use TensorFlow's dtype operation to print and check a TensorFlow's Tensor data type

Video Transcript


This video will show you how to use TensorFlow’s dtype operation to print and check a TensorFlow tensor’s data type.


First, we import TensorFlow as tf.

import tensorflow as tf


Then we check what version of TensorFlow we are using.

print(tf.__version__)

We are using TensorFlow 1.10.0.


Let’s start out by creating a simple 2x2 TensorFlow constant tensor.

example_tensor = tf.constant(
[
    [1.1, 2.2],
    [3.3, 4.4]
])

So we use tf.constant.

We’re going to have a 2x2 tensor.

You can notice that it has decimal points, so it’s going to be a floating point tensor, and we’re going to assign it to the Python variable example_tensor.


Let’s now print our tensor to see what we have.

print(example_tensor)

We can see that it’s a TensorFlow tensor, we can see that the shape is 2x2, and we can see that the dtype is float32, which is what we would expect given when we initially created the tensor, we used the decimal point.


Let’s check the data type of this TensorFlow node that we have created.

print(example_tensor.dtype)

So we’re going to do example_tensor.dtype, and we’re going to print it.

We see that the dtype is float32.


Next, let’s use Python’s type operation to check what type of data type object it is.

type(example_tensor.dtype)

We can see that it’s a class.

It’s a tensorflow.python.framework.dtypes.DType.


Now that we have our tensor, let’s launch the graph in a session so that we can evaluate the tensor.

sess = tf.Session()


We initialize all the global variables in the graph.

sess.run(tf.global_variables_initializer())


So first, let’s evaluate the tensor in a TensorFlow session.

evaluated_tensor = sess.run(example_tensor)

So sess.run, we’re going to evaluate our example_tensor, and the result that is returned is going to be assigned to the Python variable evaluated_tensor.


Next, let’s just run the tensor to see what we got.

evaluated_tensor

We see that it’s an array, it’s 2x2, and the data type is float32.


Now that we’ve evaluated the tensor in a TensorFlow session, we can use the dtype again to see what is returned.

evaluated_tensor.dtype

We see dtype and float32.


In case you had forgotten, when tensors are evaluated, they get turned into NumPy arrays which we can see when we check the type of the dtype.

type(evaluated_tensor.dtype)

So evaluated_tensor.dtype, and we’re using Python’s type operation, and we see that it’s a class of numpy.dtype.


Finally, we can use the print operation to print the tensor’s data type, and we see that it is float32.

print(evaluated_tensor.dtype)


Perfect - We were able to use TensorFlow’s dtype operation to print and check a TensorFlow tensor’s data type.

Receive the Data Science Weekly Newsletter every Thursday

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