Tensor flow creating a mask of various lengths

I have a length tensor in a tensor flow, let's say it looks like this:

[4, 3, 5, 2] 

I want to create a mask 1s and 0s, the number of which corresponds to the elements of this tensor filled with 0s, up to a total length of 8. Ie I want to create this tensor:

 [[1,1,1,1,0,0,0,0], [1,1,1,0,0,0,0,0], [1,1,1,1,1,0,0,0], [1,1,0,0,0,0,0,0] ] 

How can i do this?

+7
arrays tensorflow masking
source share
3 answers

This can be achieved using various TensorFlow transformations :

 # Make a 4 x 8 matrix where each row contains the length repeated 8 times. lengths = [4, 3, 5, 2] lengths_transposed = tf.expand_dims(lengths, 1) # Make a 4 x 8 matrix where each row contains [0, 1, ..., 7] range = tf.range(0, 8, 1) range_row = tf.expand_dims(range, 0) # Use the logical operations to create a mask mask = tf.less(range_row, lengths_transposed) # Use the select operation to select between 1 or 0 for each value. result = tf.select(mask, tf.ones([4, 8]), tf.zeros([4, 8])) 
+11
source share

Now this can be achieved with tf.sequence_mask . Read more at https://www.tensorflow.org/versions/r0.11/api_docs/python/array_ops.html#sequence_mask

+12
source share

I have a slightly shorter version than the previous answer. Not sure if it is more efficient or not working

  def mask(self, seq_length, max_seq_length): return tf.map_fn( lambda x: tf.pad(tf.ones([x], dtype=tf.int32), [[0, max_seq_length - x]]), seq_length) 
0
source share

All Articles