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

Tensorflow

Tensorflow Problem Overview


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 Solutions


Solution 1 - Tensorflow

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.

Solution 2 - Tensorflow

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.

Solution 3 - Tensorflow

On the top of installing itself as default session as per official documentation, from some tests on memory usage, it seems that the interactive session uses the gpu_options.allow_growth = True option - see [using_gpu#allowing_gpu_memory_growth] - while tf.Session() by default allocates the whole GPU memory.

Solution 4 - Tensorflow

Rather than above mentioned differences - the most important difference is with session.run() we can fetch values of multiple tensors in one step.

For example:

num1 = tf.constant(5)
num2 = tf.constant(10)
num3 = tf.multiply(num1,num2)
model = tf.global_variables_initializer()

session = tf.Session()
session.run(model)

print(session.run([num2, num1, num3]))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRaadyView Question on Stackoverflow
Solution 1 - TensorflowSetView Answer on Stackoverflow
Solution 2 - TensorflowSalvador DaliView Answer on Stackoverflow
Solution 3 - TensorflowFrancesco CascioView Answer on Stackoverflow
Solution 4 - TensorflowParvez KhanView Answer on Stackoverflow