TensorFlow学习笔记1.4:tf.Session() InteractiveSession()

A class for running TensorFlow operations.
一个用于运行TensorFlow操作的类

A Session object encapsulates the environment in which Operation objects are executed, and Tensor objects are evaluated.
会话对象封装了执行操作对象的环境, 并且计算Tensor对象的值。

# Build a graph.建立一个图
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# Launch the graph in a session.在会话中启动图
sess = tf.Session()
# Evaluate the tensor `c`. 计算张量“c”
print(sess.run(c))
# Using the `close()` method.
sess = tf.Session()
sess.run(...)
sess.close()

# Using the context manager.
with tf.Session() as sess:
  sess.run(...)
# Launch the graph in a session that allows soft device placement and
# logs the placement decisions.
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                        log_device_placement=True))

http://devdocs.io/tensorflow~python/tf/interactivesession

A TensorFlow Session for use in interactive contexts, such as a shell.

The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction.
The methods tf.Tensor.eval and tf.Operation.run will use that session to run ops.

This is convenient in interactive shells and IPython notebooks, as it avoids having to pass an explicit Session object to run ops.

For example:

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

Note that a regular session installs itself as the default session when it is created in a with statement. The common usage in non-interactive programs is to follow that pattern:

a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
with tf.Session():
  # We can also use 'c.eval()' here.
  print(c.eval())
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 郭相麟 时间的宽度对于每个人来说是相同,一天二十四个小时,但对于每个人来说,时间的厚度又是不同的,产生出多大的价...
    郭相麟阅读 1,339评论 0 0
  • 有这样一道选择题:当地球被丧尸占领,你的朋友家人同事全都变成丧尸,你是地球上最后一个人类的时候,你是选择同流合污变...
    简彤阅读 3,118评论 0 0
  • 提起“程序员”,你心中浮现的形象是不是酱婶的 . . . 疯癫 且蓬头垢面 还有酱婶的 . . 对于web前端的学...
    DarkSpy13阅读 5,215评论 1 4