tf.name_scope和tf.variable_scope的用法

tf.get_variable 和 tf.variable

TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable()和tf.variable_scope实现的。
当然,变量也可以通过tf.varivale()来创建。当tf.get_variable()用于变量创建时,和tf.variable的功能基本等价。

#以下两个定义是等价的
# get_variable(name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, collections=None, caching_device=None, partitioner=None, validate_shape=True, use_resource=None, custom_getter=None, constraint=None)
v = tf.get_variable(name='v', shape=[1], initializer=tf.constant_initializer(1.0))

# tf.Variable(<initial-value>, name=<optional-name>)
v = tf.Variable(tf.constant(1.0, shape=[1], name='v')

然而,两者在变量共享时存在区别:

  • tf.variable() :
    使用tf.variable() 时每次都会新建变量,且具有变量检查机制,会检测已经存在的变量是否设置为共享变量,如果已经存在的变量没有设置为共享变量,TensorFlow 运行到第二个拥有相同名字的变量的时候,就会报错。
  • tf.get_variable()
    大多数时候我们希望重用一些变量,所以就用到了tf.get_variable(),它会去搜索变量名,有就直接用,没有再新建。所以,常将tf.get_variable 和 tf.variable_scope配合使用,以变量共享。
tf.get_variable 和 tf.variable_scope

变量共享机制避不开的一个问题就是reuse,当reuse为False或者None时(默认值),同一个tf.variable_scope下面的变量名不能相同;当reuse为True时,tf.variable_scope只能获取已经创建过的变量。

#reuse=False时会报错的情况:
with tf.variable_scope('foo'):
    v = tf.get_variable(name='v',[1],initializer=tf.constant_initializer(1.0))
 
with tf.variable_scope('foo'):
    v1 = tf.get_variable(name='v',[1])

在这种情况下会报错:Variable foo/v already exists, disallowed.Did you mean to set reuse=True in Varscope?
其原因就是在命名空间foo中创建了相同的变量。如果我要在foo下创建一个变量v1,其name=‘v’,只需要将reuse设置为Ture就ok了。将上面第二部分代码中修改为reuse=True:

with tf.variable_scope('foo'):
    v = tf.get_variable(name='v',[1],initializer=tf.constant_initializer(1.0))
 
with tf.variable_scope('foo', reuse=True):
    v1 = tf.get_variable(name='v',[1])
    print(v1.name)      #结果为foo/v

当reuse已经设置为True时,tf.variable_scope获取已经创建过的变量。

假如我们继续增加命名空间bar,并创建name=‘v’的变量v3

with tf.variable_scope('bar', reuse=True):
    v3 = tf.get_variable(name='v',[1])

将会报错:Variable bar/v dose not exists, diallowed. Did you mean to set reuse=None in VarScope?

总结起来: reuse=False时,tf.variable_scope创建变量;reuse=True时,tf.variable_scope获取变量。

tf.variable_scope 和 tf.name_scope

除了tf.variable_scope外,tf.name_scope函数也提供了命名空间管理的功能。
为了实现变量共享,通常 :

  • tf.name_scope: 与tf.variable()搭配使用
  • tf.variable_scope: 与tf.get_variable()搭配使用

这两个函数在大部分情况下是等价的,唯一的区别是在使用tf.get_variable()函数时。
两者主要区别: tf.name_scope的命名空间管理功能只限于tf.variable生成的变量,而tf.get_variable不受tf.name_scope的影响。

### tf.variable_scope
with tf.variable_scope('foo'):
    a = tf.get_variable('bar',[1])
    print(a.name) # 结果为foo/bar:0
### tf.name_scope
with tf.name_scope('a'):
    a = tf.Variable([1])
    print(a.name) # 结果为a/Variable:0
 
    b = tf.get_variable('b',[1])
    print(b.name) # 结果为b:0

从这个结果中,我们能很清晰地看到,tf.get_variable创建的变量并不是a/b:0,而是b:0。这就表示了在tf.name_scope函数下,tf.get_variable不受其约束。

两者区别的具体实例也可以参考: tf.variable_scope和tf.name_scope的用法

参考博客:
tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope关系

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值