g = tf.Graph()
with tf.Session() as ss:
print(ss.graph)
'''
<tensorflow.python.framework.ops.Graph object at 0x000000000239F390>
<tensorflow.python.framework.ops.Graph object at 0x000000000239F390>
<tensorflow.python.framework.ops.Graph object at 0x000000000239F390>
<tensorflow.python.framework.ops.Graph object at 0x000000000239F390>
'''
使用get_default_graph()获取默认图的地址
import tensorflow as tf
con_a = tf.constant(3)
con_b = tf.constant(4)
con_c = tf.add(con_a,con_b)
print(con_c.graph)
print(tf.get_default_graph())
g = tf.Graph()
with tf.Session() as ss:
print(ss.graph)
'''
<tensorflow.python.framework.ops.Graph object at 0x000000000258F390>
<tensorflow.python.framework.ops.Graph object at 0x000000000258F390>
<tensorflow.python.framework.ops.Graph object at 0x000000000258F390>
'''
使用tf.Graph()创建自定义图来运行数据
在这里我们要先创建图,创建完成后在图中定义tensor和op,之后使用with tf.Session(graph=g) as ss来开启graph指定要运行的图。
import tensorflow as tf
# 创建图
g = tf.Graph()
with g.as_default():
#在创建的图中定义tensor和op
g_a = tf.constant(5.0)
print(g_a)
#Tensor("Const:0", shape=(), dtype=float32)
#开启graph指定要运行的图,如果我们想要得到运行的结果,必须进行下面这段代码
with tf.Session(graph=g) as ss:
print(ss.run(g_a)) #5.0
在上述代码大体分成两个阶段
1.构建图阶段
2.执行图阶段
关于结果标红问题
代码运行可能出现下面的结果:I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2