tf.get_variable创建的变量不受tf.name_scope的约束(这里不受约束指的就是,get_variable创建的变量不在tf.name_scope创建的作用域内),
import tensorflow as tf
with tf.name_scope('name_scope_x'):
var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
var3 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32)
var4 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(var1.name, sess.run(var1))
print(var3.name, sess.run(var3))
print(var4.name, sess.run(var4))
>>> var1:0 [-0.30036557] 可以看到前面不含有指定的'name_scope_x'
>>> name_scope_x/var2:0 [ 2.]
>>> name_scope_x/var2_1:0 [ 2.] 可以看到变量名自行变成了'var2_1',避免了和'var2'冲突
而且如果tf.get_variable在创建变量且没有设置共享变量,如果重命名会报错,
import tensorflow as tf
with tf.name_scope('name_scope_1'):
var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
var2 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(var1.name, sess.run(var1))
print(var2.name, sess.run(var2))
>>> ValueError: Variable var1 already exists, disallowed. Did you mean
>>> to set reuse=True in VarScope? Originally defined at:
>>> var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
设置共享变量需要tf.variable_scope,具体设置如下:
import tensorflow as tf
with tf.variable_scope('variable_scope_y') as scope:
var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
scope.reuse_variables() # 设置共享变量
var1_reuse = tf.get_variable(name='var1')
var2 = tf.Variable(initial_value=[2.], name='var2', dtype=tf.float32)
var2_reuse = tf.Variable(initial_value=[2.], name='var2', dtype=tf.float32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(var1.name, sess.run(var1))
print(var1_reuse.name, sess.run(var1_reuse))
print(var2.name, sess.run(var2))
print(var2_reuse.name, sess.run(var2_reuse))
>>> variable_scope_y/var1:0 [-1.59682846]
>>> variable_scope_y/var1:0 [-1.59682846] 可以看到变量var1_reuse重复使用了var1
>>> variable_scope_y/var2:0 [ 2.]
>>> variable_scope_y/var2_1:0 [ 2.]
当然也可以这样,
with tf.variable_scope('foo') as foo_scope:
v = tf.get_variable('v', [1])
with tf.variable_scope('foo', reuse=True):
v1 = tf.get_variable('v')
assert v1 == v
网友评论