张量流中的多个 RNN

Multiple RNN in tensorflow

我正在尝试在 TensorFlow 中使用没有 MultiRNNCell 的 2 深层 RNN,我的意思是使用 1layer 的输出作为 2layer 的输入:

cell1 = tf.contrib.rnn.LSTMCell(num_filters, state_is_tuple=True)
rnn_outputs1, _ = tf.nn.dynamic_rnn(cell1, inputs, dtype = tf.float32)
cell2 = tf.contrib.rnn.LSTMCell(num_filters, state_is_tuple=True)
rnn_outputs2, _ = tf.nn.dynamic_rnn(cell2, rnn_outputs1, dtype = tf.float32)

但我收到以下错误:"Attempt to have a second RNNCell use the weights of a variable scope that already has weights" 我不想在 cell2 中重用 cell1 的权重,我想要两个不同的层,因为我需要每一层的输出。我该怎么做?

您可以将 rnn 的构造放入 2 个不同的变量范围以确保它们使用不同的内部变量。

例如通过明确地做

cell1 = tf.contrib.rnn.LSTMCell(num_filters, state_is_tuple=True)
with tf.variable_scope("rnn1"):
    rnn_outputs1, _ = tf.nn.dynamic_rnn(cell1, inputs, dtype = tf.float32)
cell2 = tf.contrib.rnn.LSTMCell(num_filters, state_is_tuple=True)
with tf.variable_scope("rnn2"):
    rnn_outputs2, _ = tf.nn.dynamic_rnn(cell2, rnn_outputs1, dtype = tf.float32)

或使用 dynamic_rnn 方法的 scope 参数:

cell1 = tf.contrib.rnn.LSTMCell(num_filters, state_is_tuple=True)
rnn_outputs1, _ = tf.nn.dynamic_rnn(cell1, inputs, dtype=tf.float32, scope='rnn1')
cell2 = tf.contrib.rnn.LSTMCell(num_filters, state_is_tuple=True)
rnn_outputs2, _ = tf.nn.dynamic_rnn(cell2, rnn_outputs1, dtype=tf.float32, scope='rnn2')