tf.name_scope()详解
参考文献:
https://blog.****.net/jerr__y/article/details/60877873
命名空间其实就是给几个变量包一层名字,方便变量管理。函数是:tf.name_scope
另外,就像操作系统文件夹命名一样,不同的顶层文件夹下,可以有同名文件夹。这里,不同的命名空间下,可以有名字相同的变量。
import tensorflow as tf
with tf.name_scope('foo'): # 原文代码为with tf.name_scope('conv1') as scope:,这里的as scope貌似没用
weights1 = tf.Variable([1.0, 2.0], name='weights')
bias1 = tf.Variable([0.3], name='bias')
# 下面是在另外一个命名空间来定义变量的
with tf.name_scope('bar'):
weights2 = tf.Variable([4.0, 2.0], name='weights')
bias2 = tf.Variable([0.33], name='bias')
# 所以,实际上weights1 和 weights2 这两个引用名指向了不同的空间,不会冲突
init = tf.global_variables_initializer() # tensorboard中,变量初始化操作名称均为init
sess = tf.Session()
writer=tf.summary.FileWriter("logs", sess.graph) # 文件写在该.py文件同级logs文件夹下,用 tensorboard --logdir=. 即可
sess.run(init)
print(weights1.name) # foo/weights:0 0是什么意思?
print(weights2.name) # bar/weights:0