如何设置keras.layers.RNN实例的初始状态?

时间:2019-02-07 06:41:02

标签: python tensorflow keras recurrent-neural-network gated-recurrent-unit

我使用以下循环创建了一个堆叠的keras解码器模型:

# Create the encoder
# Define an input sequence.
encoder_inputs = keras.layers.Input(shape=(None, num_input_features))

# Create a list of RNN Cells, these are then concatenated into a single layer with the RNN layer.
encoder_cells = []
for hidden_neurons in hparams['encoder_hidden_layers']:
    encoder_cells.append(keras.layers.GRUCell(hidden_neurons,
                                              kernel_regularizer=regulariser,
                                              recurrent_regularizer=regulariser,
                                              bias_regularizer=regulariser))

encoder = keras.layers.RNN(encoder_cells, return_state=True)

encoder_outputs_and_states = encoder(encoder_inputs)

# Discard encoder outputs and only keep the states. The outputs are of no interest to us, the encoder's job is to create 
# a state describing the input sequence.
encoder_states = encoder_outputs_and_states[1:]
print(encoder_states)
if hparams['encoder_hidden_layers'][-1] != hparams['decoder_hidden_layers'][0]:
    encoder_states = Dense(hparams['decoder_hidden_layers'][0])(encoder_states[-1])

# Create the decoder, the decoder input will be set to zero
decoder_inputs = keras.layers.Input(shape=(None, 1))

decoder_cells = []
for hidden_neurons in hparams['decoder_hidden_layers']:
    decoder_cells.append(keras.layers.GRUCell(hidden_neurons,
                                              kernel_regularizer=regulariser,
                                              recurrent_regularizer=regulariser,
                                              bias_regularizer=regulariser))

decoder = keras.layers.RNN(decoder_cells, return_sequences=True, return_state=True)

# Set the initial state of the decoder to be the output state of the encoder. his is the fundamental part of the 
# encoder-decoder.
decoder_outputs_and_states = decoder(decoder_inputs, initial_state=encoder_states)

# Only select the output of the decoder (not the states)
decoder_outputs = decoder_outputs_and_states[0]

# Apply a dense layer with linear activation to set output to correct dimension and scale (tanh is default activation for
# GRU in Keras
decoder_dense = keras.layers.Dense(num_output_features,
                                   activation='linear',
                                   kernel_regularizer=regulariser,
                                   bias_regularizer=regulariser)

decoder_outputs = decoder_dense(decoder_outputs)

model = keras.models.Model(inputs=[encoder_inputs, decoder_inputs], outputs=decoder_outputs)
model.compile(optimizer=optimiser, loss=loss)
model.summary()

当我具有神经元数量相同的单层编码器和单层解码器时,此设置有效。但是,当解码器的层数超过一层时,它将不起作用。

我收到以下错误消息:

ValueError: An `initial_state` was passed that is not compatible with `cell.state_size`. Received `state_spec`=[InputSpec(shape=(None, 48), ndim=2)]; however `cell.state_size` is (48, 58)

我的coder_layers列表包含条目[48,58]。因此,我的由解码器组成的RNN层是一个堆叠的GRU,其中第一个GRU包含48个神经元,第二个GRU包含58个神经元。我想设置第一个GRU的初始状态。我通过密集层运行状态,以便形状与解码器的第一层兼容。错误消息表明,当我将初始状态关键字传递给解码器RNN层时,我正在尝试设置第一层和第二层的初始状态。这是正确的行为吗?通常,我将设置第一个解码器层的初始状态(不使用像这样的单元结构构建),然后将其输入馈送到后续层。从LSTMCells的GRUCell列表创建keras.layers.RNN时,默认情况下是否有办法在keras中实现这种行为?

1 个答案:

答案 0 :(得分:0)

在我自己的实验中,您的intial_states应该以batch_size作为其第一维。换句话说,一批中的每个元素可以具有不同的初始状态。从您的代码中,我认为您错过了这个维度。