【问题标题】:tensorflow-TypeError: Input 'x' of 'LogicalAnd' Op has type float32 that does not match expected type of booltensorflow-TypeError:'LogicalAnd' Op 的输入'x' 的 float32 类型与预期的 bool 类型不匹配
【发布时间】:2022-01-02 17:22:38
【问题描述】:

我有两个float32的tf数组,我在两个数组之间进行logical_and运算

tf.math.logical_and(b, a)

在这里,我得到了错误 - TypeError: Input 'x' of 'LogicalAnd' Op has type float32 that does not match expected type of bool.

我该如何解决这个问题?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    如果您打算使用 tf.math.logical_and,您的张量 ab 需要是布尔张量。检查docs。这将工作:

    import tensorflow as tf 
    
    a = tf.constant([5,4,3,2], dtype=tf.float32)
    b = tf.constant([5,6,7,8], dtype=tf.float32)
    c = tf.math.logical_and(b, a)
    

    这样的事情会起作用:

    import tensorflow as tf 
    
    a = tf.constant([5,4,3,2], dtype=tf.float32)
    b = tf.constant([5,6,7,8], dtype=tf.float32)
    
    a = tf.cast(tf.boolean_mask(a, [True, False, True, False]), dtype=tf.bool)
    b = tf.cast(tf.boolean_mask(b, [True, False, True, False]), dtype=tf.bool)
    
    print(tf.math.logical_and(b, a))
    

    只要确保张量是布尔值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2019-08-14
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      相关资源
      最近更新 更多