What's the difference between tf.Session() and tf.InteractiveSession()?
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.
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.