name_scope仅仅作用于操作上,并且不影响variable_scope。这么说有点抽象,如下代码:
import tensorflow as tf
with tf.variable_scope('root') as scope:
var1 = tf.get_variable('var1', shape=[1], dtype=tf.float32)
var2 = tf.get_variable('var2', shape=[1], dtype=tf.float32)
sum1 = var1 + var2
with tf.name_scope('plus'):
sum2 = var1 + var2
var3 = tf.get_variable('var3', shape=[1], dtype=tf.float32)
print(var1.name)
print(var2.name)
print(sum1.name)
print(sum2.name)
print(var3.name)
输出:
root/var1:0
root/var2:0
root/add:0
root/plus/add:0
root/var3:0
很容易看出,sum2
与var3
之间的区别,name_scope
只对运算操作有效(添加一个子scope),并且不影响新的Variable
。