簡體   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