我收到这样的错误:“ ValueError:传递了与initial_state
不兼容的cell.state_size
。收到的state_spec
= [InputSpec(shape =(32,512),ndim = 2),InputSpec(shape =(32,512),ndim = 2)];但是cell.state_size
是[[512,512]]“。我的问题通常是,cell.state_size
是[512, 512]
,[[512, 512]]
怎么发生?我的代码段如下所示。谢谢。
rnn_cells = [get_rnn_cell_from_keras(rnn_cell_str, rnn_layer_dim) for _ in range(rnn_layer_num)]
stacked_rnn_cells = tf.keras.layers.StackedRNNCells(rnn_cells)
rnn_layers = tf.keras.layers.RNN(stacked_rnn_cells, return_state=True)
h_state, c_state = [tf.zeros([batch_size, self.rnn_layer_dim], dtype=tf.float32), tf.zeros([batch_size, self.rnn_layer_dim], dtype=tf.float32)]
state_prev = [h_state, c_state]
rnn_output = rnn_layers(vector_prev, initial_state=hidden_state_prev)
def get_rnn_cell_from_keras(rnn_cell_str, rnn_layer_dim):
rnn_cell_str = rnn_cell_str.lower()
if rnn_cell_str == 'lstm': # Fast unidirectional LSTM implementation backed by cuDNN
rnn_cell = tf.keras.layers.LSTMCell(rnn_layer_dim)
else:
raise Exception("{} is not supported. Please choose eligible one from ['LSTM']")
return rnn_cell