输入到编解码器LSTMCell / RNN网络

时间:2019-04-06 05:40:33

标签: neural-network lstm recurrent-neural-network decoder encoder-decoder

我正在按照此处提供的代码,使用Keras创建一个LSTM编码器-解码器网络:https://github.com/LukeTonin/keras-seq-2-seq-signal-prediction。我所做的唯一更改是用LSTMCell替换了GRUCell。基本上,编码器和解码器均由35 LSTMCells的2层组成。使用RNN层将这些层相互堆叠(并合并在一起)。

LSTMCell返回2状态,而GRUCell返回1状态。这是我遇到错误的地方,因为我不知道如何为LSTMCell的2个返回状态编写代码。

我创建了两个模型:首先,是编码器-解码器模型。第二,预测模型。我在编码器-解码器模型中没有遇到任何问题,但是在预测模型的解码器中遇到了问题。

我得到的错误是: ValueError: Layer rnn_4 expects 9 inputs, but it received 3 input tensors. Input received: [<tf.Tensor 'input_4:0' shape=(?, ?, 1) dtype=float32>, <tf.Tensor 'input_11:0' shape=(?, 35) dtype=float32>, <tf.Tensor 'input_12:0' shape=(?, 35) dtype=float32>]

在预测模型中运行以下行时,会发生此错误:

decoder_outputs_and_states = decoder(
        decoder_inputs, initial_state=decoder_states_inputs)

适合的代码部分是:

encoder_predict_model = keras.models.Model(encoder_inputs,
                                           encoder_states)

decoder_states_inputs = []

# Read layers backwards to fit the format of initial_state
# For some reason, the states of the model are order backwards (state of the first layer at the end of the list)
# If instead of a GRU you were using an LSTM Cell, you would have to append two Input tensors since the LSTM has 2 states.
for hidden_neurons in layers[::-1]:
    # One state for GRU, but two states for LSTMCell
    decoder_states_inputs.append(keras.layers.Input(shape=(hidden_neurons,)))
decoder_outputs_and_states = decoder(
        decoder_inputs, initial_state=decoder_states_inputs)

decoder_outputs = decoder_outputs_and_states[0]
decoder_states = decoder_outputs_and_states[1:]

decoder_outputs = decoder_dense(decoder_outputs)

decoder_predict_model = keras.models.Model(
        [decoder_inputs] + decoder_states_inputs,
        [decoder_outputs] + decoder_states)

有人可以帮我解决上面的for循环吗,在那之后我应该通过解码器的初始状态?

1 个答案:

答案 0 :(得分:0)

我有一个类似的错误,我按照他说的解决了,添加了另一个输入张量:

# If instead of a GRU you were using an LSTM Cell, you would have to append two Input tensors since the LSTM has 2 states.
for hidden_neurons in layers[::-1]:
    # One state for GRU
    decoder_states_inputs.append(keras.layers.Input(shape=(hidden_neurons,)))
    decoder_states_inputs.append(keras.layers.Input(shape=(hidden_neurons,)))

它解决了这个问题...