如果您打算使用 tf.math.logical_and,您的张量 a 和 b 需要是布尔张量。检查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))
只要确保张量是布尔值。