繁体   English   中英

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

[英]How do I set the initial state of a keras.layers.RNN instance?

我使用以下循环创建了一个堆叠的 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)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM