Search code examples
tensorflowmasktensorone-hot-encoding

Create tensors where all elements up to a given index are 1s, the rest are 0s


I have a placeholder lengths = tf.placeholder(tf.int32, [10]). Each of the 10 values assigned to this placeholder are <= 25. I now want to create a 2-dimensional tensor, called masks, of shape [10, 25], where each of the 10 vectors of length 25 has the first n elements set to 1, and the rest set to 0 - with n being the corresponding value in lengths.

What is the easiest way to do this using TensorFlow's built in methods?

For example:

lengths = [4, 6, 7, ...]

-> masks = [[1, 1, 1, 1, 0, 0, 0, 0, ..., 0],
            [1, 1, 1, 1, 1, 1, 0, 0, ..., 0],
            [1, 1, 1, 1, 1, 1, 1, 0, ..., 0],
            ...
           ]

Solution

  • You can reshape lengths to a (10, 1) tensor, then compare it with another sequence/indices 0,1,2,3,...,25, which due to broadcasting will result in True if the indices are smaller then lengths, otherwise False; then you can cast the boolean result to 1 and 0:

    lengths = tf.constant([4, 6, 7])
    n_features = 25
    ​
    import tensorflow as tf
    ​
    masks = tf.cast(tf.range(n_features) < tf.reshape(lengths, (-1, 1)), tf.int8)
    
    with tf.Session() as sess:
        print(sess.run(masks))
    
    #[[1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    # [1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    # [1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]