Search code examples
pythontensorflowtensorragged

Pad the last dimension of a tensor with different lengths to a specific length


I have a similar question, this one TensorFlow - Pad unknown size tensor to a specific size?. My question is more difficult though, and I didn't find any solutions can solve my question. My question is that what if the given unknown tensor have different sizes in the last dimension and I want to pad them to the same fix length, how can I do that? For example, suppose the given tensor is

[[1],
 [1, 2],
 [1, 2, 3]]

I want to pad them such that I can get

[[1, 0, 0, 0],
 [1, 2, 0, 0],
 [1, 2, 3, 0]]

The solutions in the original post all assume the last dimension have the same length. Any ideas on how to solve this problem? I am not even sure if tf.pad() is the right function to achieve this...


Solution

  • The simplest solution would be to call to_tensor() on your ragged tensor. It will automatically add padding:

    import tensorflow as tf
    
    x = tf.ragged.constant([[1], [1, 2], [1, 2, 3]])
    x = x.to_tensor()
    print(x)
    
    tf.Tensor(
    [[1 0 0]
     [1 2 0]
     [1 2 3]], shape=(3, 3), dtype=int32)
    

    If you want to, for example, to pad to the length 10 instead of the default 3, try:

    import tensorflow as tf
    
    x = tf.ragged.constant([[1], [1, 2], [1, 2, 3]])
    sequence_length = 10
    x = x.to_tensor(shape=(x.bounding_shape()[0], sequence_length))
    print(x)
    
    tf.Tensor(
    [[1 0 0 0 0 0 0 0 0 0]
     [1 2 0 0 0 0 0 0 0 0]
     [1 2 3 0 0 0 0 0 0 0]], shape=(3, 10), dtype=int32)