# tf.Graph对象定义了一个命名空间对于它自身包含的tf.Operation对象 # TensorFlow自动选择一个独一无二的名字,对于数据流图中的每一个操作 # 但是给操作添加一个描述性的名字可以使你的程序更容易来阅读和调试 # TensorFlow api提供两种方式来重写一个操作的名字 # 1、每一个api函数创建了一个新的tf.Operation或者返回一个新的tf.Tensor接收一个可选的name参数 # 列如tf.constant(42.0,name="answer")创建了一个新的tf.Operation叫做answer # 并且返回以叫做"answer:0"的tf.Tensor .如果默认的数据流图已经包含了叫做"answer"的操作 # TensorFlow会在后面append,"_1","_2"来使它变得唯一 # 2、tf.name_scope函数使得在名字后面添加一个后缀变得可能 import tensorflow as tf c_0 = tf.constant(0, name="c") # operation named "c" # already-used names will be "uniquified" c_1 = tf.constant(2, name="c") # => operation named "c_1" # Name scopes add a prefix to all operations created in the same context with tf.name_scope("outer"): c_2 = tf.constant(2, name="c") # =>operation nemed "outer/c" # 在层次化文件系统中,名称范围嵌套类似的路径 with tf.name_scope("inner"): c_3 = tf.constant(3, name="c") # 已经存在的变量名字会返回到前一个变量名加上一个后缀 c_4 = tf.constant(4, name="c") # =>operation named "outer/c_1" # 已经使用的命名空间会被 "uniquified" with tf.name_scope("inner"): c_5 = tf.constant(5, name="c") # =>operation named "outer/inner_1/c" init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print(c_0) print(c_1) print(c_2) print(c_3) print(c_4) print(c_5)
下面是上面代码的输出结果:
2018-02-17 11:01:55.084300: I C: f_jenkinsworkspace el-winMwindowsPY35 ensorflowcoreplatformcpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2 Tensor("c:0", shape=(), dtype=int32) Tensor("c_1:0", shape=(), dtype=int32) Tensor("outer/c:0", shape=(), dtype=int32) Tensor("outer/inner/c:0", shape=(), dtype=int32) Tensor("outer/c_1:0", shape=(), dtype=int32) Tensor("outer/inner_1/c:0", shape=(), dtype=int32)