为了实现多层LSTM网络,我通常使用以下代码:
def lstm_cell():
return tf.contrib.rnn.LayerNormBasicLSTMCell(model_settings['rnn_size'])
attn_cell = lstm_cell
def attn_cell():
return tf.contrib.rnn.DropoutWrapper(lstm_cell(), output_keep_prob=0.7)
cell = tf.contrib.rnn.MultiRNNCell([attn_cell() for _ in range(num_layers)], state_is_tuple=True)
outputs_, _ = tf.nn.dynamic_rnn(cell, x, dtype=tf.float32)但是,这样,如果我想要操作隐藏层输出的安排,我就无法访问隐藏层输出。在不使用tf.contrib.rnn.MultiRNNCell的情况下,有没有其他方法可以建立一个多层LSTM网络?
发布于 2020-08-08 15:16:40
您可以简单地堆叠几个LSTM层,例如通过顺序模块:
model = Sequential()
model.add(layers.LSTM(..., return_sequences = True, input_shape = (...)))
model.add(layers.LSTM(..., return_sequences = True)
...
model.add(layers.LSTM(...))在这种情况下,return sequences关键字对于中间层至关重要。
https://stackoverflow.com/questions/63315923
复制相似问题