正如标题中所述,是否存在与numpy.all()函数等效的numpy.all()函数来检查bool张量中的所有值是否都是True?实现这种检查的最佳方法是什么?
发布于 2017-08-17 13:31:04
使用全,如下所示:
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()发布于 2017-08-17 13:30:09
解决这一问题的一种方法是:
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专用的功能。只是个解决办法。
发布于 2021-04-07 13:36:07
你可以在tf 2.4中使用tf.experimental.numpy.all
x = tf.constant([False, False])
tf.experimental.numpy.all(x)https://stackoverflow.com/questions/45736314
复制相似问题