import tensorflow as tf

with tf.variable_scope("foo"):
    v = tf.get_variable("v", [1], initializer=tf.constant_initializer(1.0))
                        
#with tf.variable_scope("foo"):
   # v = tf.get_variable("v", [1])
    
with tf.variable_scope("foo", reuse=True):
    v1 = tf.get_variable("v", [1])
print(v == v1)

#with tf.variable_scope("bar", reuse=True):
   # v = tf.get_variable("v", [1])

吴裕雄 python 神经网络——TensorFlow 变量管理 随笔 第1张

with tf.variable_scope("root"):
    print(tf.get_variable_scope().reuse)
    
    with tf.variable_scope("foo", reuse=True):
        print(tf.get_variable_scope().reuse)
        
        with tf.variable_scope("bar"):
            print(tf.get_variable_scope().reuse)
            
    print(tf.get_variable_scope().reuse)

吴裕雄 python 神经网络——TensorFlow 变量管理 随笔 第2张

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
v1 = tf.get_variable("v", [1])
print(v1.name)

with tf.variable_scope("foo",reuse=True):
    v2 = tf.get_variable("v", [1])
print(v2.name)

with tf.variable_scope("foo"):
    with tf.variable_scope("bar"):
        v3 = tf.get_variable("v", [1])
        print(v3.name)
        
v4 = tf.get_variable("v1", [1])
print(v4.name)

吴裕雄 python 神经网络——TensorFlow 变量管理 随笔 第3张

with tf.variable_scope("",reuse=True):
    v5 = tf.get_variable("foo/bar/v", [1])
    print(v5 == v3)
    v6 = tf.get_variable("v1", [1])     
    print(v6 == v4)

吴裕雄 python 神经网络——TensorFlow 变量管理 随笔 第4张

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄