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
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()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With