How to use tf.reset_default_graph()

With TensorFlow 2.0 coming out, now it's better to use tf.compat.v1.reset_default_graph() in order to avoid getting warning. Link to the documentation: https://www.tensorflow.org/api_docs/python/tf/compat/v1/reset_default_graph


For some reason, I need to build a new graph FOR LOTS OF TIMES, and I have just tested, which works eventually! Many thanks for Salvador Dali's answer:-)

import tensorflow as tf
from my_models import Classifier

for i in range(10):
    tf.reset_default_graph()
    # build the graph
    global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)
    classifier = Classifier(global_step)
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        print("do sth here.")

This is probably how you use it:

import tensorflow as tf
a = tf.constant(1)
with tf.Session() as sess:
    tf.reset_default_graph()

You get an error because you use it in a session. From the tf.reset_default_graph() documentation:

Calling this function while a tf.Session or tf.InteractiveSession is active will result in undefined behavior. Using any previously created tf.Operation or tf.Tensor objects after calling this function will result in undefined behavior


tf.reset_default_graph() can be helpful (at least for me) during the testing phase while I experiment in jupyter notebook. However, I have never used it in production and do not see how it would be helpful there.

Here is an example that could be in a notebook:

import tensorflow as tf
# create some graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print sess.run(...)

Now I do not need this stuff anymore, but if I create another graph and visualize it in tensorboard I will see old nodes and the new nodes. To solve this, I could restart the kernel and run only the next cell. However, I can just do:

tf.reset_default_graph()
# create a new graph
with tf.Session() as sess:
    print sess.run(...)

Edit after OP added his code:

with tf.name_scope("predict"):
    tf.reset_default_graph()

Here is what approximately happens. Your code fails because tf.name_scope already added something to a graph. While being inside of this "adding something to the graph", you tell TF to remove the graph completely, but it can't because it is busy adding something.