在Keras中使用GRU实现Seq2Seq

时间:2018-07-30 18:34:51

标签: python keras lstm keras-layer gated-recurrent-unit

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

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

尺寸必须相等,但输入形状为[1,?,?,232],[?, 256]的“ add”(op:“ Add”)的尺寸必须为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())

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

感谢您可以提供的任何帮助,是的,我需要尝试使用这两个系统以利用给定的数据集来弥补它们之间的差异。

1 个答案:

答案 0 :(得分:0)

LSTM代码中的

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