4

我从 Keras 站点植入了 10 分钟的 LSTM 示例,并调整网络以处理词嵌入而不是字符嵌入(来自https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-序列学习在 keras.html 中)。它工作得很好。

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

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

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

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

4

1 回答 1

1

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

于 2018-08-01T16:38:39.330 回答