17

I am practicing the keras method called concatenate.

And use of with statement in this example kind of got me thinking the purpose of this statement

The example code looks like:

import numpy as np 
import keras.backend as K
import tensorflow as tf

t1 = K.variable(np.array([ [[1, 2], [2, 3]], [[4, 4], [5, 3]]]))
t2 = K.variable(np.array([[[7, 4], [8, 4]], [[2, 10], [15, 11]]]))

d0 = K.concatenate([t1 , t2] , axis=-2)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print(sess.run(d0))

Then I check document from: tensorflow and says that:

A session may own resources, such as tf.Variable, tf.QueueBase, and tf.ReaderBase. It is important to release these resources when they are no longer required. To do this, either invoke the tf.Session.close method on the session, or use the session as a context manager.

Which I believe has already explained all of it,but can somebody give me more intuitive explanation.

Thanks in advance and have a nice day!

Pro_gram_mer
  • 749
  • 3
  • 7
  • 20
  • 3
    I'm not sure what could be more intuitive. The point is, like opening files which use resources, you need to remember to close them again at the end. By indenting your code to be within the `with` block, the "closing" part is all covered for you. As soon as you get out of that level of indentation, everything is torn down for you automatically, and all of the resources are released. – roganjosh Dec 21 '18 at 13:12
  • A ridiculous analogy is remembering to turn the lights off when you leave a room. Now you don't have to, the `with` context manager does it for you :) – roganjosh Dec 21 '18 at 13:17
  • @roganjosh but there's nothing happen to t1,t2 and d0 even we leave the block ,right? – Pro_gram_mer Dec 21 '18 at 13:46

2 Answers2

18

tf.Session() initiates a TensorFlow Graph object in which tensors are processed through operations (or ops). The with block terminates the session as soon as the operations are completed. Hence, there is no need for calling Session.close. Also, a session contains variables, global variables, placeholders, and ops. These have to be initiated once the session is created. Hence we call tf.global_variables_initializer().run()

A graph contains tensors and operations. To initiate a graph, a session is created which runs the graph. In other words, graph provides a schema whereas a session processes a graph to compute values( tensors ).

Blade
  • 984
  • 3
  • 12
  • 34
  • 1
    Thanks!you kick off great start for me to understand this,but it still got me thinking deeper!So the variable would not be affected no matter the session has been closed or not,right? So what is the point doing this thing( using with statement) – Pro_gram_mer Dec 21 '18 at 18:01
  • The variables could be initiated in a other Session as well. `with` statement automatically closes the session and therefore there is no need to call `session.close`. –  Dec 22 '18 at 02:11
  • So the whole point is just to skip one step that is session.close?Nothing to do with some resource release? – Pro_gram_mer Dec 22 '18 at 07:24
  • `session.close` deactivates the graph which could be again reinstantiated. Variables remain with the graph and remain unchanged. The graph could be instantiated in an another Session. –  Dec 22 '18 at 07:58
7

The tensorflow documentation is very specific about this.

Since a tf.Session owns physical resources (such as GPUs and network connections), it is typically used as a context manager (in a with block) that automatically closes the session when you exit the block.

It is also possible to create a session without using a with block, but you should explicitly call tf.Session.close when you are finished with it to free the resources.

Community
  • 1
  • 1
Leander
  • 508
  • 6
  • 21