注意下Keras序列与序列模型中的维度不匹配

问题描述

我正在尝试使用attention构建神经机器翻译模型。我正在关注Keras blog上的教程,该教程显示了如何使用序列到序列方法(无需注意)来构建NMT模型。我通过以下方式扩展了模型以合并attention-

latent_dim = 300
embedding_dim=100
batch_size  = 128

# Encoder
encoder_inputs = keras.Input(shape=(None,num_encoder_tokens))


#encoder lstm 1
encoder_lstm = tf.keras.layers.LSTM(latent_dim,return_sequences=True,return_state=True,dropout=0.4,recurrent_dropout=0.4)
encoder_output,state_h,state_c = encoder_lstm(encoder_inputs)
print(encoder_output.shape)

# Set up the decoder,using `encoder_states` as initial state.
decoder_inputs = keras.Input(shape=(None,num_decoder_tokens))


decoder_lstm = tf.keras.layers.LSTM(latent_dim,recurrent_dropout=0.2)
decoder_output,decoder_fwd_state,decoder_back_state = decoder_lstm(decoder_inputs,initial_state=[state_h,state_c])

# Attention layer
attn_out = tf.keras.layers.Attention()([encoder_output,decoder_output])

# Concat attention input and decoder LSTM output
decoder_concat_input = tf.keras.layers.Concatenate(axis=-1,name='concat_layer')([decoder_output,attn_out])

#dense layer
decoder_dense =  tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(num_decoder_tokens,activation='softmax'))
decoder_outputs = decoder_dense(decoder_concat_input)

# Define the model 
attn_model = tf.keras.Model([encoder_inputs,decoder_inputs],decoder_outputs)

attn_model.summary()

训练模型-

attn_model.compile(
    optimizer="rmsprop",loss="categorical_crossentropy",metrics=["accuracy"]
)

history = attn_model.fit(
    [encoder_input_data,decoder_input_data],decoder_target_data,batch_size=batch_size,epochs=5,validation_split=0.2,)

我的身材矮小

encoder_input_data.shape(10000,16,71)

decoder_input_data.shape(10000,59,92)

decoder_target_data.shape(10000,92)

训练该模型时,出现以下错误: InvalidArgumentError: Dimension 1 in both shapes must be equal,but are 59 and 16. Shapes are [?,59] and [?,16]. for 'model/concat_layer/concat' (op: 'ConcatV2') with input shapes: [?,300],[?,[] and with computed input tensors: input[2] = <2>.

我了解到它在抱怨encoder_input_datadecoder_input_data的尺寸,但是当我们运行{{3 }}。在这种情况下,由于Concatenation层而引发错误。

有人可以建议如何解决此问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...