#在名字为foo的命名空间内创建名字为v的变量with tf.variable_scope("foo"):
v = tf.get_variable("v", [1], initializer=tf.constant_initializer(1.0))
'''
命名空间foo中已经存在变量v,所以下面的代码将会报错:Variable foo/v already exitst,disallowed.Did you mean to set reuse=True in VarScope?
'''#with tf.variable_scope("foo"):# v = tf.get_variable("v", [1])#在生成上下文管理器时,将参数reuse 设置为True.这样tf.get_variable 函数将直接获取已经声明的变量 with tf.variable_scope("foo", reuse=True):
v1 = tf.get_variable("v", [1])
print v == v1 #输出为True,代表v,v1代表的是相同的TensorFlow 中变量。'''
将参数reuse 设置为True 时,tf.variable_scope 将只能获取已经创建过的变量。因为在命名空间bar 中还没有创建变量v,所以下面的代码将会报错:Variable foo/v not exitst,disallowed.Did you mean to set reuse=None in VarScope?
'''#with tf.variable_scope("bar", reuse=True):# v = tf.get_variable("v", [1])