Raady
Raady

Reputation: 1726

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

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.

Upvotes: 77

Views: 36577

Answers (4)

Francesco Cascio
Francesco Cascio

Reputation: 11

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.

Upvotes: 1

Set
Set

Reputation: 49779

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.

Upvotes: 83

Parvez Khan
Parvez Khan

Reputation: 577

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]))

Upvotes: -7

Salvador Dali
Salvador Dali

Reputation: 222541

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.

Upvotes: 28

Related Questions