2
votes

This is my first time in programming with python and tensorflow. I would like to use dynamic RNN to construct sentence embeddings. Here is a part of my code written in jupyter.

graph = tf.Graph()
x_data = tf.placeholder(tf.int32, [None,None])
sequence_lengths = tf.placeholder(tf.int32, shape=[None])
embedding_mat = tf.Variable(tf.random_uniform([vocab_size,embedding_size],  0.0, 1.0),dtype=tf.float32)
embedding_output = tf.nn.embedding_lookup(embedding_mat,x_data)

with tf.variable_scope('cell_def'):
    cell =tf.contrib.rnn.GRUCell(num_units = rnn_size)
    hidden_state_in =cell.zero_state(batch_size,tf.float32) 
with tf.variable_scope('gru_def'):
    output, state = tf.nn.dynamic_rnn(cell,embedding_output,initial_state=hidden_state_in,dtype=tf.float32,sequence_length=sequence_lengths)

with tf.Session(graph=graph) as sess:
    sess.run(tf.global_variables_initializer())
    feed_dict = {x_data:embedding_output}
    sess.run(output,feed_dict=feed_dict)
    #tf.get_variable_scope().reuse_variables()    
sess.close()    

When I run my code I get this error:

Variable gru_def/rnn/gru_cell/gates/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

I try to solve this problem by calling tf.get_variable_scope().reuse_variables() to set the reuse flag to True but the error persists.

Then, I add a reuse parameter to the GRUCell but I have this error:

ValueError: Variable gru_def/rnn/gru_cell/gates/kernel does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=tf.AUTO_REUSE in VarScope?

cell = tf.contrib.rnn.GRUCell(num_units = rnn_size,reuse=True)

Can you help me please.

1

1 Answers

2
votes

In tensorflow 1.3+:

cell = tf.contrib.rnn.GRUCell(num_units=rnn_size, reuse=tf.AUTO_REUSE)