python - Does it really necessary to close TensorFlow session explicitly? -
the tensorflow documentation states that:
a session may own resources, such variables, queues, , readers. important release these resources when no longer required. this, either invoke close() method on session, or use session context manager.
but reading tensorflow sources found once session out of scope closed anyway.
is correct assume safety advice avoiding dangled resources exists because python gc not give guarantees when 0 referenced object deallocated , whether call destructor overall or not, because of circular references in python objects (i didn't find confirmation session has circular references though)? there other reasons close session explicitly?
import tensorflow tf import numpy np class customsession(tf.session): def __del__(self): print("{0} - deleted".format(self)) super(customsession, self).__del__() def create_session_close(name): sess = customsession(graph=tf.graph()) sess.graph.as_default(): var = tf.variable(np.arange(1e8), name=name) sess.run(tf.global_variables_initializer()) sess.run(var) sess.close() def create_session(name): sess = customsession(graph=tf.graph()) sess.graph.as_default(): var = tf.variable(np.arange(1e8), name=name) sess.run(tf.global_variables_initializer()) sess.run(var) def run_with_close(): in range(10): print(str(i)) create_session_close(str(i)) def run_without_close(): in range(10): print(str(i)) create_session(str(i))
Comments
Post a Comment