1
正如在标题中所述,是否有一个Numpy.all()函数的TensorFlow等价物来检查布尔张量中的所有值是否为True
?实施这种检查的最佳方式是什么?解决这个问题的TensorFlow相当于numpy.all()
正如在标题中所述,是否有一个Numpy.all()函数的TensorFlow等价物来检查布尔张量中的所有值是否为True
?实施这种检查的最佳方式是什么?解决这个问题的TensorFlow相当于numpy.all()
使用tf.reduce_all,如下:
import tensorflow as tf
a=tf.constant([True,False,True,True],dtype=tf.bool)
res=tf.reduce_all(a)
sess=tf.InteractiveSession()
res.eval()
这将返回False
。
在另一方面,这将返回True
:
import tensorflow as tf
a=tf.constant([True,True,True,True],dtype=tf.bool)
res=tf.reduce_all(a)
sess=tf.InteractiveSession()
res.eval()
一种方式是做:
def all(bool_tensor):
bool_tensor = tf.cast(bool_tensor, tf.float32)
all_true = tf.equal(tf.reduce_mean(bool_tensor), 1.0)
return all_true
但是,它不是一个专用TensorFlow功能可按。只是一个解决方法。