tf.keras嵌入mask_zero = True,然后跟随GlobalAveragePooling1D生成TypeError

问题描述 投票:0回答:1

我正在使用tf.keras在google colab上使用tensorflow v2。我正在尝试使用掩盖嵌入,然后使用全局平均值。这是我的代码:

vocab_size = 1500

inputs = Input(shape=(None,), dtype=tf.int32, name='word_sequence')

x = Embedding(input_dim=vocab_size, output_dim=16, mask_zero=True)(inputs)

outputs = tf.keras.layers.GlobalAveragePooling1D()(x)

model = Model(inputs, outputs)

但是我遇到了这个错误:

TypeError:无法将类型的对象转换为Tensor。内容:[-1,无,1]。考虑将元素强制转换为受支持的类型。

如果我提供明确的输入序列长度Input(shape =(10,),.....),那么它似乎没有错误(尽管我没有使用示例数据对其进行测试)。我想知道为什么您需要指定一个明确的序列长度,我认为当层首次遇到数据时可以在运行时懒惰地完成。

此外,以下作品(摘自“屏蔽和填充” tf教程:]

inputs = tf.keras.Input(shape=(None,), dtype='int32')
x = layers.Embedding(input_dim=5000, output_dim=16, mask_zero=True)(inputs)
outputs = layers.LSTM(32)(x)

model = tf.keras.Model(inputs, outputs)

对于LSTM,似乎在模型的功能性api构造期间对None的输入形状感到满意。

有人可以解释一下GlobalAveragePooling1D有什么不好,还是应该可以,但是我做错了?

谢谢。

keras mask embedding tf.keras max-pooling
1个回答
0
投票

我不具有添加评论的名声,所以这就是我想说的:对于GRU和LSTM,我似乎都存在相同的问题。当我改用GlobalMaxPooling1D时,问题似乎消失了。我觉得这是由Masking的基础实现引起的问题,但是我对低级Keras API一无所知。

© www.soinside.com 2019 - 2024. All rights reserved.