【问题标题】:Convert boolean tensor to binary in tensorflow在张量流中将布尔张量转换为二进制
【发布时间】:2021-05-10 16:34:33
【问题描述】:

我有一个布尔张量,我想转换为一个和零的二进制张量。

把它放到上下文中 - 我有以下张量

[[ True False  True]
 [False  True False]
 [ True False  True]]

我需要将其转换为 1 和 0,这样我就可以将元素与值张量相乘,即:

[[1.         0.64082676 0.90568966]
 [0.64082676 1.         0.37999165]
 [0.90568966 0.37999165 1.        ]]

这两个功能我都试过了

   masks = tf.map_fn(logical, masks, dtype=tf.float32)
   masks = tf.vectorized_map(logical, masks)

@tf.function
def logical(x):
    if tf.equal(x, True):
        return zero
    return one

但不幸的是没有运气。我也尝试直接与布尔张量相乘,但这是不允许的。

那么关于如何解决这个问题的任何指导?

【问题讨论】:

    标签: python tensorflow machine-learning tensorflow2.0


    【解决方案1】:

    我想我用这个和一些魔法解决了它。设惩罚为值张量

    test = tf.where(masks, penalties * 0.0, penalties * 1.0)
    

    【讨论】:

    • 这是最好的答案。它对我有用。
    【解决方案2】:

    对于真正需要问题的人:

    tf.where(r, 1, 0)
    

    r 是你的布尔张量

    【讨论】:

      猜你喜欢
      • 2022-09-29
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 2018-05-28
      • 1970-01-01
      • 2016-06-09
      • 2016-02-19
      相关资源
      最近更新 更多