我试图在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)
但是我得到以下错误:"尝试让第二个RNNCell使用已经具有权重的变量范围的权重" 我不想在cell2中重用cell1的权重,我想要两个不同的层,因为我需要每层的输出。我该怎么办?
答案 0 :(得分:3)
您可以将rnn
的构造放入2个不同的变量范围,以确保它们使用不同的内部变量。
E.g。通过明确地做到
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)
或使用scope
方法的dynamic_rnn
参数:
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')