【问题标题】:Tensorflow compare tf.int32 and tf.constant gives errorTensorflow 比较 tf.int32 和 tf.constant 给出错误
【发布时间】:2016-11-18 13:31:54
【问题描述】:
import tensorflow as tf
a=tf.int32 
b=tf.constant(3)
a==b

给出错误而不是给出'false'

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/dtypes.py", line 248, in __eq__
    and self._type_enum == as_dtype(other).as_datatype_enum)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/dtypes.py", line 536, in as_dtype
    if key == type_value:
TypeError: data type not understood

为什么会产生错误。我正在使用张量流 0.8 不应该能够检查任何变量是否相等。

我试图实现的是检查一个对象是否存在于列表中

a=tf.int32
b=[tf.constant(3),..other objects]
if a in b:
  do_something()

【问题讨论】:

    标签: python python-3.x tensorflow python-3.4


    【解决方案1】:

    这种比较没有意义。

    >>> a=tf.int32
    >>> type(a)
    <class 'tensorflow.python.framework.dtypes.DType'>
    >>> print(a)
    <dtype: 'int32'>
    

    >>> b=tf.constant(3)
    >>> type(b)
    <class 'tensorflow.python.framework.ops.Tensor'>
    >>> print(b)
    Tensor("Const_1:0", shape=(), dtype=int32)
    

    您在这里看到的是,您正试图将一个类型(或类)与该类的某种形式的实例进行比较。实际这样做是没有意义的。抛出错误是因为 tf 不知道如何实际执行此相等检查。

    更新

    我看到你更新了你的答案,所以这里是回复: 虽然这是检查对象是否在集合中的正确语法,但我上面的回答仍然适用。变量a 并不是指您认为它所做的事情。它包含对int32 类型的实际定义的引用。在张量中寻找它是没有意义的。

    【讨论】:

    • 我试图检查一个对象是否存在于列表中。 (对于列表中的 i)如果 i 是 tf.int32 并且列表包含 tf.constant(3) 由于此相等性检查,我会收到错误消息。知道我怎么能做到这一点
    • 因为这可能对其他人有所帮助,我建议您针对您的实际问题打开一个新问题。您可以在此处添加评论,以便我找到新问题。如果此答案对您有所帮助,请考虑接受。
    • 我真的需要检查该对象是否存在于该列表中。所以我迭代并检查项目是否为 tf.int32 而不是 ==
    猜你喜欢
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多