将逻辑与的输出与 Tensorflow 中的两个二进制张量混淆

Confusing output of Logical AND with two binary tensors in Tensorflow

我在 tensorflow 中有两个二进制张量。我想将它们都转换为布尔张量(按元素)并基本上得到一个 "intersection",一个逻辑与。但是,在转换为布尔值以及 logical_and 部分时似乎出了点问题。我做错了什么?

sess = tf.InteractiveSession()
x = tf.random_uniform([1, 10], dtype=tf.int32, maxval=2))
y = tf.random_uniform([1, 10], dtype=tf.int32, maxval=2))
print(x.eval())
print(y.eval())
x = tf.cast(x, tf.bool)
y = tf.cast(y, tf.bool)
print(x.eval())
print(y.eval())
intersect = tf.logical_and(x, y)
print(intersect.eval())

这会产生以下输出:

[[0 0 1 1 0 1 0 0 1 1]]
[[0 1 0 0 1 0 1 0 0 1]]
[[False  True  True  True  True False False  True  True  True]]
[[False  True  True  True  True  True  True False  True  True]]
[[False False  True  True False False False False False  True]]

tf.random_normal 每次计算 intersect 时都会生成一个新的随机张量。

试试这个:

sess = tf.Session()
x = tf.random_uniform([1, 10], dtype=tf.int32, maxval=2)
sess.run([x, tf.cast(x, tf.bool)])

输出:

[array([[0, 1, 0, 0, 0, 0, 0, 0, 1, 1]], dtype=int32), 
 array([[False,  True, False, False, False, False, False, False,  True,
     True]])]

因此,如果您同时 运行 两个操作,您将获得相同的输出。 考虑将 xy 存储为 tf.constant