Tf.Session()和 tf.InteractiveSession()的区别是什么?

在哪些情况下,应该考虑 tf.Session()tf.InteractiveSession()的目的是什么?

当我尝试使用前一个函数时,一些函数(例如 .eval())不起作用,当我更改为后一个函数时,它起作用了。

35275 次浏览

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.

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

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.