variable_scope作用域

import tensorflow as tf

with tf.variable_scope("scope1") as sp:
    var1 = tf.get_variable("v1", [1])

print("sp:", sp.name)
print("var1:", var1.name)

with tf.variable_scope("scope2"):
    var2 = tf.get_variable("v2", [1])
    with tf.variable_scope(sp) as sp1:   # 使用as  xxx后变量不受作用域限制
        var3 = tf.get_variable("v3", [1])
        with tf.variable_scope(""):
            var4 = tf.get_variable("v4", [1])


print("sp1:", sp1.name)
print("var2:", var2.name)
print("var3:", var3.name)
print("var4:", var4.name)
# sp: scope1
# var1: scope1/v1:0
# sp1: scope1
# var2: scope2/v2:0
# var3: scope1/v3:0
# var4: scope1//v4:0

with tf.variable_scope("scope3"):
    with tf.name_scope("bar"):
        v5 = tf.get_variable("v5", [1])   # name_scope只能限制op,不能限制变量的命名
        x = 1.0 + v5
        with tf.name_scope(""):
            y = 1.0 + v5
print("v4:", v5.name)
print("x.op:", x.op.name)
print("y.op:", y.op.name)
# v5: scope3/v5:0
# x.op: scope3/bar/add
# y.op: add   # y变成了顶层  var4多了一个空层

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值