python tensorflow tf.session类

本文深入讲解TensorFlow中的Session类,它是执行操作和评估张量的核心组件。文章详细介绍了如何使用Session来构建和运行计算图,管理资源,以及如何正确关闭Session以释放资源。此外,还探讨了Session的配置选项,包括设备放置和日志记录。
@tf_export('Session')
class Session(BaseSession):
  """A class for 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

参考文章:tf.Session

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值