tf.is_tensor  |  TensorFlow v2.16.1 (original) (raw)

tf.is_tensor

Stay organized with collections Save and categorize content based on your preferences.

Checks whether x is a TF-native type that can be passed to many TF ops.

View aliases

Compat aliases for migration

SeeMigration guide for more details.

tf.compat.v1.is_tensor

tf.is_tensor(
    x
)

Use is_tensor to differentiate types that can ingested by TensorFlow ops without any conversion (e.g., tf.Tensor, tf.SparseTensor, andtf.RaggedTensor) from types that need to be converted into tensors before they are ingested (e.g., numpy ndarray and Python scalars).

For example, in the following code block:

if not tf.is_tensor(t):
  t = tf.convert_to_tensor(t)
return t.shape, t.dtype

we check to make sure that t is a tensor (and convert it if not) before accessing its shape and dtype. (But note that not all TensorFlow native types have shapes or dtypes; tf.data.Dataset is an example of a TensorFlow native type that has neither shape nor dtype.)

Args
x A python object to check.
Returns
True if x is a TensorFlow-native type.