76

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.

nbro
  • 15,395
  • 32
  • 113
  • 196
Raady
  • 1,686
  • 5
  • 22
  • 46

4 Answers4

82

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.

P-Gn
  • 23,115
  • 9
  • 87
  • 104
Set
  • 47,577
  • 22
  • 132
  • 150
27

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.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
1

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.

  • Was this perhaps once true but no longer the case? I seem to remember experiencing this difference in the past, but now new sessions seem to be using only 100MB of GPU RAM each (with version 1.13.1), and I can't find, in a cursory search of the 1.10, 1.11, 1.12, and 1.13 `tf.InteractiveSession` docs, evidence one way or the other. – tsbertalan Mar 20 '19 at 16:52
-7

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]))
Parvez Khan
  • 537
  • 7
  • 15