Reputation: 2279
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 Tensor
s and want to mimic above behavior. What should I do?
Upvotes: 0
Views: 59
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
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