In which cases should tf.Session()
and tf.InteractiveSession()
be considered for what purpose?
When I tried to use the former one, some functions (for example, .eval()
) didn't work, and when I changed to the later one, it worked.
tensorflow - The name tf. Session is deprecated. Please use tf. compat.
A session allows to execute graphs or part of graphs. It allocates resources (on one or more machines) for that and holds the actual values of intermediate results and variables.
TensorFlow Session is a session object which encapsulates the environment in which Operation objects are executed, and data objects are evaluated. TensorFlow requires a session to execute an operation and retrieve its calculated value. A session may own several resources, for example, tf. QueueBase, tf.
See Migration guide for more details. tf.InteractiveSession ( target='', graph=None, config=None ) The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. The methods tf.Tensor.eval and tf.Operation.run will use that session to run ops.
A TensorFlow Session for use in interactive contexts, such as a shell. See Migration guide for more details. tf.InteractiveSession ( target='', graph=None, config=None ) The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction.
The methods Tensor.eval () and Operation.run () will use that session to run ops. It is also possible to say, that InteractiveSession supports less typing, as allows to run variables without needing to constantly refer to the session object.
Alternatively, you can use with tf.compat.v1.Session (): to create a session that is automatically closed on exiting the context, including when an uncaught exception is raised. Note: The default session is a property of the current thread.
Mainly taken from official documentation:
The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. The methods Tensor.eval() and Operation.run() will use that session to run ops.
This allows to use interactive context, like shell, as it avoids having to pass an explicit Session object to run op:
sess = tf.InteractiveSession() a = tf.constant(5.0) b = tf.constant(6.0) c = a * b # We can just use 'c.eval()' without passing 'sess' print(c.eval()) sess.close()
It is also possible to say, that InteractiveSession
supports less typing, as allows to run variables without needing to constantly refer to the session object.
The only difference between Session
and an InteractiveSession
is that InteractiveSession
makes itself the default session so that you can call run()
or eval()
without explicitly calling the session.
This can be helpful if you experiment with TF in python shell or in Jupyter notebooks, because it avoids having to pass an explicit Session object to run operations.
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