TensorFlow动态RNN构建句嵌入时变量重用报错求助
Hey there! Let's work through this problem step by step—you're close, just a couple of key issues to iron out.
First, Let's Diagnose the Errors
Your first error (Variable gru_def/rnn/gru_cell/gates/kernel already exists) and the follow-up reuse error both stem from two main issues:
- A mistake in your
feed_dict(you're trying to feed a graph tensor to a placeholder, which doesn't work) - Unnecessary manual handling of the RNN cell's initial state, which creates conflicting variable scopes.
Let's Fix the Code
Here's a corrected version with explanations:
import tensorflow as tf import numpy as np # Define your hyperparameters first (adjust these to your use case) vocab_size = 1000 embedding_size = 128 rnn_size = 256 batch_size = 32 max_sequence_length = 20 # Create a fresh graph to avoid variable conflicts (critical in Jupyter!) graph = tf.Graph() with graph.as_default(): # Placeholders for your input data (these expect numpy arrays, not graph tensors) x_data = tf.placeholder(tf.int32, [None, None], name="input_word_indices") # Shape: [batch_size, sequence_length] sequence_lengths = tf.placeholder(tf.int32, shape=[None], name="sequence_lengths") # Shape: [batch_size] # Embedding layer: map word indices to dense vectors 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) # Shape: [batch_size, sequence_length, embedding_size] # Define and run the GRU RNN with tf.variable_scope('gru_sentence_encoder'): # Create the GRU cell (no need for manual reuse here unless you're reusing the cell elsewhere) cell = tf.contrib.rnn.GRUCell(num_units=rnn_size) # Let dynamic_rnn handle the initial state automatically (it calls cell.zero_state internally) # This avoids scope conflicts from manually creating hidden_state_in output, final_state = tf.nn.dynamic_rnn( cell, inputs=embedding_output, sequence_length=sequence_lengths, dtype=tf.float32 ) # Initialize all variables init_op = tf.global_variables_initializer() # Run the session with valid input data with tf.Session(graph=graph) as sess: sess.run(init_op) # Generate fake input data (replace this with your actual preprocessed sentences) fake_word_indices = np.random.randint(0, vocab_size, size=(batch_size, max_sequence_length)) fake_seq_lengths = np.random.randint(1, max_sequence_length + 1, size=(batch_size,)) # Correct feed_dict: pass numpy arrays (not graph tensors!) to placeholders feed_dict = { x_data: fake_word_indices, sequence_lengths: fake_seq_lengths } # Get the RNN outputs and final state (this is your sentence embedding!) rnn_outputs, sentence_embedding = sess.run([output, final_state], feed_dict=feed_dict) print(f"RNN output shape: {rnn_outputs.shape}") # [batch_size, max_sequence_length, rnn_size] print(f"Sentence embedding shape: {sentence_embedding.shape}") # [batch_size, rnn_size]
Key Fixes Explained
Fixed
feed_dict: You can't feedembedding_output(a tensor inside your graph) tox_data—placeholders expect external data like numpy arrays (your actual word index sequences). The fake data in the example mimics what you'd pass with real preprocessed sentences.Removed manual initial state: By letting
tf.nn.dynamic_rnnhandle the initial state, you avoid creating conflicting variable scopes. Previously, yourhidden_state_inwas defined in thecell_defscope, while the RNN ran ingru_def—this caused TensorFlow to try creating duplicate cell variables.Clean variable scoping: Putting the cell definition and
dynamic_rnncall inside the same variable scope ensures all RNN variables are created in one place, no reuse confusion needed (unless you explicitly want to reuse the cell later, in which case you'd usereuse=tf.AUTO_REUSE).
Avoiding Jupyter-Specific Issues
If you're running this in Jupyter, make sure to:
- Either wrap everything in a
tf.Graph()context (like we did) - Or add
tf.reset_default_graph()at the start of your code block to clear old variables between runs.
That should resolve both the variable reuse error and the feed_dict mistake. Your final GRU state (sentence_embedding) is now a valid fixed-size embedding for each input sentence!
内容的提问来源于stack exchange,提问作者AmalB

