【问题标题】:How to get the type of a Tensor?如何获取张量的类型?
【发布时间】:2017-03-13 07:24:08
【问题描述】:

我正在寻找类似于以下效果的东西:

x.get_shape()

这将给出x 的类型。这个有什么功能吗?

【问题讨论】:

  • 您在哪里看到x.get_shape() 返回x 的类型?你想知道张量的形状吗? x.get_shape()返回x变量的TensorShape,那你要什么?

标签: python tensorflow


【解决方案1】:

您可以使用get_shape() 来获取张量流变量的形状。

>>> x = tf.Variable(tf.random_normal([256, 100]))
>>> x.get_shape()
(256, 100)

您可以使用dtype 属性来获取张量流变量的类型。

>>> x = tf.Variable(tf.random_normal([256, 100]))
>>> x.dtype
<dtype: 'float32_ref'>

您可以使用 dtype 的as_numpy_dtype 属性将tf.dtype 转换为numpy dtype

>>> x = tf.Variable(tf.random_normal([256, 100]))
>>> x.dtype.as_numpy_dtype
<class 'numpy.float32'>

【讨论】:

  • 我收到此错误:tf.Print(self._batch_val.dtype, [self._batch_val.dtype], "dtype of batch_val:") TypeError: Failed to convert object of type 到张量。内容:。考虑将元素转换为支持的类型。有什么建议吗?
  • @Shibani 你不需要使用tf.Print() 来打印张量的类型,因为张量的类型在会话运行期间不会改变。 Tensor 类型在您构建图形时确定,因此只需使用print(x.dtype)
【解决方案2】:

要得到你可以做的类型

x.dtype

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    相关资源
    最近更新 更多