What's the equivalent operation in Tensorflow for this? For example, I have a x = np.array([-12,4,6,8,100]). I want to do as simple as this: x = x[x>5], but I can't find any TF operation for this. Thanks!
1 Answer
In TF you can do something like this to achieve similar results.
import numpy as np
import tensorflow as tf
x = np.array([-12,4,6,8,100])
y = tf.gather(x, tf.where(x > 5))
y.numpy().reshape(-1)
array([ 6, 8, 100])
Details
The tf.where will return the indices of condition that are True. Such as
x = np.array([-12,4,6,8,100])
tf.where(x > 5)
<tf.Tensor: shape=(3, 1), dtype=int64, numpy=
array([[2],
[3],
[4]])>
And next, using tf.gather, it slices from params (x) axis according to indices (from tf.where). Such as
tf.gather(x, tf.where(x > 5))