Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there .all() or .any() equivalent in python Tensorflow

Trying to find a similar operation to .any(), .all() methods that will work on a tensor. Here is a scenario:

a = tf.Variable([True, False, True], dtype=tf.bool)

# this is how I do it right now
has_true = a.reduce_sum(tf.cast(a, tf.float32)) 
# this is what I'm looking for
has_true = a.any()

Currently converting my boolean tensor into int using reduce_sum to see if there are any truths in it. Is there a cleaner way to perform this operation?

like image 478
nikolaevra Avatar asked Nov 20 '18 20:11

nikolaevra


People also ask

What is TensorFlow how many types of tensors are there?

Each operation you will do with TensorFlow involves the manipulation of a tensor. There are four main tensor type you can create: tf.

Does TensorFlow use tensors?

Learn about Tensors, the multi-dimensional arrays used by TensorFlow. Tensors are multi-dimensional arrays with a uniform type (called a dtype ). You can see all supported dtypes with names(tf$dtypes) . If you're familiar with R array or NumPy, tensors are (kind of) like R or NumPy arrays.

How do you initialize an empty tensor in TensorFlow?

If you use tf. global_variables_initializer() to initialize your variables, disable putting your variable in the list of global variables during initialization by setting collections=[] . Here np. empty is provided to x only to specify its shape and type, not for initialization.

What are tensors in Python?

A tensor is a generalization of vectors and matrices and is easily understood as a multidimensional array. In the general case, an array of numbers arranged on a regular grid with a variable number of axes is known as a tensor.


1 Answers

There are tf.reduce_any and tf.reduce_all methods:

sess = tf.Session()

a = tf.Variable([True, False, True], dtype=tf.bool)
sess.run(tf.global_variables_initializer())

sess.run(tf.reduce_any(a))
# True
sess.run(tf.reduce_all(a))
# False
like image 107
Psidom Avatar answered Oct 28 '22 13:10

Psidom