【问题标题】:Compare tensor of unsigned int to python int将 unsigned int 的张量与 python int 进行比较
【发布时间】:2020-08-28 06:53:13
【问题描述】:

我想比较一个无符号整数的 TensorFlow 张量,例如tf.uint32 到 python 整数 1。我怎么做?以下全部失败

ones = tf.ones((2, 3), dtype=tf.uint32)
ones == 1
ones == [[1, 1, 1], [1, 1, 1]]
ones == tf.constant(1, dtype=tf.uint32)
ones == tf.ones((2, 3), dtype=tf.uint32)  # ?!!! I'm surprised this doesn't work
ones == tf.constant([[1, 1, 1], [1, 1, 1]], dtype=tf.uint32)

神秘的

tensorflow.python.framework.errors_impl.NotFoundError: Could not find valid device for node.
Node:{{node Equal}}
All kernels registered for op Equal :
  device='XLA_GPU'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, ..., DT_QUINT8, DT_QINT32, DT_BFLOAT16, DT_COMPLEX128, DT_HALF]
  device='XLA_CPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, ..., DT_QUINT8, DT_QINT32, DT_BFLOAT16, DT_COMPLEX128, DT_HALF]
  device='XLA_GPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, ..., DT_QUINT8, DT_QINT32, DT_BFLOAT16, DT_COMPLEX128, DT_HALF]
  device='GPU'; T in [DT_BOOL]
  device='GPU'; T in [DT_COMPLEX128]
  device='GPU'; T in [DT_COMPLEX64]
  device='GPU'; T in [DT_INT64]
  device='GPU'; T in [DT_INT16]
  device='GPU'; T in [DT_INT8]
  device='GPU'; T in [DT_INT32]
  device='GPU'; T in [DT_UINT8]
  device='GPU'; T in [DT_DOUBLE]
  device='GPU'; T in [DT_HALF]
  device='GPU'; T in [DT_FLOAT]
  device='CPU'; T in [DT_BOOL]
  device='CPU'; T in [DT_STRING]
  device='CPU'; T in [DT_COMPLEX128]
  device='CPU'; T in [DT_COMPLEX64]
  device='CPU'; T in [DT_INT64]
  device='CPU'; T in [DT_INT32]
  device='CPU'; T in [DT_BFLOAT16]
  device='CPU'; T in [DT_INT16]
  device='CPU'; T in [DT_INT8]
  device='CPU'; T in [DT_UINT8]
  device='CPU'; T in [DT_DOUBLE]
  device='CPU'; T in [DT_HALF]
  device='CPU'; T in [DT_FLOAT]
  device='XLA_CPU'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, ..., DT_QUINT8, DT_QINT32, DT_BFLOAT16, DT_COMPLEX128, DT_HALF]
 [Op:Equal]

tf.int32 工作正常

>>> tf.ones((2, 3), dtype=tf.int32) == 1
<tf.Tensor: shape=(2, 3), dtype=bool, numpy=
array([[ True,  True,  True],
       [ True,  True,  True]])>

【问题讨论】:

  • 你真的需要uint32,你总是可以投射和比较吗?还是只是想知道错误的来源?
  • 我想使用uint32 作为原始张量。我很高兴cast。事实上,这回答了我的问题,谢谢。但我现在也很好奇为什么这些比较都不起作用

标签: python tensorflow int equality


【解决方案1】:

您可以与uint8 进行比较。例如,下面的 sn -p 给出正确的输出:

ones = tf.ones((2, 3), dtype=tf.uint8)
ones == 1
<tf.Tensor: shape=(2, 3), dtype=bool, numpy=
array([[ True,  True,  True],
       [ True,  True,  True]])>

您还可以转换为任何不受支持的数据类型。

ones = tf.ones((2, 3), dtype = tf.uint32)
ones_c = tf.cast(ones, dtype = tf.int32)
ones_c == 1
<tf.Tensor: shape=(2, 3), dtype=bool, numpy=
array([[ True,  True,  True],
       [ True,  True,  True]])>

看起来像是一个错误,这里有一个关于行为的 GitHub 问题(跟踪):

https://github.com/tensorflow/tensorflow/issues/39457

更新:现在,您可以使用tf-nightly 来比较uint16uint32

https://pypi.org/project/tf-nightly-gpu/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    相关资源
    最近更新 更多