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

ⅰ亾dé卋堺 提交于 2021-01-27 13:04:47

问题


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,


回答1:


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)



来源:https://stackoverflow.com/questions/61771605/does-tf-keras-layers-conv1d-support-raggedtensor-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!