1

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!

0

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.