一、英文文档深入解读
class tf.Graph
A TensorFlow computation, represented as a dataflow graph.
Graph实际上是一个由数据流图表示的计算过程
A Graph
contains a set of Operation
objects, which represent units of computation; and Tensor
objects, which represent the units of data that flow between operations.
Graph表示了数据流图中的操作流水,而Tensor表示的是其中流动的数据
底下这几段讲述了系统默认的Graph获取方式,以及他的“线程不安全”特性。
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
:
c = tf.constant(4.0)
assert c.graph is tf.get_default_graph()
Another typical usage involves the Graph.as_default()
context manager, which overrides the current default graph for the lifetime of the context:
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.
二、关于Tensorflow的图计算过程
我们通过下面的代码来看一下Tensorflow的图计算过程:
import tensorflow as tf
a = tf.constant(1)
b = tf.constant(2)
c = tf.constant(3)
d = tf.constant(4)
add1 = tf.add(a, b)
mul1 = tf.mul(b, c)
add2 = tf.add(c, d)
output = tf.add(add1, mul1)
with tf.Session() as sess:
print sess.run(output)
# result: 9
上面的代码构成的Graph如下图所示,
当Session加载Graph的时候,Graph里面的计算节点都不会被触发执行。当运行sess.run(output)的时候,会沿着指定的Tensor output来进图路径往回触发相对应的节点进行计算(图中红色线表示的那部分)。当我们需要output的值时,触发Operation tf.add(add1, mul1)被执行,而该节点则需要Tensor add1和Tensor mul1的值,则往回触发Operation tf.add(a, b)和Operation tf.mul(b, c)。以此类推。
所以在计算Graph时,并不一定是Graph中的所有节点都被计算了,而是指定的计算节点或者该节点的输出结果被需要时。