import tensorflow as tf
In [3]:
with tf.variable_scope("foo"):
v = tf.get_variable("v", [1], initializer=tf.constant_initializer(1.0))
#with tf.variable_scope("foo"):
# v = tf.get_variable("v", [1])
with tf.variable_scope("foo", reuse=True):
v1 = tf.get_variable("v", [1])
print( v == v1)
#with tf.variable_scope("bar", reuse=True):
# v = tf.get_variable("v", [1])
True
2. 嵌套上下文管理器中的reuse参数的使用。
In [5]:
with tf.variable_scope("root"):
print(tf.get_variable_scope().reuse)
with tf.variable_scope("foo", reuse=True):
print (tf.get_variable_scope().reuse)
with tf.variable_scope("bar"):
print( tf.get_variable_scope().reuse)
print( tf.get_variable_scope().reuse)
FalseTrueTrueFalse
3. 通过variable_scope来管理变量。
In [6]:
v1 = tf.get_variable("v", [1])
print(v1.name)
with tf.variable_scope("foo",reuse=True):
v2 = tf.get_variable("v", [1])
print (v2.name)
with tf.variable_scope("foo"):
with tf.variable_scope("bar"):
v3 = tf.get_variable("v", [1])
print (v3.name)
v4 = tf.get_variable("v1", [1])
print (v4.name)
v:0
foo/v:0
foo/bar/v:0v1:0
v = tf.Variable(0, dtype=tf.float32, name="v")
for variables in tf.global_variables(): print( variables.name)
ema = tf.train.ExponentialMovingAverage(0.99)
maintain_averages_op = ema.apply(tf.global_variables())
for variables in tf.global_variables(): print( variables.name)