Skip to main content

How to Check the Data Type of a Tensor in TensorFlow

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Every value in a TensorFlow tensor has the same type, its dtype: whole numbers, decimals, true-or-false values or text. Many errors come from two tensors with different dtypes, so it's worth knowing how to check. This guide reads the dtype of four tensors in TensorFlow 2.22.0.

Syntax​

Syntax
<tensor>.dtype
<tensor>.dtype == tf.<data type>
<tensor>.dtype.name
PartWhat it means
<tensor>.dtypeThe data type of every value in the tensor, a tf.dtypes.DType
== tf.<data type>True when the tensor has that type, such as tf.int32
.dtype.nameThe type's name as text, such as "int32"

The tensor guide starts with it: "Tensors are multi-dimensional arrays with a uniform type (called a dtype)." The DType API page describes the object .dtype returns: it "Represents the type of the elements in a Tensor."

The Example​

Four tensors, one of each common kind: whole numbers, decimal numbers, true-or-false values and words.

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

dtypes.py
import tensorflow as tf

whole = tf.constant([1, 2, 3])
decimal = tf.constant([1.5, 2.5])
flags = tf.constant([True, False])
words = tf.constant(["cat", "dog"])

print(whole.dtype)
print(decimal.dtype)
print(flags.dtype)
print(words.dtype)
print(whole.dtype == tf.int32)
print(whole.dtype.name)

The dtypes.py script in a macOS-style code window: tensors of whole numbers, decimals, True and False, and the words cat and dog, then prints of each one's dtype, of whole.dtype == tf.int32, and of whole.dtype.name.

No tensor names its type: TensorFlow picks one from the values. The last two lines compare a dtype with tf.int32 and read its name.

Step 2: Run It​

Terminal
uv run python dtypes.py
Output
<dtype: 'int32'>
<dtype: 'float32'>
<dtype: 'bool'>
<dtype: 'string'>
True
int32

The terminal after uv run python dtypes.py: dtype int32, float32, bool and string, then True, then int32.

TensordtypeHolds
wholeint32Whole numbers, 4 bytes each
decimalfloat32Decimal numbers, 4 bytes each
flagsboolTrue or False
wordsstringText, stored as bytes

whole.dtype == tf.int32 was True, and .name gave the plain text int32, handy in messages and logs. The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
Checking the type with Python's type()type(whole) printed <class 'tensorflow.python.framework.ops.EagerTensor'>, the kind of object, which is the same for every tensorRead whole.dtype

FAQ​

Why does TensorFlow pick int32 and float32, not 64-bit types?​

That's its rule for Python numbers. The tensor guide: "TensorFlow converts Python integers to tf.int32 and Python floating point numbers to tf.float32". A 32-bit value takes 4 bytes, half the 8 bytes of a 64-bit one.

Can I compare a dtype with its name as text?​

Yes. whole.dtype == "int32" was True, and whole.dtype == "float32" was False. TensorFlow's DType turns the other side into a dtype before it compares, and returns False when that isn't possible.

Can I mix dtypes in one tensor?​

No. A tensor has one dtype for all its values. To combine tensors of different dtypes, convert one with tf.cast first.

Conclusion​

.dtype gave int32, float32, bool and string for whole numbers, decimals, true-or-false values and text. It compares with tf.int32 and friends, .dtype.name turns it into text, and type() only says that the object is a tensor.

Additional Resources​