How to create a tensor mask in Tensorflow?

A tensor mask can be very useful when developing a Deep Learning Model. It can be used to turn on/off activations deterministically or randomly.

We will make a mask of 0 s and 1s:

1
2
3
4
5
[[1,1,1,1,0,0,0,0],
[1,1,1,0,0,0,0,0],
[1,1,0,0,0,0,0,0],
[1,0,0,0,0,0,0,0],
]

Make a 4x8 matrix where each row contains the length repeated 8 times: lengths=[4,3,5,2]:

1
length_transposed = tf.expand_dims(lengths, 1)

Make a 4x8 matrix where each row contains [0, 1, …7]

1
2
range = tf.range(0, 8 , 1)
range_row = tf.expand_dims(range, 0)

Use the logical operations to create a mask:

1
mask = tf.less(range_row, lengths_transposed)

#use the select operation to select between 1 or 0 for each value:

1
result = tf.select(mask, tf.ones([4,8]), tf.zeros(4,8]))

Check

1
tf.sequence(lengths, max(en=None, dtype=tf.bool, name=None))