如何设置keras.layers.RNN实例?

2024-10-02 16:33:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

当我有一个单层编码器和一个单层解码器,其中神经元的数量是相同的,这个设置就可以工作了。然而,当解码器的层数超过一层时,它不起作用。在

我收到以下错误消息:

^{pr2}$

我的解码器层列表包含条目[48,58]。因此,我的RNN层是一个堆叠的GRU,第一个GRU包含48个神经元,第二个GRU包含58个神经元。我想设置第一个GRU的初始状态。我通过一个密集的层运行状态,这样形状与解码器的第一层兼容。当我将初始状态关键字传递给解码器RNN层时,错误消息指示我正在尝试设置第一层和第二层的初始状态。这是正确的行为吗?通常我会设置第一个解码器层的初始状态(不是用这样的单元结构构建的),然后将它的输入输入输入到后续层。在创建keras.layers.RNN从LSTMCells的GRUCell列表中?在


Tags: oftheencoderlayers解码器outputshiddenkeras
1条回答
网友
1楼 · 发布于 2024-10-02 16:33:58

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

相关问题 更多 >