参考内容都出自于官方API教程tf.Graph
一、Graph类调用
这里使用的是1.15版本,TF官方为了能够在2.0+版本中保持兼容,因此调用时使用了tf.compat.v1.Graph。若安装的就是1.15版本,直接tf.Graph即可。
定义:一个Graph是一系列tf.Operation(表示计算单元)和tf.Tensor(表示operations之间的数据流单元)的组合。
在构建图时,系统已经默认有default Graph被注册,可以直接向默认图中添加操作。这个默认的图通过**tf.get_default_graph()**来获取。下例在默认图中创建了一个操作,并且查看是否成功添加进了图:
a = tf.constant(4.0)
print(a.graph)
print(tf.get_default_graph())
也可以使用**tf.Graph()**新建图,并且设置为默认、添加操作。一般使用Context Manger来进行管理,使代码在局部生效。一旦脱离with区域,自定义默认图失效:
g = tf.Graph()
with g.as_default():
b = tf.constant(30.0)
print(b.graph)
print(g)
print(tf.get_default_graph())
tf.get_default_graph()
二、方法
并没有全列出来,见一个记录一个:
1.as_default()
在上面已经阐述过,返回的是一个context manager,使得当前图为default graph。
如果不自定义图,则所有的Op都会被放置于系统默认图中。
一般使用with形式来进行调用,可以进行嵌套。使用方法如下:
g = tf.Graph()
with g.as_default():
c = tf.constant(5.0)
assert c.graph is g
with tf.Graph().as_default() as g:
c = tf.constant(5.0)
assert c.graph is g
2.device()
调用方法为device(device_name_or_function),其中的device_name_or_function一共有三种状况:
- 设备的name string:所有在context中构建的Op都会被放置于制定设备中。
- 若是一个function:每一次Op被创建时都会被调用。
- 如果是None:Context中所有的设定都将被忽略。
g = tf.Graph()
with g.as_default():
with g.device('/gpu:0'):
e = tf.constant(2.0, name='a_g')
print(e)
f = tf.constant(3.0)
print(f)
with g.device('/device:GPU:0'):
with g.device(None):
def matmul_on_gpu(n):
if n.type == "MatMul":
return "/device:GPU:0"
else:
return "/cpu:0"
with g.device(matmul_on_gpu):
3.finalize()
一旦一个Graph调用了此方法,此图被锁定,不能够添加新的Op。一般用于多线程中放置Op的添加。Graph具有finalized的property,用于查询Graph是否被锁定:
print g.finalized
with g.as_default():
n = tf.constant(1.0)
g.finalize()
print g.finalized
with g.as_default():
k = tf.constant(1.0)
4.name_scope()
调用方法:name_scope(name=string),返回一个context manager,能够对Op进行级联式/层级化命名。一旦一段中有with name_scope,则这段中所有名称的前面都会加入prefix。
- 重复问题:如果和已有的scope重名,自动调用self.unique_name(name)创建一个新独特命名,策略为添加下标"_1"、"_2"…
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"
c_2 = tf.constant(7.0, name="c")
assert c_2.op.name == "c_2"
with g.name_scope("nested") as scope:
nested_c = tf.constant(10.0, name="c")
assert nested_c.op.name == "nested/c"
with g.name_scope("inner"):
nested_inner_c = tf.constant(20.0, name="c")
assert nested_inner_c.op.name == "nested/inner/c"
with g.name_scope("inner"):
nested_inner_1_c = tf.constant(30.0, name="c")
assert nested_inner_1_c.op.name == "nested/inner_1/c"
with g.name_scope(scope):
nested_d = tf.constant(40.0, name="d")
assert nested_d.op.name == "nested/d"
with g.name_scope(""):
e = tf.constant(50.0, name="e")
assert e.op.name == "e"
with tf.Graph().as_default() as g:
inputs = tf.constant([1.0])
with g.name_scope('my_layer') as scope:
weights = tf.Variable(initial_value=[0.1], name="weights")
biases = tf.Variable(initial_value=[0.1], name="biases")
affine = tf.multiply(inputs, weights) + biases
output = tf.nn.relu(affine, name=scope)
assert weights.op.name == "my_layer/weights"
assert biases.op.name == "my_layer/biases"
assert affine.op.name == "my_layer/add"
assert output.op.name == "my_layer"
with g.name_scope('my_layer') as scope_:
weights = tf.Variable(initial_value=[0.1], name="weights")
biases = tf.Variable(initial_value=[0.1], name="biases")
affine = tf.multiply(inputs, weights) + biases
output = tf.nn.relu(affine, name=scope_)
assert weights.op.name == "my_layer_1/weights"
assert biases.op.name == "my_layer_1/biases"
assert affine.op.name == "my_layer_1/add"
assert output.op.name == "my_layer_1"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)