Tensorflow共享变量

tf.name_scope() vs tf.variable_scope()

  1. tf.name_scope creates namespace for operators in the default graph
  2. tf.variable_scope creates namespace for both variables and operators in defaul graph
    3 tf.get_variable will ignore “name scope” but use “variable scope)

for example :

In [9]: with tf.name_scope("scope1"):
   ...:     v1=tf.get_variable("var",[1],dtype=tf.float32)
   ...:     v2=tf.Variable(1,name="var2",dtype=tf.float32)
   ...:     a=tf.add(v1,v2)
   ...:     

In [10]: print(v1.name)
var:0

In [11]: print(v2.name)
scope1/var2:0

In [12]: print(a.name)
scope1/Add:0

In [13]:  with tf.variable_scope("scope2"):
    ...:    v1=tf.get_variable("var",[1],dtype=tf.float32)
    ...:    v2=tf.Variable(1,name="var2",dtype=tf.float32)
    ...:    a=tf.add(v1,v2)
    ...: 

In [14]: print(v1.name)
scope2/var:0

In [15]: print(v2.name)
scope2/var2:0

In [16]: print(a.name)
scope2/Add:0

Use cases:

  • tf.name_scope(name) for (name scope)
  • tf.variable_scope(name_or_scope) for (variable scope )
  • -tf.op_scope(values,name,default_name=None) for (name scope)
  • tf.variable_op_scope(values,name_or_scope,Default_name=None) for (variable scope)

  • -
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值