python-tensorflow 变量及作用域之作用域(二)

前一节讲变量

tf.get_variable和tf.Variable新建变量的区别,区别在于重复定义变量时,计算机是否自动更改变量属性名,tf.variable会自动更改属性名加‘_n’,tf.get_variable就会报错。

本节讲作用域

结论1变量名就是一个名字,变量的name属性是地址

a = tf.Variable(3.0,dtype=tf.float32)
print(a.name)

输出:Variable_1:0

定义了变量,变量名是a,name属性为Variable_1:0,就是一个地址

 

结论2tf.variable_scope('scope1')修订了块内的变量name属性,相当于新找了个地址存储变量值

with tf.variable_scope('scope1'):
    a = tf.Variable(3.0,dtype=tf.float32)
    print(a.name)

输出:scope1/Variable:0

 

结论3tf.name_scope('scope1')未修订了块内的变量name属性,未开辟空间存储变量

with tf.name_scope('scope2'):
    b = tf.get_variable(initializer=3.0,dtype=tf.float32,name='b')
    print(b.name)

输出:b:0

注:若name_scope对变量name属性有影响,那么输出应该为scope2/b:0

 

结论4:tf.name_scope有点类似于 b = a ,重新定义了名字,而且这个名字是给tensorboard用的,用以分块;未考虑tensorboard时,跟删掉没什么差别

with tf.name_scope('scope2'):
    with tf.variable_scope('v_scope'):
        b = tf.get_variable(initializer=3.0,dtype=tf.float32,name='b')
        print(b.name)

print(b.name)

 

输出:

v_scope/b:0

v_scope/b:0

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值