Skip to main content
 首页 » 编程设计

python之TensorFlow 1.8.0,急切执行和比较未按预期运行

2024年08月16日30zdz8207

在 TensorFlow 程序员指南中 Flow Control/FizzBuzz示例显示:

num = tf.constant(num) 
if num % 3 == 0 and num % 5 == 0: 

但是,这对我不起作用。

fiver % 5 == 0 
False 

我成功使用它的唯一方法是使用:

(num % 5).numpy() == 0 

python 比较是否应该与 EagerTensor 类型一起使用? tf.equal() 当然有效,但该示例显示了直接比较,例如 == 0。

请您参考如下方法:

这听起来像是文档中的错误。如果你看the source of the equality operator of the Tensor object ,

def __eq__(self, other): 
    # Necessary to support Python's collection membership operators 
    return id(self) == id(other) 

因此 my_boolean_tensor==True(或 False)将始终返回 False 因为张量对象不是 True 或 False 对象。

如果我正确理解该运算符中的注释,这种行为不太可能改变。