在 Keras 中使用 GRU 实现 Seq2Seq

Rid*_*bic 5 python lstm keras gated-recurrent-unit keras-layer

我植入了 Keras 站点上的十分钟 LSTM 示例,并调整了网络以处理单词嵌入而不是字符嵌入(来自https://blog.keras.io/a-ten-month-introduction-to-sequence-to-序列学习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())
Run Code Online (Sandbox Code Playgroud)

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())
Run Code Online (Sandbox Code Playgroud)

我用“#这是错误出现的地方”标记了错误的发生。

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

小智 2

decoder_statesLSTM 代码中是一个列表,因此您可以将列表添加到列表中,从而生成组合列表。但在 GRU 代码中,decoder_statesGRU 层的输出将具有不同的类型。没有完整的代码会使调试变得更加困难,但请尝试以下操作:[decoder_outputs] + [decoder_states]) # Notice brackets around decoder_states