代码之家  ›  专栏  ›  技术社区  ›  musically_ut

为什么Tensorflow不覆盖张量的_ueq_uu?

  •  2
  • musically_ut  · 技术社区  · 8 年前

    Tensorflow覆盖了 Tensor including __lt__ , __ge__

    __eq__ seems to be conspicuously absent :

    ops.Tensor._override_operator("__lt__", gen_math_ops.less)
    ops.Tensor._override_operator("__le__", gen_math_ops.less_equal)
    ops.Tensor._override_operator("__gt__", gen_math_ops.greater)
    ops.Tensor._override_operator("__ge__", gen_math_ops.greater_equal)
    

    为什么 ==

    代码示例:

    a = tf.constant([1,2])
    b = tf.constant([3,4])
    a == b
    >>> False
    a < b
    >>> <tf.Tensor 'Less:0' shape=(2,) dtype=bool>
    

    另一方面,对于numpy:

    a = np.asarray([1,2])
    b = np.asarray([3, 4])
    a == b
    >>> array([False, False], dtype=bool)
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Martijn Pieters    8 年前

    张量 使生效 __eq__ the implementation only tests for identity . 我找到了 this GitHub issue ,这解释了为什么张量测试身份,而不传播:

    这可能是一个复杂的事实,即张量可以用作字典中的键,我相信使用 == 查找具有相同哈希的匹配对象

    __均衡器__ 因为超负荷播放,所以不能在字典中使用张量作为键。定义 __hash__ 方法(如果要将此类对象用作字典中的键,则需要), 必须 为两个相等的对象生成相同的哈希值;请参阅 __hash__ method :

    唯一需要的属性是比较相等的对象具有相同的哈希值

    但广播将为具有不同散列值的对象生成“真实”张量对象。

    (猜测是 __均衡器__ 会破坏布尔测试是错误的;布尔测试使用 __bool__

    如果需要对张量进行元素相等性测试,可以使用 tf.equal() tf.not_equal()