tensorflowconv-neural-networkragged

Does tf.keras.layers.Conv1D support RaggedTensor input?


In the tensorflow conv1D layer documentation, it says that;

'When using this layer as the first layer in a model, provide an input_shape argument (tuple of integers or None, e.g. (10, 128) for sequences of 10 vectors of 128-dimensional vectors, or (None, 128) for variable-length sequences of 128-dimensional vectors.'

So I understand that we can input variable length sequences but when I use a ragged tensor input for conv1D layer, it gives me an error:

ValueError: Layer conv1d does not support RaggedTensors as input.

What is really meant with variable length sequences if not RaggedTensors?

Thank you,


Solution

  • Providing an answer here for the community, even if the answer is already present in the comment section.

    tf.keras.layers.Conv1D does not support Ragged Tensors instead you can pad the sequences using tf.keras.preprocessing.sequence.pad_sequences and use it as an input to the Conv1D layer.

    Here is the example to pad_sequenes.

    sequence = [[1], [2, 3], [4, 5, 6]]
    tf.keras.preprocessing.sequence.pad_sequences(sequence)
    

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

    You can also do a fixed-length padding, change the padding values, and post padding like below:

    sequence = [[1], [2, 3], [4, 5, 6]]
    tf.keras.preprocessing.sequence.pad_sequences(sequence,maxlen=2,value=-1,padding="post")  
    

    array([[ 1, -1],[ 2, 3],[ 5, 6]], dtype=int32)