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

你可能感兴趣的:(TensorFlow学习笔记1.4:tf.Session() InteractiveSession())