之前一直很困惑,tf.variable_scope和tf.name_scope都是管理上下文环境的,它们有什么不同?
查阅资料时,发现了一段有意思的测试代码
import tensorflow as tf
def scoping(fn, scope1, scope2, vals):
with fn(scope1):
a = tf.Variable(vals[0], name='a')
# 此处注意 b是get_variable
b = tf.get_variable('b', initializer=vals[1])
c = tf.constant(vals[2], name='c')
with fn(scope2):
d = tf.add(a * b, c, name='res')
print('\n '.join([scope1, a.name, b.name, c.name, d.name]), '\n')
return d
d1 = scoping(tf.variable_scope, 'scope_vars', 'res', [1, 2, 3])
d2 = scoping(tf.name_scope, 'scope_name', 'res', [1, 2, 3])
# 如果加上这一行,就会报错,因为d3的变量b会和d2的变量b冲突
# d3 = scoping(tf.name_scope, 'scope_name2', 'res', [1, 2, 3])
# 但这一行就不会冲突,因为d3和d1的变量b各自有作用域
# d3 = scoping(tf.variable_scope, 'scope_vars2', 'res', [1, 2, 3])
with tf.Session() as sess:
writer = tf.summary.FileWriter('logs', sess.graph)
sess.run(tf.global_variables_initializer())
print(sess.run([d1, d2]))
writer.close()
运行后,得到如下输出
scope_vars
scope_vars/a:0
scope_vars/b:0
scope_vars/c:0
scope_vars/res/res:0
scope_name
scope_name/a:0
b:0
scope_name/c:0
scope_name/res/res:0
TensorBoard显示
总而言之,tf.name_scope仅为非tf.get_variable创建的tensor添加前缀;而tf.variable_scope为所有tensor添加前缀