tf.keras.layers.Conv1D是否支持RaggedTensor输入? [英] Does tf.keras.layers.Conv1D support RaggedTensor input?

查看:97
本文介绍了tf.keras.layers.Conv1D是否支持RaggedTensor输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在tensorflow conv1D层文档中,它表示;

In the tensorflow conv1D layer documentation, it says that;

'当将此层用作模型的第一层时,请提供input_shape参数(整数或无的元组,例如(10,128)128维向量的10个向量的序列,或者(无,128)128维向量的可变长度序列.'

'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.'

所以我知道我们可以输入可变长度的序列,但是当我在conv1D层上使用参差不齐的张量输入时,会给我一个错误:

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:层conv1d不支持RaggedTensors作为输入.

ValueError: Layer conv1d does not support RaggedTensors as input.

如果不是RaggedTensors,那么可变长度序列的真正含义是什么?

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

谢谢

推荐答案

在此处为社区提供答案,即使答案部分中已经存在答案.

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

tf.keras.layers.Conv1D 不支持参差不齐的张量,而您可以使用 tf.keras.preprocessing.sequence.pad_sequences 填充序列并将其用作输入到Conv1D层.

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.

这是pad_sequenes的示例.

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)

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)

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

这篇关于tf.keras.layers.Conv1D是否支持RaggedTensor输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆