tensorflow boolean_mask inverse?

Viewed 1143

I have a tensor that I split up depending on a boolean_mask:

with tf.Session() as sess:
    boolean_mask = tf.constant([True, False, True, False])
    foo = tf.constant([[1,2],[3,4],[5,6],[7,8]])   
    true_foo = tf.boolean_mask(foo, boolean_mask, axis=0)
    false_foo = tf.boolean_mask(foo, tf.logical_not(boolean_mask), axis=0)
    print(sess.run((true_foo, false_foo)))

Outputs:

(array([[1, 2],
        [5, 6]], dtype=int32), 
 array([[3, 4],
        [7, 8]], dtype=int32))

I do some operations to true_foo and false_foo, and then I want to put them back together in the original order:

    true_bar = 2*true_foo
    false_bar = 3*false_foo
    bar = tf.boolean_mask_inverse(boolean_mask, true_bar, false_bar)
    print(sess.run(bar))

Should output:

array([[ 2, 4],
       [ 9,12],
       [10,12],
       [21,24]], dtype=int32)
2 Answers

Similar to your own solution but with tf.scatter_nd.

true_mask = tf.cast(tf.where(boolean_mask), tf.int32)
false_mask = tf.cast(tf.where(~boolean_mask), tf.int32)
t_foo = tf.scatter_nd(true_mask, true_bar, shape=tf.shape(foo))
f_foo = tf.scatter_nd(false_mask, false_bar, shape=tf.shape(foo))
res = t_foo + f_foo
# array([[ 2,  4],
#        [ 9, 12],
#        [10, 12],
#        [21, 24]], dtype=int32)

Basically, you can scatter the true_bar and false_bar to two different tensors, and add them together.

This is what I'm currently doing, but it seems unnecessarily complicated:

def boolean_mask_inverse(boolean_mask, true_bar, false_bar):
    stacked_bar = tf.concat((true_bar, false_bar), axis=0)
    index_mapping = tf.where(boolean_mask)
    true_index_mapping = tf.where_v2(boolean_mask)[:,0]
    false_index_mapping = tf.where_v2(tf.logical_not(boolean_mask))[:,0]
    stacked_index_mapping = tf.concat((true_index_mapping, false_index_mapping), axis=0)
    basic_indices = tf.range(tf.shape(stacked_index_mapping)[0])
    inverse_index_mapping = tf.gather(basic_indices, stacked_index_mapping)
    return tf.gather(stacked_bar, inverse_index_mapping)
Related