先看代码:
#命名空间函数tf.variable_scope()和tf.name_scope()函数区别于使用
import tensorflow as tf
with tf.variable_scope("foo"):
a = tf.get_variable("bar", [1])
print(a.name) #foo/bar:0
b = tf.Variable("b", [1])
print(b.name) #foo/Variable:0
with tf.variable_scope("bar"):
a = tf.get_variable("bar", [1])
print(a.name) #bar/bar:0
b = tf.Variable("b", [1])
print(b.name) #bar/Variable:0
with tf.name_scope("a"):
a = tf.Variable([1])
print(a.name) #a/Variable:0
with tf.name_scope("b"):
b = tf.get_variable("b", [1])
print(b.name) #b_1:0
with tf.name_scope("b"):
c = tf.get_variable("b", [1])
print(c.name) #出错,b已存在
可以看出,对于tf.Variable()函数,两者的使用情况都一样;而tf.get_variable()函数,它不受name_scope约束,已经声明过的变量就不能再声明了。