Maybe
Maybe

Reputation: 2279

Is there a way to do the same thing in tensorflow as it's done below in numpy?

By using numpy, I could index an array as below

x[mask==1]

Assuming x and mask are both numpy array and mask contains only 1 and 0.

Now I have both x and mask as Tensors and want to mimic above behavior. What should I do?

Upvotes: 0

Views: 59

Answers (2)

Eliethesaiyan
Eliethesaiyan

Reputation: 2322

This will give you a bolean mask

 import tensorflow as tf
 a = tf.Variable( [1,2,3,1] )   
 comparison = tf.equal( a, tf.constant( 1 ) )  
 start_op = tf.global_variables_initializer()    
 with tf.Session() as session:
   session.run(tart_op)

  print(session.run(comparison))

[ True False False  True]

Upvotes: 1

akuiper
akuiper

Reputation: 214927

Use boolean_mask.

Example:

x = tf.constant([[1,2],[3,4]])
mask = tf.constant([[1,0],[0,1]])
tf.boolean_mask(x, tf.equal(mask, 1)).eval()
# array([1, 4])

Upvotes: 2

Related Questions