tf.Graph.__init__:建立一个空图;
tf.Graph.as_default():一个将某图设置为默认图,并返回一个上下文管理器,常与 with 结构相搭配;
g = tf.Graph()
with g.as_default():
# Define operations and tensors in `g`.
c = tf.constant(30.0)
assert c.graph is g
c = tf.constant(4.0)
assert c.graph is tf.get_default_graph()
tf.Graph.finalized:返回True,如果图被完成
- tf.Graph.finalize():完成图的构建,即将其设置为只读模式
if not tf.Graph.finalized:
tf.Graph.finalize()
tf.Graph.control_dependencies(control_inputs):定义一个控制依赖,并返回一个上下文管理器,也常与 with 结构搭配;
with g.control_dependencies([a, b, c]):
....
d = …
e = …
def my_func(pred, tensor):
with tf.control_dependencies([pred]):
return tf.matmul(tensor, tensor)
tf.Graph.device(device_name_or_function):定义运行图所使用的设备,并返回一个上下文管理器
with g.device('/gpu:0'): ...
with g.device('/cpu:0'): ...