RobinHood
RobinHood

Reputation: 407

Multiple RNN in tensorflow

I'm trying to use a 2 deep layer RNN without MultiRNNCell in TensorFlow, I mean using the output of the 1layer as the input of the 2layer as:

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)

but I get the following error: "Attempt to have a second RNNCell use the weights of a variable scope that already has weights" I don't want to reuse the weights of the cell1 in the cell2, I want two differents layers because I need the outputs of each layer. How can I do it?

Upvotes: 1

Views: 874

Answers (1)

pfm
pfm

Reputation: 6328

You could put the construction of your rnn into 2 different variable scopes to ensure they use different internal variables.

E.g. by doing it explicitly

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)

or by using the scope argument of the dynamic_rnn method:

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')

Upvotes: 3

Related Questions