@tf_export('Session')classSession(BaseSession):"""A classfor running TensorFlow operations. 用于运行TensorFlow操作的类。
A `Session` object encapsulates the environment in which `Operation`
objects are executed,and `Tensor` objects are evaluated. For
example:
“Session”对象封装了执行“操作”对象并评估“张量”对象的环境。 例如:
# Build a graph. 建立一个图
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# Launch the graph in a session. 在session中启动这个图。
sess = tf.Session()# Evaluate the tensor `c`. 评估张量c。print(sess.run(c))# 结果为30
A session may own resources, such as
`tf.Variable`, `tf.QueueBase`,and `tf.ReaderBase`. It is important to release
these resources when they are no longer required. To do this, either
invoke the `tf.Session.close` method on the session,or use
the session as a context manager. The following two examples are
equivalent:
session 可能拥有资源,例如“ tf.Variable”,“ tf.QueueBase”和“ tf.ReaderBase”。 当不再需要这些资源时,重要的是释放它们。 为此,请在会话上调用`tf.Session.close`方法,或将该会话用作上下文管理器。 以下两个示例是等效的:
# Using the `close()` method. 使用close()方法。
sess = tf.Session()
sess.run(...)
sess.close()# Using the context manager. 使用上下文管理器。with tf.Session()as sess:
sess.run(...)
The
[`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto)
protocol buffer exposes various configuration options for a
session. For example, to create a session that uses soft constraints
for device placement,and log the resulting placement decisions,
create a session as follows:[`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.pr oto)协议缓冲区公开了会话的各种配置选项。
例如,要创建使用软约束进行设备放置的会话并记录生成的放置决策,请按以下方式创建会话:
# 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))
def__init__(self, target='', graph=None, config=None):"""Creates a new TensorFlow session. 创建一个新的TensorFlow session。
If no `graph` argument is specified when constructing the session,
the default graph will be launched in the session. If you are
using more than one graph (created with `tf.Graph()` in the same
process, you will have to use different sessions for each graph,
but each graph can be used in multiple sessions. In this case, it
is often clearer to pass the graph to be launched explicitly to
the session constructor.
如果在构建session时未指定`graph`参数,则默认图形将在session中启动。
如果您在同一过程中使用多个图(通过`tf.Graph()`创建的),则每个图必须使用不同的session,
但是每个图可以用于多个session。在这种情况下, 通常更清楚地将要显式启动的图传递给会话构造函数。
Args:
target: (Optional. 可选的) The execution engine to connect to.
Defaults to using an in-process engine. See
[Distributed TensorFlow](https://tensorflow.org/deploy/distributed)
for more examples.
要连接的执行引擎。
默认为使用进程内引擎。
有关更多示例,请参见[Distributed TensorFlow](https://tensorflow.org/deploy/distributed)。
graph: (Optional. 可选的) The `Graph` to be launched (described above).
要启动的“图”(如上所述)。
config: (Optional. 可选的) A
[`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto)
protocol buffer with configuration options for the session.
具有会话配置选项的[`ConfigProto`]
(https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto)协议缓冲区。
"""super(Session, self).__init__(target, graph, config=config)# NOTE(mrry): Create these on first `__enter__` to avoid a reference cycle.# 在第一个__enter__上创建它们以避免引用循环。
self._default_graph_context_manager =None
self._default_session_context_manager =None