Purpose of using "with tf.Session()"?
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.
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 ).