Class Graph
A TensorFlow computation, represented as a dataflow graph.
一个Graph
包含很多tf.Operation
对象( represent units of computation;)和tf.Tensor
对象 (represent the units of data that flow between operations)
该类下的方法
tf.get_default_graph
- 获取默认的图
定义一个Operation
就会添加新的操作到默认图:
c = tf.constant(4.0)
assert c.graph is tf.get_default_graph()
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
一个Graph
实例可以包括任意的”collections”。比如 tf.Variable
在创建一个图的时候放在这个 collection ( tf.GraphKeys.GLOBAL_VARIABLES
) ,其他collection可以通过声明别的名字。collections相当对图中的类似的计算单元做了打包。
name_scope
name_scope(name)
作用:Returns a context manager that creates hierarchical names for operations.
#操作单元在流程图上的操作名称
with tf.Graph().as_default() as g:
c = tf.constant(5.0, name="c")
assert c.op.name == "c"
c_1 = tf.constant(6.0, name="c")
assert c_1.op.name == "c_1"
#nested被声明为图g的默认scope
# Creates a scope called "nested"
with g.name_scope("nested") as scope:
nested_c = tf.constant(10.0, name="c")
assert nested_c.op.name == "nested/c"
#图g创建scope "inner",被放在默认的scope“nested”内
with g.name_scope("inner"):
nested_inner_c = tf.constant(20.0, name="c")
assert nested_inner_c.op.name == "nested/inner/c"
# 名称为inner的scope已经存在,自动起别名 "inner_1",同样放在“nested”内
with