tf.Session() 和 tf.InteractiveSession() 有什么区别? [英] What's the difference between tf.Session() and tf.InteractiveSession()?

查看:32
本文介绍了tf.Session() 和 tf.InteractiveSession() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In which cases should tf.Session() and tf.InteractiveSession() be considered for what purpose?

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

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.

推荐答案

主要摘自官方 文档:

与常规会话的唯一区别是 InteractiveSession 在构建时将自身安装为默认会话.Tensor.eval() 和 Operation.run() 方法将使用该会话来运行操作.

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.

这允许使用交互式上下文,如 shell,因为它避免了必须传递显式 Session 对象来运行操作:

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

也可以说,InteractiveSession 支持较少的输入,因为允许运行变量而无需不断引用会话对象.

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.

这篇关于tf.Session() 和 tf.InteractiveSession() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆