繁体   English   中英

使用 Keras 注意力连接序列 2 序列模型中的层形状错误

[英]Concatenate layer shape error in sequence2sequence model with Keras attention

我正在尝试使用 Colab 中的 Keras 实现一个简单的单词级序列到序列模型。 我正在使用 Keras 注意层。 这是模型的定义:

embedding_size=200
UNITS=128

encoder_inputs = Input(shape=(None,), name="encoder_inputs")

encoder_embs=Embedding(num_encoder_tokens, embedding_size, name="encoder_embs")(encoder_inputs)

#encoder lstm
encoder = LSTM(UNITS, return_state=True, name="encoder_LSTM") #(encoder_embs)
encoder_outputs, state_h, state_c = encoder(encoder_embs)

encoder_states = [state_h, state_c]

decoder_inputs = Input(shape=(None,), name="decoder_inputs")
decoder_embs = Embedding(num_decoder_tokens, embedding_size, name="decoder_embs")(decoder_inputs)

#decoder lstm
decoder_lstm = LSTM(UNITS, return_sequences=True, return_state=True, name="decoder_LSTM")
decoder_outputs, _, _ = decoder_lstm(decoder_embs, initial_state=encoder_states)

attention=Attention(name="attention_layer")
attention_out=attention([encoder_outputs, decoder_outputs])

decoder_concatenate=Concatenate(axis=-1, name="concat_layer")([decoder_outputs, attention_out])
decoder_outputs = TimeDistributed(Dense(units=num_decoder_tokens, 
                                  activation='softmax', name="decoder_denseoutput"))(decoder_concatenate)

model=Model([encoder_inputs, decoder_inputs], decoder_outputs, name="s2s_model")
model.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['accuracy'])

model.summary()

模型编译很好,没有任何问题。 编码器和解码器的输入和输出形状为:

Encoder training input shape:  (4000, 21)
Decoder training input shape:  (4000, 12)
Decoder training target shape:  (4000, 12, 3106)
--
Encoder test input shape:  (385, 21)

这是 model.fit 代码:

model.fit([encoder_training_input, decoder_training_input], decoder_training_target,
      epochs=100,
      batch_size=32,
      validation_split=0.2,)

当我运行 fit 阶段时,我从 Concatenate 层收到此错误:

ValueError: Dimension 1 in both shapes must be equal, but are 12 and 32. 
Shapes are [32,12] and [32,32]. for '{{node s2s_model/concat_layer/concat}} = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32](s2s_model/decoder_LSTM/PartitionedCall:1,
s2s_model/attention_layer/MatMul_1, s2s_model/concat_layer/concat/axis)' with input shapes: [32,12,128], [32,32,128], [] and with computed input tensors: input[2] = <2>.

因此,前 32 个是batch_size ,128 个是来自decoder_outputsattention_out输出形状,12 个是解码器输入的标记数。 我不明白如何解决这个错误,我无法改变我认为的输入令牌的数量,对我有什么建议吗?

感谢@Majitsima 解决了这个问题。 我交换了注意力层的输入,而不是

attention=Attention(name="attention_layer")
attention_out=attention([encoder_outputs, decoder_outputs])

输入是

attention=Attention(name="attention_layer")
attention_out=attention([decoder_outputs, encoder_outputs])

decoder_concatenate=Concatenate(axis=-1, name="concat_layer")([decoder_outputs, attention_out])

现在一切似乎都正常了,再次感谢@Majitsima,希望这能有所帮助!

将连接层中的axis=-1 替换为axis=1。 本文档中的示例应阐明原因。

您的问题在于传递给串联的输入。 您需要指定正确的轴来连接两个不同形状的矩阵或张量,因为它们在 Tensorflow 中被调用。 形状 [32, 12, 128] 和 [32, 32, 128] 在通过传递 1 引用的第二个维度上有所不同(因为维度从 0 开始向上)。 这将导致形状 [32, (12+32), 128],增加第二维中的元素。

当您将轴指定为 -1(默认值)时,您的连接层在使用前基本上会展平输入,在您的情况下,由于尺寸差异,这不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM