Tensorflow: saving and resoring session - multiple variables
<p>Given the following code:</p>

<pre><code>import tensorflow as tf

with tf.Session() as sess:
    var = tf.Variable(42, name='var')
    sess.run(tf.global_variables_initializer())
    tf.train.export_meta_graph('file.meta')

with tf.Session() as sess:
    saver = tf.train.import_meta_graph('file.meta')
    print sess.run(var)
</code></pre>

<p>I get an error at the line <code>saver = tf.train.import_meta_graph('file.meta')</code> saying <code>ValueError: At least two variables have the same name: var</code>.</p>

<p>How can I get around this? Is there anyway to <em>overwrite</em> the compute graph when importing a metagraph?</p>

<p><strong>EDIT:</strong></p>

<p>I have arrived at the following code:</p>

<pre><code>import tensorflow as tf

file_name = "./file"

with tf.Session() as sess:
    var = tf.Variable(42, name='my_var')
    sess.run(tf.global_variables_initializer())

    saver = tf.train.Saver()
    saver.save(sess,file_name)
    saver.export_meta_graph(file_name + '.meta')

with tf.Session() as sess:
    saver = tf.train.import_meta_graph(file_name + '.meta')
    saver.restore(sess, file_name)
    print(sess.run(var))

    # new code that fails:
    saver = tf.train.Saver()
    saver.save(sess,file_name)
    saver.export_meta_graph(file_name + '.meta')
</code></pre>

<p>This prints the correct value for <code>var</code>, but when I come to save the graph a second time, I get the same original error: <code>ValueError: At least two variables have the same name: var</code></p>