TensorFlow——图(Graph)和会话(Session)

1. tf.Graph

A TensorFlow computation, represented as a dataflow graph(一个TensorFlow计算被表示为一个数据流图).

A Graph contains a set of tf.Operation objects, which represent units of computation; and tf.Tensor objects, which represent the units of data that flow between operations(一个Graph包含一系列tf.Operation对象和tf.Tensor对象;tf.Operation对象代表计算单元,tf.Tensor对象代表operation之间流动的数据单元).

A default Graph is always registered, and accessible by calling tf.get_default_graph. To add an operation to the default graph, simply call one of the functions that defines a new Operation(默认图总是被registered,可以通过调用tf.get_default_graph访问。要向默认图添加操作,只需调用被定义的函数即可):

import tensorflow as tf

c = tf.constant(4.0)

print(c.graph)                 # 得到c所在图对象的内存地址
print(tf.get_default_graph())  # 得到当前程序默认图对象的内存地址

assert c.graph == tf.get_default_graph()  # 断言语句
print('-----')  # 该语句成功输出说明上面语句正确

输出:

<tensorflow.python.framework.ops.Graph object at 0x000001F58BA3A7F0>
<tensorflow.python.framework.ops.Graph object at 0x000001F58BA3A7F0>
-----

Another typical usage involves the tf.Graph.as_default context manager, which overrides the current default graph for the lifetime of the context(另一个典型的用法是tf.Graph.as_default上下文管理器,在上下文的生命周期中重写当前默认图):

g = tf.Graph()                 # 创建图实例(对象)
with g.as_default():
  # Define operations and tensors in `g`.
  c = tf.constant(30.0)
  assert c.graph is g

Important note: This class is not thread-safe for graph construction. All operations should be created from a single thread, or external synchronization must be provided. Unless otherwise specified, all methods are not thread-safe(注意:这个类对于图构造来说线程不安全。所有操作都应该从单个线程创建,或者必须提供外部同步。除非另有说明,所有方法都不是线程安全的).

2. tf.Session

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对象封装了执行Operation对象和计算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`.
print(sess.run(c))

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(一个会话可以拥有如tf.Variable,tf.QueueBase, tf.ReaderBase这些资源。当这些资源不再需要时,释放它们是很重要的。要做到这一点,可以在会话上调用tf.Session.close方法,或者使用会话作为上下文管理器。以下两个例子是等价的):

# Using the `close()` method.
sess = tf.Session()
sess.run(...)
sess.close()

# Using the context manager.
with tf.Session() as sess:
  sess.run(...)

Methods: __init__(初始化方法)

__init__(
    target='',
    graph=None,
    config=None
)

Creates a new TensorFlow session(创建一个新的TensorFlow会话).

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 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(如果在构造会话时没有指定graph参数,则将在会话中启动默认图。如果您在同一程序中使用多个图(用tf.Graph()创建),那么您将不得不为每个图使用不同的会话,但是每个图都可以在多个会话中使用。在这种情况下,通过将graph参数显式地传递给会话构造函数,这会比较清楚).

程序中多个Graph例子:

import tensorflow as tf

g1 = tf.Graph()
g2 = tf.Graph()
c0 = tf.constant(0.0)

with g1.as_default():                # 将g1设置为默认图然后再图中添加Operation
       c1 = tf.constant(1.0)
with tf.Graph().as_default() as g2:
       c2 = tf.constant(2.0)

with tf.Session() as sess:           # c0被添加在默认图中无需指明图参数
    assert c0.graph == tf.get_default_graph()
    print(sess.run(c0))              # 0.0

with tf.Session(graph=g1) as sess1:  
    assert c1.graph is g1
    print(sess1.run(c1))             # 1.0

with tf.Session(graph=g2) as sess2:
    assert c2.graph is g2
    print(sess2.run(c2))             # 2.0
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值