【tensorflow】scope的使用以及tf.Variable()和tf.get_variable()的区别

1.Scope

作用:

①为scope内的op和tensor的name增加前缀,方便管理

②其reuse参数可用来变量共享


两种scope:

①tf.name_scope(name): 为scope内的op和tf.Variable()/tf.constant()创建的tensor添加前缀name,例子如下

with tf.name_scope('scope1') as scope1:
    var1=tf.Variable(name='var1',initial_value=1)
    var2=tf.get_variable(name='var2',shape=())
    init_op=tf.initialize_all_variables()
print(var1.name)
print(var2.name)
print(init_op.name)

result:

scope1/var1:0

var2L0

scope1/init


②tf.variable_scope(name): 为scope内的所有op和tensor添加前缀name,例子如下

with tf.variable_scope('scope1') as scope1:
    var1=tf.Variable(name='var1',initial_value=1)
    var2=tf.get_variable(name='var2',shape=())
    init_op=tf.initialize_all_variables()
print(var1.name)
print(var2.name)
print(init_op.name)


result:

scope1/var1:0
scope1/var2:0
scope1/init


利用tf.get_variable_scope()返回的obj可获取当前scope的信息,如.name()获取scope名等


2.tf.Variable()和tf.get_variable()的区别

①如前所述,tf,Variable()受name_scope影响,tf.get_variable()则不受

②创建name冲突的变量时,tf.Variable()自动变更name,而tf.get_variable()根据scope的reuse是否为true,返回或创建参数,实现变量共享

共享参数,是指name已存在,则获取,若不存在,则创建。

例子:

with tf.variable_scope('scope1') as scope1:
    var1=tf.get_variable(name='var1',shape=())
    var2 = tf.get_variable(name='var1', shape=())
print(var1.name)
print(var2.name)


result:

ValueError: Variable scope1/var1 already exists, disallowed.


with tf.variable_scope('scope1') as scope1:
    var1 = tf.get_variable(name='var1', shape=())
with tf.variable_scope(scope1, reuse=True):
    var2 = tf.get_variable(name='var1', shape=())
print(var1.name)
print(var2.name)
print(var1==var2)

result:

scope1/var1:0 

scope1/var1:0

True


事实上可以直接修改scope的reuse参数,更为简便

with tf.variable_scope('scope1') as scope1:
    var1 = tf.get_variable(name='var1', shape=())
    scope1.reuse_variables()
    var2 = tf.get_variable(name='var1', shape=())
print(var1.name)
print(var2.name)
print(var1==var2)









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值