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

原创
2018/07/19 12:04
阅读数 55

 

原文链接: 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
分享
返回顶部
顶部