一、graph基本概念
tensor.graph
tf.get_default_graph()
with g.as_default()
def test_graph():
c = tf.constant(value=1)
print(c.graph),(tf.get_default_graph()) #c1在默认图中 0x7fbaf4926790,0x7fbaf4926790
g=tf.Graph() #新建一张图,Graph是一个类
with g.as_default():
c2 = tf.constant(value=2)
print(c2.graph), (tf.get_default_graph())
# c2在新创建的图g中(图g覆盖了默认图),此时with语句中的操作都会更新在图g
# 0x7fbb2c4a7210,0x7fbb2c4a7210
c3 = tf.constant(value=3)
print(c3.graph), (tf.get_default_graph())
# 跳出g,默认图就是一开始c1所在的默认图
# 0x7fbaf4926790,0x7fbaf4926790
二、graph基本操作
-
graph.get_operations()
,返回一系列ops
-
graph.get_operation_by_name()
,根据name返回op
-
graph.get_tensor_by_name()
,根据name返回tensor