title: How can I create tensorflow model in a for loop? I keep getting error "Variable weight_enc already exists, disallowed"
body: <p>The following is part of my code creating the model. When I run this in a for-loop, in the second loop it reports error, says <code>Variable weight_enc already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?</code></p>
  <pre><code>p_input = tf.placeholder(tf.float32, shape=(None, _abnormal_data.shape[1]))
  p_input_reshape = tf.reshape(p_input, [-1, _abnormal_data.shape[1]])
  
  # initialize weights randomly from a Gaussian distribution
  # create the initializer for weights
  weight_initer = tf.truncated_normal_initializer(mean=0.0, stddev=0.01)
  
  w_enc = tf.get_variable(name=&quot;weight_enc&quot;, dtype=tf.float32, shape=[_abnormal_data.shape[1], _hidden_num],
                          initializer=weight_initer)
  w_dec = tf.get_variable(name=&quot;weight_dec&quot;, dtype=tf.float32, shape=[_hidden_num, _abnormal_data.shape[1]],
                          initializer=weight_initer)
  
  w_enc_sparse = tf.convert_to_tensor(np.random.randint(2, size=(_abnormal_data.shape[1], _hidden_num)),
                                      dtype=tf.float32)
  w_dec_sparse = tf.convert_to_tensor(np.random.randint(2, size=(_hidden_num, _abnormal_data.shape[1])),
                                      dtype=tf.float32)
  </code></pre>
  <p>I understand that this is because I did not clear the graph after the first loop and in the second loop, the variable is already created in the first loop, so that when I want to create it for the second time, it is rejected by tensorflow.</p>
  <p>But due to the requirement of my job, I do hope to run this code in the for-loop. Is there anyway I can achieve it? I think there should be some way but I can't find it.</p>
  <p>I tried use <code>tf.reset_default_graph()</code> in the loop but it does not help.</p>
  <p>Thank you all for helping me!!!</p>