Closing a session in a tensor stream does not reset graph

The number of nodes available in the current graph increases at each iteration. This seems unintuitive because the session is closed and all of its resources must be freed. What is the reason that previous nodes are still preserved 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 
+9
source share
3 answers

The final session does not have a graphic design reset. If you want to reset the 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())) 
+14
source

I encountered problems closing the session when starting the TensorFlow program from Spyder. It seems that the RNN cells remain and strive to create new names of the same name, which creates problems. This is probably due to the fact that when starting from Spyder, the TensorFlow c-c session does not close properly, even if the program completed the "launch" from Spyder. Spyder needs to be restarted to get a new session. When you start from Spyder, setting "reuse = True" on the cells turns around this problem. However, this does not look like a valid mode in iterative programming when learning an RNN cell. In this case, some unexpected results / behavior may occur if the observer does not know what is happening.

+2
source

Let's first see what happens in tf.Session ().

This means that you send the standard def diagram to the tenorflow runtime and then allocate the GPU / CPU / Remote memory accordingly.

So, when you close the session, the runtime simply frees up all the allocated resources, but does not leave your schedule untouched!

0
source

Source: https://habr.com/ru/post/1265263/


All Articles