TensorFlow 作用域与操作符的受限范围

原文链接: TensorFlow 作用域与操作符的受限范围

上一篇: TensorFlow 变量定义 初始化和共享

下一篇: TensorFlow 图的基本操作

variable_scope 影响变量和操作符
name_scope 只影响操作符
with tf.name_scope(""),使用空字符串将作用域返回到顶层
tf.variable_scope("") 相当于添加一个空层

import tensorflow as tf

tf.reset_default_graph()

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

print("sp:", sp.name)  # sp: scope1
print("var1:", var1.name)  # var1: scope1/v:0

with tf.variable_scope("scope2"):
    var2 = tf.get_variable("v", [1])

    with tf.variable_scope(sp) as sp1:
        var3 = tf.get_variable("v3", [1])

        with tf.variable_scope(""):
            var4 = tf.get_variable("v4", [1])

print("sp1:", sp1.name)  # sp1: scope1
print("var2:", var2.name)  # var2: scope2/v:0
print("var3:", var3.name)  # var3: scope1/v3:0
# 多了一个空层
print("var4:", var4.name)  # var4: scope1//v4:0
# variable_scope 影响变量和操作符
with tf.variable_scope("scope"):
    # name_scope 只影响操作符
    with tf.name_scope("bar"):
        v = tf.get_variable("v", [1])
        x = 1.0 + v

        # 使用空字符串将作用域返回到顶层
        with tf.name_scope(""):
            y = 1.0 + v
print("v:", v.name)  # v: scope/v:0
print("x.op:", x.op.name)  # x.op: scope/bar/add
print("y.op:", y.op.name)  # y.op: add

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值