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?
Each operation you will do with TensorFlow involves the manipulation of a tensor. There are four main tensor type you can create: tf.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With