Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

closing session in tensorflow doesn't reset graph

The number of nodes available in the current graph keep increasing at every iteration. This seems unintuitive since the session is closed, and all of it's resources should be freed. What is the reason why the previous nodes are still lingering even when creating a new session? Here is my code:

for i in range(3):
    var = tf.Variable(0)
    sess = tf.Session(config=tf.ConfigProto())
    with sess.as_default():
        tf.global_variables_initializer().run()
        print(len(sess.graph._nodes_by_name.keys()))
    sess.close() 

It outputs:

5
10
15
like image 519
titus Avatar asked Mar 09 '17 22:03

titus


1 Answers

Closing session does not reset graph by design. If you want to reset graph you can either call tf.reset_default_graph() like this

for _ in range(3):
    tf.reset_default_graph()
    var = tf.Variable(0)
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        print(len(session.graph._nodes_by_name.keys()))

or you can do something like this

for _ in range(3):
    with tf.Graph().as_default() as graph:
        var = tf.Variable(0)
        with tf.Session() as session:
            session.run(tf.global_variables_initializer())
            print(len(graph._nodes_by_name.keys()))
like image 55
Mad Wombat Avatar answered Oct 02 '22 11:10

Mad Wombat