gpt4 book ai didi

tensorflow - tf.keras.layers.Conv1D 是否支持 RaggedTensor 输入?

转载 作者:行者123 更新时间:2023-12-03 23:47:29 25 4
gpt4 key购买 nike

在 tensorflow conv1D 层文档中,它说;

'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 层使用参差不齐的张量输入时,它给了我一个错误:

ValueError: Layer conv1d does not support RaggedTensors as input.



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

谢谢,

最佳答案

在此处为社区提供答案,即使答案已出现在评论部分。
tf.keras.layers.Conv1D不支持不规则张量,您可以使用 tf.keras.preprocessing.sequence.pad_sequences 填充序列并将其用作 Conv1D 层的输入。

这是 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)



你也可以做一个固定长度的填充,改变填充值,然后像下面这样填充:
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)

关于tensorflow - tf.keras.layers.Conv1D 是否支持 RaggedTensor 输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61771605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com