命名空间
1. 不同的命名空间。
tf.variable_scople 与tf.name_scope函数提供命名空间管理功能。
大部分情况下等价。
唯一的区别,在使用tf.get_viriable函数时。
import tensorflow as tf
with tf.variable_scope("foo"):
a = tf.get_variable("bar", [1])
print (a.name)
with tf.variable_scope("bar"):
b = tf.get_variable("bar", [1])
print (b.name)
结果
foo/bar:0
bar/bar:0
2. tf.Variable和tf.get_variable的区别。
with tf.name_scope("a"):
a = tf.Variable([1])
print (a.name)
a = tf.get_variable("b", [1])
print (a.name)
with tf.variable_scope("a"):
a = tf.Variable([1])
print (a.name)
a = tf.get_variable("b", [1])
print (a.name)
结果
a/Variable:0
b:0
a_1/Variable:0
a/b:0
3. TensorBoard可以根据命名空间来整理可视化效果图上的节点。
with tf.name_scope("input1"):
input1 = tf.constant([1.0, 2.0, 3.0], name="input1")
with tf.name_scope("input2"):
input2 = tf.Variable(tf.random_uniform([3]), name="input2")
output = tf.add_n([input1, input2], name="add")
writer = tf.summary.FileWriter("/log/simple_example.log", tf.get_default_graph())
writer.close()
默认视图中,同一个命名空间下的所有节点会被缩略成一个节点,只有顶层命名空间中的节点才会被显示。
展开后