Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between tf.Session() and tf.InteractiveSession()?

Tags:

tensorflow

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.

like image 590
Raady Avatar asked Jan 22 '17 13:01

Raady


People also ask

What can I use instead of tf session?

tensorflow - The name tf. Session is deprecated. Please use tf. compat.

What is the use of tf session?

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.

What is tf session in TensorFlow?

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.

What is the difference between a session and an interactivesession?

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.

What is interactivesession in TensorFlow?

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.

What is the use of interactivesession in Python?

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.

How to create a session that is automatically closed when exiting context?

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.


2 Answers

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.

like image 83
Set Avatar answered Oct 19 '22 22:10

Set


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.

like image 28
Salvador Dali Avatar answered Oct 19 '22 23:10

Salvador Dali