这四个函数是互相联系的。下面翻译一下stackoverflow上的高票答案
what’s the difference of name scope and a variable scope in tensorflow?
翻译:
在讲name scope和variable scope之前,我们先简单介绍一下变量共享机制,这个机制使得tensorflow使得graph的其他部分也能调用之前定义过的变量。
tf.get_variable()方法可以用来创建新的变量或者用来检索之前已经定义过的变量。而tf.Variable是会一直创建新的变量,如果你命名重复,tf会自动给变量加一个后缀。
使用tf.variable_scope使得变量检索变得更简单。
简单来说
tf.name_scope()是给operator以及tf.Variable创建的variable创建namespace的
tf.variable_scope()是给variable和operator创建namespace的
也就是说,tf.get_variable()创建的变量是不受name_scope影响的
举个例子
with tf.name_scope("my_scope"):
v1 = tf.get_variable("var1", [1], dtype=tf.float32)
v2 = tf.Variable(1, name="var2", dtype=tf.float32)
a = tf.add(v1, v2)
print(v1.name) # var1:0 ,namescope被忽略了
print(v2.name) # my_scope/var2:0
print(a.name) # my_scope/Add:0
with tf.name_scope("foo"):
with tf.variable_scope("var_scope"):
v = tf.get_variable("var", [1])
with tf.name_scope("bar"):
with tf.variable_scope("var_scope", reuse=True):
v1 = tf.get_variable("var", [1])
print(v.name) # var_scope/var:0
print(v1.name) # var_scope/var:0
这使得我们可以在不同的name scope下共享变量.
这对大型网络在tensorboard中可视化非常有用,这个 教程 还可以