【问题标题】:Confusing output of Logical AND with two binary tensors in Tensorflow混淆逻辑与的输出与 Tensorflow 中的两个二进制张量
【发布时间】:2018-02-12 17:40:27
【问题描述】:

我在张量流中有两个二进制张量。我想将它们都转换为布尔张量(按元素),并且基本上得到一个“交集”,一个逻辑与。然而,在转换为布尔值以及 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]]

【问题讨论】:

    标签: tensorflow logical-operators tensor


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多