当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python tf.is_tensor用法及代码示例

检查 x 是否是可以传递给许多 TF 操作的 TF-native 类型。

用法

tf.is_tensor(
    x
)

参数

  • x 要检查的 python 对象。

返回

  • True 如果 x 是 TensorFlow-native 类型。

使用 is_tensor 区分无需任何转换即可被 TensorFlow ops 摄取的类型(例如,tf.Tensortf.SparseTensortf.RaggedTensor)与需要在摄取之前转换为张量的类型(例如,numpy ndarray 和 Python 标量)。

例如,在以下代码块中:

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

我们在访问它的 shapedtype 之前检查以确保 t 是一个张量(如果不是则转换它)。 (但请注意,并非所有 TensorFlow 原生类型都有 shape 或 dtype;tf.data.Dataset 是 TensorFlow 原生类型的一个示例,它既没有 shape 也没有 dtype。)

相关用法


注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.is_tensor。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。