
本节介绍TensorFlow与创建计算图的几个函数:
示范1:
import tensorflow as tf
# 初始化一个计算图对象g
g = tf.Graph()
# 在g中添加节点定义计算图
with g.as_default():
a = tf.constant(10,name = 'a')
b = tf.constant(5,name = 'b')
c = tf.add(a,b)
print(c.graph) 执行后输出为:

示范2:
import tensorflow as tf
#获取默认的计算图对象句柄g0
g0 = tf.get_default_graph()
#在g0中添加节点定义计算图
with g0.as_default():
a = tf.constant(10,name = 'a')
b = tf.constant(5,name = 'b')
c = tf.add(a,b)
print(c.graph)
执行后输出为:

示范3:
import tensorflow as tf
#清空默认的计算图
tf.reset_default_graph()
# 直接用Operator定义的节点将添加到默认计算图中
a = tf.constant(10,name = 'a')
b = tf.constant(5,name = 'b')
c = tf.add(a,b)
print(c.graph)
# 查看默认的计算图中的Operators
tf.get_default_graph().get_operations()
执行后输出为:

推荐使用示范1的方式定义计算图,不用每次都 tf.reset_default_graph()。