在 Keras 中使用 GRU 实现 Seq2Seq

Implementing Seq2Seq with GRU in Keras

我从 Keras 站点植入了 10 分钟的 LSTM 示例,并调整了网络以处理词嵌入而不是字符嵌入(来自 https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html)。效果很好。

但现在我很难使用 GRU 而不是 LSTM。 调整变量后,编译和训练(拟合函数)开始工作。但是当我尝试使用网络通过自定义输入对其进行测试时,它会抛出:

尺寸必须相等,但对于 'add'(运算符:'Add'),尺寸为 232 和 256,输入形状为:[1,?,?,232],[ ?,256]

LSTM的相关工作代码是:

encoder_inputs = Input(shape=(None, num_encoder_tokens), name="Encoder_Input")
encoder = LSTM(latent_dim, return_state=True, name="Encoder_LSTM")
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]
decoder_inputs = Input(shape=(None, num_decoder_tokens), name="Decoder_Input")
decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True, name="Decoder_LSTM")

decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
                                     initial_state=encoder_states)

decoder_dense = Dense(num_decoder_tokens, activation='softmax', name="DecoderOutput")
decoder_outputs = decoder_dense(decoder_outputs)

model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

result = model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
          batch_size=batch_size,
          epochs=epochs,
          validation_split=0.2)

encoder_model = Model(encoder_inputs, encoder_states)
decoder_state_input_h = Input(shape=(latent_dim,))
decoder_state_input_c = Input(shape=(latent_dim,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(
    decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model(
    [decoder_inputs] + decoder_states_inputs,
    [decoder_outputs] + decoder_states)
reverse_target_word_index = dict(
    (i, word) for word, i in target_token_index.items())

GRU 代码是:

encoder_inputs = Input(shape=(None, num_encoder_tokens), name="Encoder_Input")
encoder = GRU(latent_dim, return_state=True, name="Encoder_GRU")
_, encoder_state = encoder(encoder_inputs)
decoder_inputs = Input(shape=(None, num_decoder_tokens), name="Decoder_Input")
decoder_gru = GRU(latent_dim, return_sequences=True, return_state=True, name="Decoder_GRU")

decoder_outputs, _ = decoder_gru(decoder_inputs, initial_state=encoder_state)

decoder_dense = Dense(num_decoder_tokens, activation='softmax', name="DecoderOutput")
decoder_outputs = decoder_dense(decoder_outputs)

model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

result = model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
          batch_size=batch_size,
          epochs=epochs,
          validation_split=0.2)

encoder_model = Model(encoder_inputs, encoder_state)
decoder_states_inputs = Input(shape=(latent_dim,))
decoder_outputs, decoder_states = decoder_gru(
    decoder_inputs, initial_state=decoder_states_inputs)
decoder_outputs = decoder_dense(decoder_outputs)

decoder_model = Model(
    [decoder_inputs] + decoder_states_inputs,
    [decoder_outputs] + decoder_states) # This is where the error appears

reverse_input_word_index = dict(
    (i, word) for word, i in input_token_index.items())
reverse_target_word_index = dict(
    (i, word) for word, i in target_token_index.items())

我用“# This is where the error appears”标记了错误的发生。

感谢您提供的任何帮助,是的,我需要尝试使用这两个系统来比较它们与给定数据集的差异。

decoder_states 在您的 LSTM 代码中是一个列表,因此您将列表添加到列表中,从而生成组合列表。但是在 GRU 代码中,你有 decoder_states 作为 GRU 层的输出,它将具有不同的类型。没有完整的代码会使调试更加困难,但试试这个:[decoder_outputs] + [decoder_states]) # Notice brackets around decoder_states