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.
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
, and
tf.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.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.is_tensor\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/tensor_util.py#L1128-L1156) |\n\nChecks whether `x` is a TF-native type that can be passed to many TF ops.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.is_tensor`](https://www.tensorflow.org/api_docs/python/tf/is_tensor)\n\n\u003cbr /\u003e\n\n tf.is_tensor(\n x\n )\n\nUse `is_tensor` to differentiate types that can ingested by TensorFlow ops\nwithout any conversion (e.g., [`tf.Tensor`](../tf/Tensor), [`tf.SparseTensor`](../tf/sparse/SparseTensor), and\n[`tf.RaggedTensor`](../tf/RaggedTensor)) from types that need to be converted into tensors before\nthey are ingested (e.g., numpy `ndarray` and Python scalars).\n\nFor example, in the following code block: \n\n if not tf.is_tensor(t):\n t = tf.convert_to_tensor(t)\n return t.shape, t.dtype\n\nwe check to make sure that `t` is a tensor (and convert it if not) before\naccessing its `shape` and `dtype`. (But note that not all TensorFlow native\ntypes have shapes or dtypes; [`tf.data.Dataset`](../tf/data/Dataset) is an example of a TensorFlow\nnative type that has neither shape nor dtype.)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----|---------------------------|\n| `x` | A python object to check. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| `True` if `x` is a TensorFlow-native type. ||\n\n\u003cbr /\u003e"]]